运行脚本时出错

时间:2013-04-19 12:00:31

标签: php mysql

执行此脚本时,我会收到此错误代码 警告:mysql_fetch_assoc():提供的参数不是第47行/home/a777gqka/public_html/testserver/index.php中的有效MySQL结果资源

$query = mysql_query("SELECT
e.id,
ts.name AS stage_name,
DATE_FORMAT(e.startdate, '2013-05-15') AS startdate,
DATE_FORMAT(e.startdate, 'H:i') AS starttime,
MIN(if( ep.number = 1, p.name, NULL)) AS home_team,
if(e.status_type = \"notstarted\", \"-\",(MIN(if(ep.number = 1, r.value, NULL))))  AS home_score,
if(e.status_type = \"notstarted\", \"-\",(MIN(if(ep.number = 2, r.value, NULL)))) AS away_score,
MIN(if(ep.number = 2, p.name, NULL)) AS away_team,
es.name AS status_text
FROM
tournament_template AS tt INNER JOIN
tournament AS t ON t.tournament_templateFK = tt.id INNER JOIN
tournament_stage AS ts ON t.id = ts.tournamentFK INNER JOIN
event AS e ON ts.id = e.tournament_stageFK INNER JOIN
event_participants AS ep ON e.id = ep.eventFK LEFT JOIN
status_desc AS es ON e.status_descFK = es.id LEFT JOIN
participant AS p ON ep.participantFK = p.id LEFT JOIN
result AS r ON ep.id = r.event_participantsFK AND r.result_code = \"runningscore\" LEFT JOIN
property AS prop ON e.id = prop.objectFK AND prop.object =\"event\" AND prop.name = \"Live\"
WHERE
tt.sportFK = ".$sportFK."  AND
e.startdate BETWEEN ".$TODAY_FROM." AND ".$TODAY_TO." AND
prop.value = \"yes\"
GROUP BY
e.id
ORDER BY 
ts.id, e.startdate, e.id");
while($row = mysql_fetch_assoc($query)){
// Do something
}

4 个答案:

答案 0 :(得分:0)

 $query = "SELECT .... ";
 if($result = mysql_query($query)){
     while($row = mysql_fetch_assoc($result)){
          //...
     }
 } else {
     echo mysql_error();
 }

除此之外你应该输出你的错误,你应该至少使用mysqli。不推荐使用mysql_ *函数,将使用PHP 5.5删除它。

答案 1 :(得分:0)

您没有执行查询。

在while循环之前添加$result = mysql_query($query),并将循环测试更改为$row = mysql_fetch_assoc($result)

此外,不推荐使用mysql_ *函数,并且将在以后的PHP更新中删除它们。切换到mysqli_ *或PDO。

我个人建议PDO。

答案 2 :(得分:0)

$query = mysql_query("SELECT...") or die(mysql_error());
if ($query){
while($row = mysql_fetch_assoc($query)){
// Do something
}
}

答案 3 :(得分:-1)

请试试这个

if(mysql_num_rows($query) > 0){

   while($row = mysql_fetch_assoc($query)){
   // Do something
   }
}
else{
    echo "No record found";
}