简体   繁体   English

PHP 中 mysql_query() 函数中的奇怪错误(查询中的和子句)

[英]Strange Error in mysql_query() function in PHP (And clause in Query)

When I add AND operator in mysql_query() function, it stops working and anything after that stops working!当我在 mysql_query() 函数中添加 AND 运算符时,它停止工作,然后停止工作! For Example: When i wrote this:例如:当我写这个时:

 $query1 = mysql_query("SELECT *  FROM chat1 where friendname = '$_POST[fname]' ");

 $row= mysql_fetch_array($query1) or die(mysql_error()); 
 echo "$row[message]";

The above query runs successfully !以上查询运行成功!

But when i do this :但是当我这样做时:

 $query1 = mysql_query("SELECT *  FROM chat1 where friendname = '$_POST[fname]' AND username = '$_POST[uname]' ");

 $row= mysql_fetch_array($query1) or die(mysql_error()); 

 echo "$row[message]";

I get Null output!我得到空输出! I think the "AND" operator is not working!!!我认为“AND”运算符不起作用!!! please help me with this!!请在这件事上给予我帮助!! Have a look at my complete code and Database Snapshot!看看我的完整代码和数据库快照! Click here 点击这里

If it is returning NULL then probably the record doesn't exists.如果它返回 NULL 那么可能记录不存在。 Try to output this query on the screen and post the raw query here.尝试在屏幕上输出此查询并在此处发布原始查询。

Maybe your search needs a LIKE instead of a =也许您的搜索需要LIKE而不是=

Likely, the row(s) you are looking for do not exist.很可能,您要查找的行不存在。

The AND is a boolean operator that requires that both expressions have to evaluate to true. AND是一个布尔运算符,它要求两个表达式的计算结果都为真。 In the context of your query, that means for a row to be returned, both of the conditions have to be true on that single row.在您的查询上下文中,这意味着要返回一行,该单行的两个条件都必须为真。

I suspect that you may want an OR those two conditions.我怀疑您可能需要OR这两个条件。 Did you want to return only rows that meet both criteria, or did you want any rows that have fname with a certain value, along with any rows that have uname of a specific value?您是否只想返回满足这两个条件的行,或者您是否想要任何具有特定值的fname行,以及具有特定值的uname的任何行? If the first query is returning rows, then replacing AND with OR should return you some rows.如果第一个查询返回行,那么用OR替换AND应该返回一些行。


For debugging this type of problem, generate the SQL text into a variable, and then echo or var_dump the SQL text, before you send it to the database.要调试此类问题,请将 SQL 文本生成到一个变量中,然后在将 SQL 文本发送到数据库之前回显或 var_dump 该 SQL 文本。

eg例如

    $sql = "SELECT *  FROM chat1 where friendname = '" 
         . mysql_real_escape_string($_POST['fname'])
         ."' ";
    echo "SQL=" . $sql ;  # for debugging

Take the text of SQL statement that's emitted to another client, to test the SQL statement, to figure out if the SQL statement is actually returning the resultset you expect it to return.获取发送给另一个客户端的 SQL 语句的文本,以测试 SQL 语句,以确定该 SQL 语句是否实际返回了您希望它返回的结果集。

(In your code, reference the $sql in the function that prepares/executes the SQL statement.) (在您的代码中,在准备/执行 SQL 语句的函数中引用$sql 。)

Follow this pattern for all dynamically generated SQL text: generate the SQL text into a variable.对于所有动态生成的 SQL 文本,请遵循此模式:将 SQL 文本生成到变量中。 For debugging, echo or var_dump or otherwise emit or log the contents of the variable.对于调试, echovar_dump或以其他方式发出或记录变量的内容。 Take the SQL text to another client and test it.将 SQL 文本带到另一个客户端并对其进行测试。

Dumping code that isn't working on to StackOverflow is not the most efficient way to debug your program.将不工作的代码转储到 StackOverflow并不是调试程序的最有效方法。 Narrow down where the problem is.缩小问题所在。

How to debug small programs http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ 如何调试小程序http://ericlippert.com/2014/03/05/how-to-debug-small-programs/


NOTES笔记

You probably want to verify that $_POST['fname']) contains a value.您可能想验证$_POST['fname'])包含一个值。

It's valid (SQL-wise) for a SELECT statement to return zero rows, if there are no rows that satisfy the predicates.如果没有满足谓词的行,则 SELECT 语句返回零行是有效的(SQL 明智的)。

Potentially unsafe values must be properly escaped if you include them in the text of a SQL statement.如果将潜在的不安全值包含在 SQL 语句的文本中,则必须正确转义它们。 (A better pattern is to use prepared statements with bind placeholders , available in the (supported) mysqli and PDO interfaces. (更好的模式是使用带有绑定占位符的准备好的语句,在(支持的)mysqli 和 PDO 接口中可用。

Also, use single quotes around fname .... eg另外,在fname周围使用单引号 .... 例如

    $_POST['fname']
           ^     ^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM