Problem with PHP and MySQL (mysql_query)

I have a problem with the condition if. (Mysql_query) I check in my database, and if it USERdoes not exist, this is an else condition. And that is not what I want. I want it to execute the function error_type(...);→ - this is the manual function that I create. What 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 error_typeI 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

Thank you all for your suggestion.

+3
source share
3 answers

A query that returns 0 results was successful, so yours $responsewill not be in a state false.

Try the following:

$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 , , 0, , .

: sql example

select * from data where id = "notHere"; , 0 , select * from data LIMIT 1; 1 .

+2

:

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

, $response mysql_query, . , false.

false :

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

" $response mysql_query .

0

How about using php mysql_affected_rows

0
source

Source: https://habr.com/ru/post/1795333/


All Articles