Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP多个条目_Php_Mysql - Fatal编程技术网

PHP多个条目

PHP多个条目,php,mysql,Php,Mysql,我正在用PHP/MYSQL创建一个小项目,但我无法按需要的方式运行查询。我有两张桌子 表1(字符): 表2(拼写): 我得到了输出: Name Spell1 Name Spell2 Name Spell3 但我需要的是: Name Spell1 Spell2 Spell3 我的问题是: $query = "SELECT char.name AS name, spells.spell_name AS spell FROM char, spell

我正在用PHP/MYSQL创建一个小项目,但我无法按需要的方式运行查询。我有两张桌子

表1(字符):

表2(拼写):

我得到了输出:

Name Spell1
Name Spell2
Name Spell3
但我需要的是:

Name Spell1
     Spell2
     Spell3
我的问题是:

 $query = "SELECT char.name AS name, spells.spell_name AS spell
              FROM char, spells
              WHERE (char.id = spells.spell_name)";

有什么想法吗?

我想你必须首先获得要查询的角色的ID,然后提取他/他有权使用的咒语。例如:

$char_id = 0; // value would be assigned arbitrarily.

$query = "SELECT *
FROM 'spells' s
WHERE s.char = $char_id;";

$result = $pdo->query($query);

while($row = $result->fetchObj()){
    // do something with the spells obj here
}

使用SQL时,您需要一次抓取整行,因此我认为您想要的情况是不可能的

正如戈登托11所写。进行两次选择,或使用两个结果集创建查询(一个命令中有多个选择),或接受当前状态(正常,您可以验证数据一致性)。我更喜欢当前状态,但有时使用任何描述的解决方案(基于查询频率、结果大小等)

如果需要列出此类数据,可以使用以下内容:

$currentName = null;

while ($row = mysql_fetch_object($result))
{
    if ($currentName != $row->name)
    {
        echo "<b>" . $row->name . "</b><br />";
        $currentName = $row->name;
    }

    echo $row->spell_name . "<br />";
}
$currentName=null;
while($row=mysql\u fetch\u object($result))
{
如果($currentName!=$row->name)
{
回显“$row->name.”
; $currentName=$row->name; } echo$row->拼写_名称。“
”; }
$char_id = 0; // value would be assigned arbitrarily.

$query = "SELECT *
FROM 'spells' s
WHERE s.char = $char_id;";

$result = $pdo->query($query);

while($row = $result->fetchObj()){
    // do something with the spells obj here
}
$currentName = null;

while ($row = mysql_fetch_object($result))
{
    if ($currentName != $row->name)
    {
        echo "<b>" . $row->name . "</b><br />";
        $currentName = $row->name;
    }

    echo $row->spell_name . "<br />";
}