mysql_query(“SELECT...”) 对于不存在的主键值返回 NULL 值行,渲染 (mysql_num_rows > 0) 检查无用
我对使用 mysql 和 php 很陌生,这是我在网上找到的检索结果的方法:
$con = mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Cannot connect to the database");
$query = "SELECT ... FROM ... WHERE ID=5";
$result = mysql_query($query);
$num = mysql_num_rows($result);
if ($num > 0 ) {
// do stuff
} else {
// inform user
}
mysql_close($con);
ID
是我的表自动生成的非空主键。现在,我的表中有 20 条记录。如果我输入与任何这些现有记录相对应的 ID
值,一切都会顺利进行并按预期工作。
但是,如果我输入 ID
值,例如 54 或任何与我现有的 20 条记录不对应的值(意味着不存在具有此类 ID
的行) ),我希望 $num <= 0
并且我的条件语句将转到 else
块。但我发现情况并非如此 - 它仍然转到 TRUE
分支并继续到 // do stuff
块,所以我在 phpmyadmin 上运行查询并发现发现我在所有字段中都得到了 1 行带有 NULL
的数据。这就是 $num
返回 1
的原因,即 >0
。
问题:
- 我如何正确地检查不存在的行?这样,只有当我确实获得包含数据的行时,我的脚本的其余部分才会运行。
- 我在网上研究时发现了 mysql_store_result 和 mysql_free_result ,但没有足够的信息使它们正常工作。比如我将它们放在代码中的什么位置,即使没有它们,我怎么也能在 php 中做我现在需要做的事情,等等?
- 在 php 中处理 mysql 数据库时,我在这里缺少任何“最佳实践”的东西吗?
- 当我必须调用多个sql语句时,我所做的就是在我
之前循环执行
我的连接。这是“最佳实践”吗?$query
-$result
-$num
过程>mysql_close
非常感谢,伙计们,你们都太棒了。
更新:包括整个查询
SELECT Dyna,
Floater,
Can.Label as CanLabel,
GROUP_CONCAT(DISTINCT Vanity.Name SEPARATOR ', ') as VanityName,
GROUP_CONCAT(DISTINCT Univ SEPARATOR ', ') as Univ,
ShopDate,
ManuDate,
FlipTran,
Remarks,
Bib,
Abbrev,
ShopChar,
Comment,
Value,
ERUSD
FROM
(Vanity INNER JOIN ((Vanity INNER JOIN Shoppy ON Vanity.VanityID = Shoppy.VanityID) INNER JOIN ShoppyVanity ON Shoppy.ID = ShoppyVanity.ID) ON Vanity.MethodID = ShoppyVanity.MethodID) INNER JOIN UAC ON (Shoppy.VanityID = UAC.VanityID) AND (Vanity.VanityID = UAC.VanityID)
WHERE (((Shoppy.ID)=5))
i'm very new to using mysql with php, and here's what i found online to retrieve my results:
$con = mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Cannot connect to the database");
$query = "SELECT ... FROM ... WHERE ID=5";
$result = mysql_query($query);
$num = mysql_num_rows($result);
if ($num > 0 ) {
// do stuff
} else {
// inform user
}
mysql_close($con);
ID
is my table's autogenerated non-null primary key. right now, i have 20 records in my table. if i enter an ID
value corresponding to any of these existing records, everything goes well and works as intended.
however, if i enter an ID
value of, say, 54 or anything that doesn't correspond to any of my existing 20 records (meaning no row with such an ID
exists), i expect $num <= 0
and my conditional statement will go to the else
block. but i found out that wasn't the case - it still goes to the TRUE
branch and proceeds to // do stuff
block, so i ran the query on phpmyadmin and found out that i'm getting exactly 1 row with NULL
in all of the fields. that's why $num
was returning 1
which is >0
.
questions:
- how do i properly go about checking for nonexistent rows? so that the rest of my script will only run if i actually get a row that contains data.
- i found
mysql_store_result
andmysql_free_result
while researching this online but there's not enough info to make them work properly. like where in my code do i place them, how come i can do what i need to do right now in php even without them, etc? - any "best practice" stuff i'm missing here when dealing with mysql databases in php?
- when i have to call several sql statements, what i do is cycle through the
$query
-$result
-$num
process before imysql_close
my connection. is that "best practice"?
thank so much, guys, you're all amazing.
UPDATE: Including whole query
SELECT Dyna,
Floater,
Can.Label as CanLabel,
GROUP_CONCAT(DISTINCT Vanity.Name SEPARATOR ', ') as VanityName,
GROUP_CONCAT(DISTINCT Univ SEPARATOR ', ') as Univ,
ShopDate,
ManuDate,
FlipTran,
Remarks,
Bib,
Abbrev,
ShopChar,
Comment,
Value,
ERUSD
FROM
(Vanity INNER JOIN ((Vanity INNER JOIN Shoppy ON Vanity.VanityID = Shoppy.VanityID) INNER JOIN ShoppyVanity ON Shoppy.ID = ShoppyVanity.ID) ON Vanity.MethodID = ShoppyVanity.MethodID) INNER JOIN UAC ON (Shoppy.VanityID = UAC.VanityID) AND (Vanity.VanityID = UAC.VanityID)
WHERE (((Shoppy.ID)=5))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用组聚合函数,例如
GROUP_CONCAT
,MySQL 将从整个结果集中创建一个组。如果没有结果,它将在NULL
值上运行它们,从而产生空行。尝试将其添加到查询的最后以删除其中没有有效数据的行:HAVING VanityName IS NOT NULL
If you use group aggregate functions such as
GROUP_CONCAT
MySQL will create a group out of your whole result set. If there are no results, it will run them onNULL
values instead, resulting in an empty row. Try adding this to the very end of your query to get rid of rows that don't have valid data in them:HAVING VanityName IS NOT NULL
将其更改为
Change it to
试试这个:
Try this:
使用不同的
mysql_*
函数,例如mysql_fetch_assoc
。如果您阅读手册,可以在 http://www.php 找到.net/manual/en/function.mysql-fetch-assoc.php,如果没有要获取的行,您将看到它返回false
。通常,您可以将该调用作为while
循环的条件,即:这将循环直到读取最后一行。
Use a different
mysql_*
function, such asmysql_fetch_assoc
. If you read the manual, found at http://www.php.net/manual/en/function.mysql-fetch-assoc.php, you will see that it returnsfalse
if there are no rows to fetch. Typically, you can make that call the conditional for awhile
loop, i.e.:This will loop until it has read the last row.