0

i want to check that is there any record with given email address in database with this code in PHP, but when i have that email address in the database it says "doesnt exist"! whats the wrong with this code?

$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";
      }
Fcoder
  • 9,066
  • 17
  • 63
  • 100
  • 2
    `or die(mysql_error());` – Hanky Panky Apr 30 '13 at 05:48
  • 3
    1. You haven't selected database with `mysql_select_db()`, so your query goes nowhere. 2. [`MySQL`](http://php.net/manual/en/book.mysql.php) (_mysql_*_ functions) extension is [***deprecated***](http://php.net/manual/en/function.mysql-connect.php). I suggest to use [`MySQLi`](http://php.net/manual/en/book.mysqli.php) (_mysqli_*_ functions) or [`PDO`](http://php.net/manual/en/book.pdo.php) instead. – BlitZ Apr 30 '13 at 05:48
  • 3
    In fact, `new mysqli(...)` or `mysqli_connect(...)` accepts the DB name as a fourth parameter, yet `mysql_connect()` doesn't. Yet another advantage, albeit a minor one, of mysqli over mysql. – PleaseStand Apr 30 '13 at 05:51

2 Answers2

3

You're giving incorrect connection string. Should be something,

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

Updated

As PleaseStand mention in comment mysqli accept DB name as fourth parameter.

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

Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
2

mysql_connect requires 3 parameter, you passed 4 here, also you forgot to select Database here, you below code for this

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

You can also use mysqli here like this

$mysqli = new mysqli('localhost', 'your_username', 'your_password', 'your_db'); 
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100