How to get the result of a select count(*) query in PHP?

前端 未结 10 1206
离开以前
离开以前 2021-02-03 10:49

I have this query to use in PHP:

mysql_query(\"select count(*) from registeredUsers where email=\".$_SESSION[\"username\"]);

When I use e

相关标签:
10条回答
  • 2021-02-03 10:59

    mysql_query returns a result resource. You can read the result with mysql_result

    $res = mysql_query("select count(*) from registeredUsers where email='".mysql_real_escape_string($_SESSION["username"])."'");
    echo mysql_result($res,0);
    
    0 讨论(0)
  • 2021-02-03 11:03

    You need to use mysql_fetch_array() to return value in a user defined variable. Then have to print the returned value.

     $result=mysql_query("select count(*) from registeredUsers where email='{$_SESSION['username']}'")
     $COUNT_NUMBER=mysql_fetch_array($result); 
     echo "<br>1.Count=" .$COUNT_NUMBER[0]; 
    
    0 讨论(0)
  • 2021-02-03 11:04

    Your code doesn't include any fetch statement. And as another answer notes, you need single quotes around $_SESSION["username"].

    $result = mysql_query("select count(*) from registeredUsers where email='{$_SESSION['username']}'");
    
    // Verify it worked
    if (!$result) echo mysql_error();
    
    $row = mysql_fetch_row($result);
    
    // Should show you an integer result.
    print_r($row);
    
    0 讨论(0)
  • 2021-02-03 11:04

    mysql_query() returns a resource used to get information from the result set. Use a function such as mysql_fetch_array() to retrieve rows from the result set. In this case, there will only be one row.

    0 讨论(0)
  • 2021-02-03 11:07

    You may want to echo out the query itself to determine that it is returning what you expect.

    0 讨论(0)
  • 2021-02-03 11:08

    You need single quotes around the session variable in your query

    $result = mysql_query("SELECT COUNT(*) 
                           FROM registeredUsers 
                           WHERE email = '".$_SESSION['username']."' ");
    
    0 讨论(0)
提交回复
热议问题