我正在使用以下来选择某张照片,如何合并'IF EXISTS',以便在没有ID时回显一些文本找到。如何检查一行中是否存在ID并在不存在的情况下回显一些文本
$Result = mysql_query("SELECT * FROM photos WHERE UID = '$ID' limit 30")
我正在使用以下来选择某张照片,如何合并'IF EXISTS',以便在没有ID时回显一些文本找到。如何检查一行中是否存在ID并在不存在的情况下回显一些文本
$Result = mysql_query("SELECT * FROM photos WHERE UID = '$ID' limit 30")
if (mysql_num_rows($Result)) echo "You have photos!";
else echo "Dude... take some photos!";
如果ID存在,你会找到行,所以在ID不存在的情况下,num_rows将不返回任何内容。所以我们必须先检查ID是否存在。 – Relm
假设您有一个包含用户标识(UID)的表,为什么不只是查询有效的用户标识? '$ Result = mysql_query(“SELECT UID FROM users WHERE UID ='$ ID'limit 1”);'' –
当无法找到ID,在$结果将包含0值。
现在你可以问,$结果多少值包含mysql_num_rows()功能是这样的:
if (mysql_num_rows($Result) == 0) echo "No Photo with $ID found";
else {
while($photo = mysql_fetch_object($Result)) {
//Here $photo contains a photo with the UID $ID
}
}
这是我落得这样做。
<?php
$data = preg_replace ('#[^0-9 ]#i', '', $_POST['data']);
require_once 'db_conx.php';
$Result = mysql_query("SELECT * FROM photos WHERE pid = '$data' limit 30")
or die (mysql_error());
while($row = mysql_fetch_array($Result)){
$NoPhotos = (empty($row['photo'])) ? 'This Business Profile has no photos, you can send this business a message and ask them to upload some ' : '';
$Photos = (!empty($row['photo'])) ? '<img width="32%" height="70" src="'.$row['photo'].'" class="ProPhotosID">' : '';
echo '
'.$NoPhotos.''.$Photos.'
';
}
?>
您的意思是mysql_num_rows($结果)== 0 http://www.php.net/mysql_num_rows? – Andrew