方法:查询数据,若数据存在则输出(前端这个要自己写,我只写了一个echo)
代码如下:
//$conn = new mysqli($sql_server_name, $sql_username, $sql_password, $sql_db);
$timu = $_GET["timu"];
$sql = $conn-query("查询 * from problems where timu
='". $timu . "'"); // 从problems库里查 注意把"查询"改成"select",因为百度知道会屏蔽sql语句
if (mysqli_fetch_assoc($sql) 0) {
echo "已存在";
} else {
echo "不存在";
}
in_array() 函数在数组中搜索给定的值.
语法
in_array(value,array,type)
参数
value
必需.规定要在数组搜索的值.
array
必需.规定要搜索的数组.
type
可选.如果设置该参数为 true,则检查搜索的数据与数组的值的类型是否相同.
如:
php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
if (in_array("Glenn",$people))
{
echo "Match found";
else
echo "Match not found";
可以用下面的代码查看数据库中数据表是否存在:
$con = mysql_connect("localhost","$username","$password");
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("$datebase_name", $con);
$result = mysql_query("SELECT * FROM your_table");
while($row = mysql_fetch_array($result))
{ if(!$row){ echo "表不存在!"; } else{ echo "表存在!"; }
mysql_close($con);
$username="root";?//?连接数据库用户名
$password="root";?//?连接数据库密码
$mysql_database="chengdu";?//?数据库的名字
//?连接到数据库
$conn=mysql_connect($server_name,?$username,?$password);
//选择数据库
mysql_select_db($mysql_database,$conn);
//?从表中提取信息的sql语句
$chaKeName=mysql_query("select Coltd from KeHu where KeID ='$row[ID]'",$conn);
? if (count(mysql_fetch_array($chaKeName)) 0)
? ? ? ? {$KeCo="存在";}
? ? {$KeCo="不在";}
echo $KeCo;
//?关闭连接
mysql_close($conn);
if (count(mysql_fetch_array($chaKeName)) 0)? 在判断的这一句加了一个统计
用语句查,记录为真,还是为空.
$result = mysql_query("select +field+ from +table+ Where +field+='$field'";)
if( mysql_num_rows($result) != '0' ) {//判断记录不为空
$row = mysql_fetch_array($result);
$rows = $row['field'];
echo "没有data";
不要用mysql_fetch_object()这个函数来判断,这个返回来的是个数组;一般情况下是这么来判断的:$query="select * from ".$tablepre."common_members"." where username ='".$_POST['reg_username']."'";$result=mysql_query($query,$link);$num = mysql_num_rows($result); //取得结果集中行的数目if($num){echo 'script type="text/javascript"alert("用户名已被占用 ,请重新输入!");location.replace("../reg/index.php");/script';die();}
以上就是土嘎嘎小编为大家整理的php判断数据是否查到相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!