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

前端 未结 10 1207
离开以前
离开以前 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 11:11

    It should give you the amount of registere users who have the email address that you provide as the parameter to this query. (Might be a check if the given email address is already registered for another user.) If the email address is not yet registered, an empty field will be returned. (That might be the reason why nothing gets printed out in your case. Try it with an email address that you are certain of to be in the database.)

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

    The count query will always return a value, which is 0 if no records are returned, or an integer above 0 if records match it.

    It should at least be printing out 0, the query you posted means:

    Get the number of records where the email address is equal to the session username

    This might not make sense, do you mean to do where username = ".$_SESSION["username"] or something similar?

    0 讨论(0)
  • 2021-02-03 11:19
    $resultemp = mysql_query("select count(*) AS count from registeredUsers where email='{$_SESSION['username']}'");
    
    // Verify mySQL Query Rresult
    
    if (!$resultemp) echo mysql_error();
    
    // Convert mySQL Result for PHP
    
    $counter=mysql_fetch_assoc($resultemp);
    $counter=$counter['count'];
    
    // Print Total Employees
    
    echo $counter;
    
    0 讨论(0)
  • 2021-02-03 11:25

    Try casting it to string before echoing it. As an int, 0 will display as an empty string.

    0 讨论(0)
提交回复
热议问题