首先,对于这个漫长的问题感到抱歉......但这是我的头脑。任何帮助都会得到很好的接受。
我编写了以下函数来从mysql数据库返回数据:
function injuryTable()
{
# get all players that are injured and their injuires...
$sql = "SELECT players.id, players.injury_id, players.pname, injuries_date.name, start_date, end_date
FROM players INNER JOIN injuries_date
ON injury_id = injuries_date.id";
$result = sqlQuery($sql);
return $result;
}
sqlQuery函数如下:
function sqlQuery($sql)
{
$products = array();
$link = dbConnect('localhost', 'root', '', 'umarrr');
$result = mysqli_query($link, $sql);
while ($row = mysqli_fetch_array($result))
{
$products[] = $row;
}
# return each row:
return $products;
#mysqli_close($link);
}
它全部连接到数据库,一切正常。 但是,当我尝试迭代结果时:它只返回一行:
$injury_table = injuryTable();
// make it more readable:
foreach ($injury_table as $table);
{
echo $table['pname'];
echo $table['name'];
echo $table['start_date'];
echo $table['end_date'];
}
我上面写的sql语句在mysql查询浏览器中运行得很好,所以有谁知道这里的问题可能是什么?
print_r($injury_table)
Array ( [0] => Array ( [0] => 1 [id] => 1 [1] => 6 [injury_id] => 6 [2] => person [pname] => person [3] => wrist [name] => wrist [4] => 2008-11-21 [start_date] => 2008-11-21 [5] => 2010-11-11 [end_date] => 2010-11-11 ) [1] => Array ( [0] => 2 [id] => 2 [1] => 5 [injury_id] => 5 [2] => woman [pname] => woman [3] => neck [name] => neck [4] => 2009-11-12 [start_date] => 2009-11-12 [5] => 2010-09-09 [end_date] => 2010-09-09 ) [2] => Array ( [0] => 3 [id] => 3 [1] => 4 [injury_id] => 4 [2] => girl [pname] => girl [3] => groin [name] => groin [4] => 2010-11-27 [start_date] => 2010-11-27 [5] => 2010-12-01 [end_date] => 2010-12-01 ) [3] => Array ( [0] => 4 [id] => 4 [1] => 1 [injury_id] => 1 [2] => boy [pname] => boy [3] => achilles [name] => achilles [4] => 2010-02-01 [start_date] => 2010-02-01 [5] => 2010-03-23 [end_date] => 2010-03-23 ) [4] => Array ( [0] => 5 [id] => 5 [1] => 2 [injury_id] => 2 [2] => man [pname] => man [3] => toe [name] => toe [4] => 2010-01-01 [start_date] => 2010-01-01 [5] => 2010-02-02 [end_date] => 2010-02-02 ) )
答案 0 :(得分:3)
要检查的一些事项:
mysqli_query()
的返回值。您假设查询成功,并且查询每次都无法检查的原因太多。失败时它将返回布尔值FALSE。sqlQUery()
函数内的静态变量中。在您需要(或需要)的情况下并不多)每次同时使用多个手柄,或每次都使用全新的闪亮清洁手柄。mysqli_num_rows()
以查看查询的PHP版本返回了多少?答案 1 :(得分:1)
为什么要为每次通话打开与数据库的新连接?这是执行查询的一种非常低效的方式。我会将数据库连接作为参数传递,或者由于您使用的是mysqli,只需将$link
作为参数传递即可。
至于你的代码无法正常工作的原因,我不知道,但您可以尝试使用以下内容进行一些基本的错误报告:
$result = mysqli_query($link, $sql) or
trigger_error('Query Failed: ' . mysqli_error($link));
我还会将MYSQL_ASSOC
添加到提取功能,因为您没有使用基于索引的数组,这将使您的脚本更加高效。
希望这会帮助你。
答案 2 :(得分:1)
真的很糟糕(也许是侮辱性的)回答...
foreach ($injury_table as $table);
{
echo $table['pname'];
echo $table['name'];
echo $table['start_date'];
echo $table['end_date'];
echo '<br/>'; // HTML new line
}
或者
foreach ($injury_table as $table);
{
echo $table['pname'];
echo $table['name'];
echo $table['start_date'];
echo $table['end_date'];
echo "\n"; // Console new line
}