简体   繁体   English

mysql_num_rows():提供的参数不是有效的MySQL结果资源(唯一)

[英]mysql_num_rows(): supplied argument is not a valid MySQL result resource (unique)

Basically I am pulling values fro four variables from a form and then using those values as WHERE statements in selecting from a table in my DB. 基本上,我是从表单中的四个变量中提取值,然后在从数据库的表中进行选择时将这些值用作WHERE语句。 I am only providing a snippet of my code here, because I have verified that the correct values are coming from the form to the variables in question, and the code works great and moves past the if statement into my else statement when I am only checking groupid and memberid. 我仅在此处提供我的代码的一部分,因为我已经验证了正确的值是从表单到有问题的变量的,并且代码工作得很好,并且在我仅检查时通过if语句进入了else语句。 groupid和memberid。

groupid is a 4 digit number. groupid是4位数字。 memberid is a 3 digit number. memberid是3位数字。 name is a full name like John A. Smith and dob is a date like 1900-10-31 name是一个全名,例如John A. Smith,而dob是一个日期,例如1900-10-31

The query I am trying to run is below. 我尝试运行的查询如下。 After I get the MySQL error it does display the message "Member does not exist." 收到MySQL错误后,它确实显示消息“成员不存在”。 What am I missing here? 我在这里想念什么?

$qry="SELECT * FROM members WHERE groupid = $groupid AND memberid = $memberid AND   membername = $name AND memberdob = $dob";
$result=mysql_query($qry);

if(mysql_num_rows($result) == 0)
    {
    echo "Member does not exist";
    }
 else  {....
$qry="SELECT * FROM members 
      WHERE groupid = $groupid 
      AND memberid = $memberid
      AND membername = '$name'
      AND memberdob = '$dob'";

Put quotes around membername and memberdob 各地把报价membernamememberdob

groupid is a 4 digit number. groupid是4位数字。 memberid is a 3 digit number. memberid是3位数字。 name is a full name like John A. Smith and dob is a date like 1900-10-31 name是一个全名,例如John A. Smith,而dob是一个日期,例如1900-10-31

That's what you think. 那是你的想法。 Golden rule: never trust any input from user . 黄金法则: 永远不要信任用户的任何输入

If you want to stick to the mysql extension, then first of all sanitize the input with mysql_real_escape_string . 如果要坚持使用mysql扩展,则首先使用mysql_real_escape_string清理输入。 And secondly, quote the variables you pass to the query. 其次,引用传递给查询的变量。 So, instead of 所以,代替

$qry = "... WHERE groupid = $groupid ...";

do

$qry = "... WHERE groupid = '" . mysql_real_escape_string($groupid) . "'...";

In your case, the most probable reason for the query to fail is the $name variable. 在您的情况下,查询失败的最可能原因是$name变量。 It contains spaces. 它包含空格。 The space is where the query breaks. 该空间是查询中断的地方。 Quoting the variable will solve the issue: 引用变量将解决问题:

$qry = "SELECT *
        FROM members
        WHERE groupid = '" . mysql_real_escape_string($groupid) . "'
        AND memberid = '" . mysql_real_escape_string($memberid) . "'
        AND membername = '" . mysql_real_escape_string($name) . "'
        AND memberdob = '" . mysql_real_escape_string($dob) . "'";

Also, improve the query execution with error handling. 另外,通过错误处理改善查询执行。 Simple example: 简单的例子:

$result = mysql_query($qry) or die('Invalid query: ' . mysql_error());

This will return detailed error description which will help you to determine the root cause. 这将返回详细的错误描述,这将帮助您确定根本原因。

As for the recommended alternatives for the discouraged mysql extension: please read about Choosing an API in the PHP manual. 至于不推荐使用的mysql扩展的推荐替代方法:请阅读PHP手册中有关选择API的信息。

Being unaware of the whole context of your application, I will assume that the variables are handled properly after being retrieved from the form. 由于不了解您的应用程序的整个上下文,因此我假设从表单中检索出变量后,它们已正确处理。

mysql normally complains about string fields if they are not properly enclosed in quotes. 如果字符串字段没有正确地用引号引起来,mysql通常会抱怨它们。 It could be that $name and $memberdob are retrieved from the form correctly but are not passed into the query as needed. 可能是$ name和$ memberdob是从表单中正确检索到的,但未根据需要传递到查询中。 Try changing your query to the following: 尝试将查询更改为以下内容:

$qry="SELECT * FROM members WHERE 
      groupid = $groupid AND memberid = $memberid AND
      membername = '$name' AND memberdob = '$dob'";

暂无
暂无

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

相关问题 mysql_num_rows():提供的参数不是有效的MySQL结果资源 - mysql_num_rows(): supplied argument is not a valid MySQL result resource mysql_num_rows():提供的参数不是有效的MySQL结果资源 - mysql_num_rows(): supplied argument is not a valid MySQL result resource 警告mysql_num_rows():提供的参数不是有效的MySQL结果 - Warning mysql_num_rows(): supplied argument is not a valid MySQL result 警告:mysql_num_rows():提供的参数不是没有mysql_error()的有效MySQL结果资源 - Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource with no mysql_error() 警告:mysql_num_rows():提供的参数不是有效的MySQL结果资源 - Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource 如何解决mysql_num_rows():提供的参数不是PHP中的有效MySQL结果资源 - How to solve mysql_num_rows(): supplied argument is not a valid MySQL result resource in php 警告:mysql_num_rows():提供的参数不是有效的MySQL结果资源 - Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource 警告:mysql_num_rows():提供的参数不是有效的MySQL结果资源nationalbuilder webhooks API - Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource nationbuilder webhooks API mysql_num_rows():提供的参数在15中不是有效的MySQL结果资源 - mysql_num_rows(): supplied argument is not a valid MySQL result resource in 15 修复错误:mysql_num_rows():提供的参数不是有效的MySQL结果资源 - Fixing the error: mysql_num_rows(): supplied argument is not a valid MySQL result resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM