简体   繁体   中英

Problem with PHP and MySQL (mysql_query)

i got a problem about an if condition. (mysql_query) I verify in my database and if the USER don't exists it does the else condition. And that is not what i want. I want it to do the error_type(...); --> this function is a manual function that i create myself. Which is:

else{
  var_dump($response);
  mysql_query("DELETE FROM info_compte WHERE username      ='$user'");

  echo "<pre>";
      echo "L'utilisateur <b>".$user."</b> a été supprimé avec succès!"; 
  echo "</pre>";
}

Here is my complete function: (for the error_type i do the require at the beginning of the file)

function db_delete($user, $site){

    $querycon = "SELECT username FROM info_compte WHERE username = '$user' AND siteWeb = '$site'";
    $response = mysql_query($querycon);

    if($response > 0)
        error_type(6, $user,"");
    else {
        var_dump($response);
        mysql_query("DELETE FROM info_compte WHERE username ='$user'");
        echo "<pre>";
        echo "L'utilisateur <b>".$user."</b> a été supprimé avec succès!"; 
        echo "</pre>";
    }

}//db_delete

Thanks to all for your suggestion

A query which returns 0 results, was successful, so your $response will not be in false state.

Try this:

$querycon = "SELECT username FROM info_compte WHERE username = '$user' AND siteWeb = '$site'";
$response = mysql_query($querycon)
if(mysql_num_rows($response) < 1){
...

mysql_num_rows returns the number of rows returned by the query, if this number is 0 than your query was successful but there were no results.

Consider this example: sql示例

In the first query select * from data where id = "notHere"; this query is successful however it returns 0 rows, whereas the second query select * from data LIMIT 1; is successful and returns 1 row.

Your problem is with this line:

if(!($response = mysql_query($querycon))){

What's happening is you're assigning $response to be equal to mysql_query , then negating it. As long as the assignment works, you will always end up with false.

Instead, do the assignment and compare to false in one go:

if($response = mysql_query($querycon) === false){

With this, you're saying 'assign $response to the value of mysql_query and compare to false.

如何使用php的mysql_affected_rows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM