检查记录是否存在于php中


checking if a record exist or not in php

我想检查是否有任何记录给定的电子邮件地址在数据库与此代码在PHP中,但当我有该电子邮件地址在数据库中它说"不存在"!这个代码有什么问题?

$con=mysql_connect("localhost","gvf_lingo","btrbghtre544","vtrbt_lingo");
        $result = mysql_query("SELECT user_email FROM rating WHERE user_email='testuser@gmail.com'");
        $num_rows = mysql_num_rows($result);
      if ($num_rows > 0) {
        echo "exist";
      }else{
        echo "does not exist";
      }

您正在提供错误的连接字符串。应该是什么,

$con = mysql_connect("localhost","gvf_lingo","btrbghtre544");
mysql_select_db("vtrbt_lingo",$con);

please estand 在注释中提到mysqli接受数据库名称作为第四个参数

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db'); 

注意:请不要在新代码中使用mysql_*函数。它们不再被维护,并被正式弃用。见红框?学习预处理语句,并使用PDO或MySQLi——本文将帮助您决定使用哪一种。如果你选择PDO,这里有一个很好的教程。

mysql_connect需要3个参数,您在这里传递了4个参数,并且您忘记在这里选择数据库,您在下面为这个

编写代码
$con=mysql_connect("localhost","gvf_lingo","btrbghtre544");
mysql_select_db("vtrbt_lingo",$con);

你也可以像这样使用mysqli

$mysqli = new mysqli('localhost', 'your_username', 'your_password', 'your_db');