Php 返回0的Mysql num行数
我正在从数据库中提取线程Php 返回0的Mysql num行数,php,mysql,Php,Mysql,我正在从数据库中提取线程 $getthreads = mysql_query("SELECT * from threads WHERE subcat=" . $thread . "") or die(mysql_error()); while ($threads = mysql_fetch_array($getthreads)) { if (mysql_num_rows($threads) > 0) { 警告:mysql_num_rows()希望参数1是第30行C:\xampp\htdo
$getthreads = mysql_query("SELECT * from threads WHERE subcat=" . $thread . "") or die(mysql_error());
while ($threads = mysql_fetch_array($getthreads)) {
if (mysql_num_rows($threads) > 0) {
警告:mysql_num_rows()希望参数1是第30行C:\xampp\htdocs\views\threads.php中给定的资源数组
但是数据库中有正确的信息,下面是我的$threads变量:
$thread = isset($_GET['thread']) ? $system->escape($_GET['thread']) : null;
为什么它会给我这个错误?我猜
subcat
是一个字符串变量。如果是这样,这可能会奏效:
SELECT * from threads WHERE subcat='" . $thread . "'"
接下来是mysql命令已经过时的强制性声明
接下来,代码容易受到SQL注入攻击。您应该学习如何使用参数
$Query = "SELECT * from threads WHERE subcat='".mysql_real_escape_string($thread)."'";
$getthreads = mysql_query($Query) or die(mysql_error());
if (mysql_num_rows($getthreads) > 0) {
参数:正在评估的结果资源。此结果来自对mysql_query()的调用
PS使用mysqli或pdo,mysql不推荐使用试试这个,添加了“.mysql\u real\u escape\u string($thread)。”
mysql\u real\u escape\u string-转义字符串中的特殊字符,以便在SQL语句中使用
$getthreads = mysql_query("SELECT * from threads WHERE subcat='" . mysql_real_escape_string($thread) . "' ") or die(mysql_error());
if (mysql_num_rows($getthreads) > 0) {
while ($threads = mysql_fetch_array($getthreads)) {
....
}
}
首先,您使用的mysql\u num\u行参数错误。用,
if(mysql_num_rows($getthreads) > 0) {
}
但是,实际上你不需要这样做。在从查询中获取任何行之前,循环不会运行。我想,如果你的疑问是错误的,你可能会面临问题。如前所述,我看到您将null值分配给$thread
,似乎您希望在数据库中检查null
。您不应该将null
与=
运算符匹配。即使在大多数情况下它看起来是正常的,但它的行为可能是预期的。你必须使用
... WHERE subcat is NULL") ...
匹配数据库中的null
值。这样,您必须更改代码的逻辑以测试null
。我想,可能是这样的
... WHERE subcat = '". mysql_real_escape_string($thread).'" OR is NULL) ...
如果,我正确地理解了您希望通过此代码实现的目标。$threads实际上不是
资源
,您不能使用mysql\u num\u rows
。若在一个循环中,您不需要这样做,若行为空,它将结束,以了解如何解决此问题。sql攻击的主观性是什么?@user3162803。这意味着如果$thread
有一个特定的值,那么用户就可以养成这个习惯。看这幅漫画。转义字符串不重要吗?@user3162803。是的。我首先关注语法问题最可能的原因,即缺少单引号。Nigro的答案更好,值得接受。我不知道mysql:)我会改变它。@Nigro引用什么?谢谢,我会投最好的答案。为什么投反对票???我很好奇downvoter的原因。。。