1

I am using PHP 7 and the latest mysql on ubuntu 16.10. This is the current code I have

<?php
        $dbhost = 'localhost:3036';
        $dbuser = 'root';
        $dbpass = 'password';
        $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    
        $sql = "DELETE FROM databasetable WHERE columnA LIKE '%Test7%'" ;
        mysql_select_db('jdbdev');
        $retval = mysql_query( $sql, $conn );
        
        if(! $retval ) {
           die('Could not delete data: ' . mysql_error());
        }
        
        echo "Deleted data successfully\n";
        
        mysql_close($conn);

?>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Brigitte
  • 79
  • 2
  • 9

6 Answers6

0

Should change to below query

DELETE FROM databasetable WHERE columnA like '%Test7%'

There is difference between = and like operator

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ravi
  • 30,829
  • 42
  • 119
  • 173
0

Assuming you're trying to delete all the rows where columnA contains the test "Test7", you need to use the like operator, not the = operator:

DELETE FROM databasetable WHERE columnA LIKE '%Test7%'
-- Here --------------------------------^
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

If you want to use wildcard % then you have to use LIKE instead of =, so change your sql to:

"DELETE FROM databasetable WHERE columnA LIKE '%Test7%'"

Also, mysql functions are vulnerable to sql injection, you should use mysqli or even better PDO to manage your database.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Željko Krnjić
  • 2,356
  • 2
  • 17
  • 24
0
$sql = "DELETE FROM databasetable WHERE columnA = '%Test7%'" ;

should be columnA like '%test7%'

$sql = "DELETE FROM databasetable WHERE columnA like '%test7%'

Like and = both are different.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Shashank Shah
  • 2,077
  • 4
  • 22
  • 46
0

Try:

<?php
        $dbhost = 'localhost';
        $dbuser = 'root';
        $dbpass = 'password';
        $db_name = 'jdbdev';
        $conn = mysqli($dbhost, $dbuser, $dbpass, $db_name);

        try{
         if($conn->connect_error){
          echo 'Could not establish connection to database because '.$conn->connect_error;
            }
        }catch(Exception $ex){
          echo $ex->getMessage();
          exit;
          }
           
         $sql =  "DELETE FROM databasetable WHERE databasetable.columnA LIKE '%Test7%'";
        $retval = $conn->query($sql);

        if($retval === FALSE) {
           die('Could not delete data: ' . mysql_error());
        }

        echo "Deleted data successfully\n";

        mysql_close($conn);

?>
  

Sometimes, you have to reference the database table in front of the column

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rotimi
  • 4,783
  • 4
  • 18
  • 27
0

It's not very clear for me what you are trying to accomplish using this:

WHERE columnA = '%Test7%'" ;

Is the percent sign a literal that should match just like that? As you probably know, percentage sign is a wildcard in mysql and it should be use with LIKE rather than equal.

In this case, your condition will be:

WHERE columnA LIKE '%Test7%'`
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alex
  • 187
  • 4