-4

What's wrong with this code?

  $query = "select * from user where email= '$email'";

if( mysql_num_rows( $query )==0 ){
    echo '<tr><td colspan="4">No Rows Returned</td></tr>';

  }else{
    while($row = mysql_fetch_assoc( $query ) ) {
        $id=$row['id'];
        echo"yolo";

}

I have the email in my db `but it returns

 No rows returned

Why?

Irina Farcau
  • 167
  • 10

4 Answers4

1

You have to execute the query. The argument to mysql_num_rows() and mysql_fetch_assoc() must be a result returned from mysql_query().

$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result)) ==0 ){
    echo '<tr><td colspan="4">No Rows Returned</td></tr>';

} else {
    while($row = mysql_fetch_assoc( $query ) ) {
        $id=$row['id'];
        echo"yolo";
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Warning

This extension was deprecated in PHP 5.5.0,enter code here and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

See also MySQL: choosing an API guide and related FAQ for more information.

Using msqli

 $conn = new mysqli($servername, $username, $password, $dbname);

     if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
     } 

     $sql = "select * from user where email= '$email'";
     $result = $conn->query($sql);

     if ($result->num_rows > 0) {

          while($row = $result->fetch_assoc()) {
              echo "<br> id: ". $row["id"]. " - Email: ". $row["email"] . "<br>";
          }
     } else {
          echo "0 results";
     }

To know More about mysqli

http://php.net/manual/en/book.mysqli.php

Pradeep
  • 9,667
  • 13
  • 27
  • 34
0

try this

  $query = "select * from user where email= '$email'";
  $query = mysql_query($query);
 if( mysql_num_rows( $query )==0 ){
 echo '<tr><td colspan="4">No Rows Returned</td></tr>';

 }else{
   while($row = mysql_fetch_assoc( $query ) ) {
    $id=$row['id'];
    echo"yolo";

 }
D Coder
  • 572
  • 4
  • 16
0

You have to write like this: $query = mysql_query("select * from user where email= '$email'");

And also Read: The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

Community
  • 1
  • 1
satya
  • 76
  • 10