开发者

Problem with PHP and MySQL (mysql_query)

开发者 https://www.devze.com 2023-02-14 12:14 出处:网络
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(...);

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'utilisateu开发者_开发技巧r <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:

Problem with PHP and MySQL (mysql_query)

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.


How about using php's mysql_affected_rows

0

精彩评论

暂无评论...
验证码 换一张
取 消