Php 如果数据库表中有0项,则返回错误
如果表中没有列出任何内容,我正在寻找显示错误消息的方法 我有一张照片桌 如果这个表是空的,我想回显一些东西 否则,请展示图片 在那张桌子里面我有Php 如果数据库表中有0项,则返回错误,php,mysql,Php,Mysql,如果表中没有列出任何内容,我正在寻找显示错误消息的方法 我有一张照片桌 如果这个表是空的,我想回显一些东西 否则,请展示图片 在那张桌子里面我有 id, name, url id = id name = name of image url = url of image. 如果没有行,我们有一个错误 $query1 = mysql_query("SELECT COUNT(*) FROM photos;"); mysql_fetch_array($query1); if(empty($query1
id, name, url
id = id
name = name of image
url = url of image.
如果没有行,我们有一个错误
$query1 = mysql_query("SELECT COUNT(*) FROM photos;");
mysql_fetch_array($query1);
if(empty($query1)) {
echo "nothing";
} else {
echo "good";
}
试试这个
$query1 = mysql_query("SELECT COUNT(*) FROM photos;");
$result = mysql_fetch_array($query1);
if(empty($result)) {
echo "nothing";
} else {
echo "good";
}
试试这个
$query = "SELECT * FROM photos";
$result= mysql_query($query);
$length= mysql_num_rows($result);
if($length>0)
{
while($rows = mysql_fetch_array($result))
{
echo $rows['name'];
echo "<img src='$rows[url]' />";
}
}
else
{
echo "Nothing to display";
}
$query=“从照片中选择*”;
$result=mysql\u query($query);
$length=mysql\u num\u行($result);
如果($length>0)
{
while($rows=mysql\u fetch\u array($result))
{
echo$rows['name'];
回声“;
}
}
其他的
{
回应“无需显示”;
}
希望这能起作用像
$sql = "SELECT COUNT(*) AS amountPhotos FROM photos";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if ($row["amountPhotos"] == 0) {
echo "There are no photos in the photo table.";
}
或
这大致概括了这个问题的答案: 他们甚至提供了一个示例代码:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) { //<--- here they check if number of rows returned is greater than 0 (so there is data to display)
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results"; //<----- nothing found
}
$conn->close();
?>
只要修改这个,你就可以开始了。谢谢,好问题。我启动了一个查询,从照片中选择计数(*)。如果(query1=0)show error在查询结果上使用
if()
,empty()
,可以添加带问题的代码吗?我选择这个是因为它最简单。其他的很好,但这更容易。谢谢@tobse
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) { //<--- here they check if number of rows returned is greater than 0 (so there is data to display)
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results"; //<----- nothing found
}
$conn->close();
?>