Mysqli DELETE QUERY not working in PHP Script

后端 未结 2 1544
耶瑟儿~
耶瑟儿~ 2021-01-21 06:31

Im using the following code to remove an entry from a table what i want to do is to check if any value was deleted from the table.If one value is deleted,the script should print

相关标签:
2条回答
  • 2021-01-21 06:54

    How about using execption. I also changed the code a little bit.

    <?php
    $mysqli = new mysqli("SQLHOST.COM", "CLIENT", "PASSWORD", "DNAME", 1234);
    $connection = mysqli_connect('SQLHOST', 'CLIENT', 'PASSWORD') or die(mysqli_error());
    try {
        $select_db = mysqli_select_db('DBNAME', $connection);
        if (!$select_db) {
            throw new Exception("Could not connect!");
        }
    }
    catch (exception $e) {
        echo "Error (File: " . getFile() . ", line " . $e->getLine() . "): " . $e->
            getMessage();
    }
    $query = mysqli_query('DELETE FROM ktable WHERE code="' . $value . ';"');
        if ($query) {
    
            printf("Select returned %d rows.\n", $result->num_rows);
            printf($result->num_rows);
            mysqli_close();
        }
    ?>
    
    0 讨论(0)
  • 2021-01-21 07:00

    what you delete what you need to return is affected_rows http://www.php.net/manual/en/mysqli.affected-rows.php

    What you need to Replace

    if ($result = $mysqli->query("DELETE FROM ktable WHERE code='value'")) {
        printf("Select returned %d rows.\n", $result->num_rows);
    
    
        printf($result->num_rows);
        $result->close();
    }
    

    Working Code

    $value = ""; // Set To any Value
    $mysqli = new mysqli ( "SQLHOST.COM", "CLIENT", "PASSWORD", "DNAME", 1234 );
    if ($mysqli->connect_errno) {
        printf ( "Connect failed: %s\n", $mysqli->connect_error );
        exit ();
    } else {
        printf ( "cONN Sucees" );
        if ($mysqli->query (sprintf ( "DELETE FROM ktable WHERE code='%s'", mysqli_real_escape_string ( $mysqli, $value ) ) )) {
            printf ( "Affected Rows  %d rows.\n", $mysqli->affected_rows );
        }
    }
    

    You should have a working output

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