我有这段代码:
<?php
include 'connect.inc.php';
include 'header.inc.php';
$mesiace = array("Január", "Február", "Marec", "Apríl", "Máj", "Jún", "Júl",
"August", "September", "Október", "November", "December");
echo '<div id="rozpis">';
$sql = "SELECT game_id, rival_id, result, h_a, UNIX_TIMESTAMP(datum) AS datum FROM game ORDER BY datum ASC";
$result = mysql_query($sql, $db) or die("ERROR: ".mysql_error());
if(mysql_num_rows($result) > 0)
{
echo '<table>';
echo '<tr><td>Dátum</td><td>Domáci</td><td>Hostia</td><td>Výsledok</td></tr>';
while($row = mysql_fetch_array($result)) //here is the problem
{
extract($row);
$prikaz = 'SELECT rival_id, name FROM rival WHERE rival_id = '. $rival_id;
$vystup = mysql_query($prikaz, $db) or die(mysql_error($db));
$rival = mysql_fetch_array($vystup);
echo '<tr>';
echo '<td>' .date("j. F Y", $datum). '</td>';
if ($h_a == 1)
{
echo '<td> Capitals </td>';
echo '<td>' .$rival['name']. '</td>';
}
else
{
echo '<td>' .$rival['name']. '</td>';
echo '<td> Capitals </td>';
}
echo '<td><a href="zapas.php?zapas='. $game_id.'">' .$result. '</td>';
echo '</tr>';
}
echo '</table>';
}
else
echo 'No matches';
echo '</div>';
include 'zahlavie.php';
?>
当我运行此脚本时,它会写下此警告:
Warning: mysql_fetch_array() expects parameter 1 to be resource,
string given in H:\xampp\htdocs\rozpis.php on line 16
我真的不知道问题出在哪里。在表中我有三行,但它只写第一行然后写这条消息。当我在phpMyAdmin中运行$query
时,它正确运行或当我使用echo mysql_num_rows($result)
时,它也会正确返回。
答案 0 :(得分:5)
问题是您正在使用函数extract
,并且您的一个db列被命名为“result”。因此会用一些字符串覆盖变量$ result,这当然是错误的。
$sql = "SELECT game_id, rival_id, result, h_a, UNIX_TIMESTAMP(datum) AS datum FROM game ORDER BY datum ASC";
$result = mysql_query($sql, $db) or die("ERROR: ".mysql_error());
while($row = mysql_fetch_array($result)) {
extract($row); // <- here is the problem