解决方法
如果您使用的是MysqL数据库,那么请使用以下命令 –
$query = "SELECT username from my_table where username='bob'"; $result = MysqL_query($query); if(MysqL_num_rows($result) > 0) { // row exists. do whatever you would like to do. }
如果要使用PDO (PHP Data Object)
,as alex建议,那么使用以下代码 –
$dbh = new PDO("MysqL:host=your_host_name;dbname=your_db_name",$user,$pass); $stmt = $dbh->prepare("SELECT username from my_table where username = ':name'"); $stmt->bindParam(":name","bob"); $stmt->execute(); if($stmt->rowCount() > 0) { // row exists. do whatever you want to do. }