Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQL查询不返回任何内容_Php_Mysql - Fatal编程技术网

Php MySQL查询不返回任何内容

Php MySQL查询不返回任何内容,php,mysql,Php,Mysql,我在堆栈上搜索了这个问题,但什么也没找到。这可能是语法问题,但我不明白 //Include necessary files include_once("inc/mysql.inc.php"); include_once("inc/security.inc.php"); //SECURITY: Check input before adding them into the table. $u = security_checkinput($u); //Connect to the MySQL d

我在堆栈上搜索了这个问题,但什么也没找到。这可能是语法问题,但我不明白

//Include necessary files
include_once("inc/mysql.inc.php");
include_once("inc/security.inc.php");

//SECURITY: Check input before adding them into the table.
$u = security_checkinput($u);

//Connect to the MySQL database.
mysql_dbconnection();
//Go get users id corresponding to the search
$u = mysql_query("SELECT id FROM users
            WHERE name LIKE \"%$u%\" OR active LIKE \"%$u%\" 
            ORDER BY name ASC") or die("There is a server error : " . mysql_error());
$u = mysql_fetch_row($u);
//Close database connection
mysql_dbclose();

//Return the array.
return $u;
在查询之后,我在$u上尝试了一个mysql_num_行,它返回0。不幸的是,users表和name列中有一些内容

有什么帮助吗? 非常感谢

编辑:问题来自安全检查功能。我希望尽可能避免sql注入,因此我做了以下几点:

    function security_checkinput ($s)
{
    $s = stripslashes($s);
    if (!is_numeric($s))
        $s = "'".addslashes($s)."'";
    return $s;  
}

您是否首先回显您的查询并尝试直接在数据库中运行它

无论如何,要正确使用子句

$u = mysql_query("SELECT id FROM users
            WHERE name LIKE '%$u%' OR active LIKE '%$u%' 
            ORDER BY name ASC") or die("There is a server error : " . mysql_error());

您不需要在此处使用“/”,phpvariable将在此处自行处理。试试这个

我不完全确定这是否是您的问题的唯一原因,但是

WHERE name LIKE \"%$u%\" OR active LIKE \"%$u%\" 
应该是

WHERE name LIKE '%$u%' OR active LIKE '%$u%' 
$query = mysql_query("SELECT id FROM users
            WHERE name LIKE '%$u%' OR active LIKE '%$u%' 
            ORDER BY name ASC") or die("There is a server error : " . mysql_error());

您是否尝试过直接对数据库运行此查询?它返回那里的是什么?

美元是什么?您的查询很好,似乎没有任何匹配的行


第1点:您将查询赋值给$u,并在LIKE语句中使用相同的变量

第2点: 您正在错误地编写语句。应该是

WHERE name LIKE '%$u%' OR active LIKE '%$u%' 
$query = mysql_query("SELECT id FROM users
            WHERE name LIKE '%$u%' OR active LIKE '%$u%' 
            ORDER BY name ASC") or die("There is a server error : " . mysql_error());

我更喜欢CONCAT,因为如果有很多变量,你不需要写n个OR语句。

你的安全函数看起来是个问题。 例如,只要$u是单词test,它就会返回'test' 已经在SQL语句中

把它从你的安全功能中移除,你会没事的。 不过,我确实建议更改带斜杠,并将斜杠添加到更合适的内容中,例如mysql\u real\u escape\u string

基于旧代码的修复示例:

function security_checkinput ($s)
{
    $s = stripslashes($s);
    if (!is_numeric($s))
        $s = addslashes($s);
    return $s;  
}
mysql\u real\u escape\u字符串用法示例(您应该真正使用此字符串):


确保你也使用了Nishu Tayal的溶液。斜杠不属于这里。

您是否尝试过回显SQL语句,并直接在数据库中运行它?请查看操作符的工作原理。
$u=security\u checkinput($u)--
$u
包含什么?我很确定它不包含您期望它包含的内容。$u是php变量,如上面查询中定义的…:p那没用D字符串,实际上它取决于MySQL运行的SQL模式。通常MySQL会乐于接受双引号字符串——但是——如果启用了双引号,则双引号将用作反勾号——用于括起列名的字符串。是的,这是一个非常好的观点。我认为这里的问题可能与美元有关——这还没有提供给我们。你说得对,我的错。。。我发现问题出在我的安全检查功能上。我把它贴到了原来的帖子里。