回答

收藏

检查mysql_query是否返回任何东西

技术问答 技术问答 286 人阅读 | 0 人回复 | 2023-09-14

$query = "SELECT * FROM `table`";
/ x/ X) N3 b( c  C$results = mysql_query($query, $connection);
- p% a/ A# E2 b& x5 p2 J# t/ x如果“表”没有行。什么是最简单的检查方法?$ C  ^% W9 }/ e9 H1 U
               
4 O+ Y- |& Q5 ^+ m5 a; C; ~解决方案:
+ X! |+ O( |' Y7 f3 e               
+ u# m9 w; L5 R1 X# c
% k" V9 Q3 K$ d( N4 w; o# \: X9 i# V/ ]6 Y; u) f
                您可以使用mysql_num_rows($4 s0 w9 d- h( C# \7 X
results)来检查是否返回了0行,或者使用以下更快的替代方法:
; j2 _8 E7 n/ _7 a$query = "SELECT COUNT(*) AS total FROM table";$ b  L) C& O' o; _0 b( ^; Z$ f
$results = mysql_query($query, $connection);1 R- U9 p0 O. x  y1 d- N
$values = mysql_fetch_assoc($results);
9 t4 v6 A" h4 o+ q0 N5 i$num_rows = $values['total'];
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则