![](/img/trans.png)
[英]PHP and MySQLi check to see if the table exists and if it doesn't create it
[英]PHP - Check table to see if entry exists
添加之前,我需要检查表中是否存在记录。 我做了一些挖掘,这也是人们不断回来的原因:
$result= mysql_query("SELECT id FROM mytable WHERE city = 'c7'");
if(mysql_num_rows($result) == 0) {
// row not found, do stuff...
} else {
//row found, do other stuff...
}
或那里的一些变化。
除了$ result从未返回正数的事实外,此逻辑正是我所需要的。
该记录确实存在,应该返回肯定的结果。
我也试过
$sql="SELECT COUNT(email) FROM table WHERE email=$mail;";
$yesorno = mysqli_query($sql);
echo $yesorno ;
作为测试,回声不返回任何值。
您需要先检查查询是否成功运行,否则可能有问题
$Query = "Select id from mytable where city = 'c7' ";
if($result = mysql_query($Query)) {
if ( mysql_num_rows($result) == 0 ) {
// no rows found ;
} else {
// row exist ;
}
} else {
echo "Query failed ". $Query;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.