Possible Duplicate:
php warning mysql_fetch_assoc
我只是实现了我的网站的一个简单的部分,它只是从头(subid)中获取一个变量,然后用数据库检查它,然后输出与该变量相关的其他字段。
但是我犯了这个错误-
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/admin/public_html/report.php on line 14
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''/home/admin/public_html/log/log_274b43e6ad_New Text Document (7).txt.txt' at line 1
这是我网页的代码
include 'connect_to_mysql.php';
$sql_header = mysql_query("SELECT * FROM system");
$header_array = mysql_fetch_assoc($sql_header);
$total_scans = $header_array['total_scans'];
$malware_detected = $header_array['malware_detected'];
$total_users = $header_array['total_users'];
$report_id = $_GET['log'];
var_dump($report_id);
$sql_report = mysql_query("SELECT * FROM logs WHERE log_name='$report_id");
var_dump($sql_report);
$report_array = mysql_fetch_assoc($sql_report) or die(mysql_error());
$file_name = $report_array['file_name'];
$file_size = $report_array['file_size'];
$submission_date = $report_array['submission_date'];
$result = $report_array['result'];
$status = $report_array['status'];
有什么问题吗?我已经尝试了所有的方法并检查了我的数据库,所有的名称都是正确的,所有的方法都是正确的,我甚至检查了数据库中的$report_id变量,它匹配,所以我不确定为什么它会出错。
谢谢你的帮助
最佳答案
你的代码不做任何错误检查,所以当查询失败时它会自动中断也就不足为奇了。检查错误,它将告诉您出了什么问题-如何做在manual on mysql_query()
或在reference question.中概述。例子:
$sql_report = mysql_query("SELECT * FROM logs WHERE log_name='$report_id");
// Bail out on error
if (!$sql_report)
{
trigger_error("Database error: ".mysql_error(), E_USER_ERROR);
die();
}
在您的特定情况下,您在
WHERE log_name='$report_id")
此外,您显示的代码易受SQL injection攻击。你需要像这样逃避你使用的每一个值:
$report_id = mysql_real_escape_string($_GET['log']);
要使其工作,您需要将查询中的每个值都放入引号中。
关于php - mysql_fetch_assoc错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8779552/