Mysqli DELETE QUERY 在 PHP 脚本中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10153255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:31:42  来源:igfitidea点击:

Mysqli DELETE QUERY not working in PHP Script

phpdatabasemysqli

提问by techno

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 success else false.This is what i have achieved till now.Please help

我使用以下代码从表中删除条目我想要做的是检查是否从表中删除了任何值。如果删除了一个值,脚本应该打印成功否则为 false。这就是我所实现的现在请帮忙

<?PHP
    $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 ($result = $mysqli->query("DELETE FROM ktable WHERE code='value'")) {
        printf("Select returned %d rows.\n", $result->num_rows);


     printf($result->num_rows);
        $result->close();
    }

    }
    ?>

回答by Baba

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

你删除你需要返回的是affected_rowshttp://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

你应该有一个工作输出

回答by johndavedecano

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();
    }
?>