mysql_query(“SELECT...”) 对于不存在的主键值返回 NULL 值行,渲染 (mysql_num_rows > 0) 检查无用

发布于 2024-12-10 00:35:44 字数 2025 浏览 0 评论 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

问题:

  1. 我如何正确地检查不存在的行?这样,只有当我确实获得包含数据的行时,我的脚本的其余部分才会运行。
  2. 我在网上研究时发现了 mysql_store_result 和 mysql_free_result ,但没有足够的信息使它们正常工作。比如我将它们放在代码中的什么位置,即使没有它们,我怎么也能在 php 中做我现在需要做的事情,等等?
  3. 在 php 中处理 mysql 数据库时,我在这里缺少任何“最佳实践”的东西吗?
  4. 当我必须调用多个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:

  1. 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.
  2. i found mysql_store_result and mysql_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?
  3. any "best practice" stuff i'm missing here when dealing with mysql databases in php?
  4. when i have to call several sql statements, what i do is cycle through the $query-$result-$num process before i mysql_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

对你而言 2024-12-17 00:35:44

如果您使用组聚合函数,例如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 on NULL 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

老旧海报 2024-12-17 00:35:44
$num = mysql_numrows($result);

将其更改为

$num = mysql_num_rows($result);
$num = mysql_numrows($result);

Change it to

$num = mysql_num_rows($result);
面犯桃花 2024-12-17 00:35:44

试试这个:

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 Shoppy ON (Vanity.VanityID = Shoppy.VanityID)
       INNER JOIN ShoppyVanity  ON (Shoppy.ID = ShoppyVanity.ID)
       INNER JOIN ShoppyVanity  ON (ShoppyVanity.MethodID=Vanity.MethodID) 
       INNER JOIN UAC           ON (Shoppy.VanityID = UAC.VanityID AND Vanity.VanityID =UAC.VanityID) 
       WHERE Shoppy.ID=5

Try this:

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 Shoppy ON (Vanity.VanityID = Shoppy.VanityID)
       INNER JOIN ShoppyVanity  ON (Shoppy.ID = ShoppyVanity.ID)
       INNER JOIN ShoppyVanity  ON (ShoppyVanity.MethodID=Vanity.MethodID) 
       INNER JOIN UAC           ON (Shoppy.VanityID = UAC.VanityID AND Vanity.VanityID =UAC.VanityID) 
       WHERE Shoppy.ID=5
晒暮凉 2024-12-17 00:35:44

使用不同的 mysql_* 函数,例如 mysql_fetch_assoc。如果您阅读手册,可以在 http://www.php 找到.net/manual/en/function.mysql-fetch-assoc.php,如果没有要获取的行,您将看到它返回 false。通常,您可以将该调用作为 while 循环的条件,即:

while( $row = @mysql_fetch_assoc( $result ) ){
    // do something
}

这将循环直到读取最后一行。

Use a different mysql_* function, such as mysql_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 returns false if there are no rows to fetch. Typically, you can make that call the conditional for a while loop, i.e.:

while( $row = @mysql_fetch_assoc( $result ) ){
    // do something
}

This will loop until it has read the last row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文