php - mysql_fetch_assoc 错误

标签 php mysql html

<分区>

Possible Duplicate:
php warning mysql_fetch_assoc

我只是在我的网站上实现一个简单的部分,它只是从 header (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/

相关文章:

php - MySQL HY000 1366 : Trying to store 4 byte utf8 character

php - 有没有比 VALUES($var1, $var2, $var3) 更好的方法向 MySQL 查询插入值?

javascript - 无法在 iframe 内移动滚动条

javascript - jQuery - 尝试选择选项菜单时包含问题

PHP、mysql 和两个数据库 - 使用不同的架构从第一个数据库中的表更新第二个数据库表中的某些行

php - 将变量传递到 url

mysql - 为什么在多次执行查询后收到一条成功消息,显示 `2 rows inserted`?

PHP 在需要时添加额外的空格

php - 当前时间秒

html - 我可以包含外部 SVG defs