繁体   English   中英

如何检查mysql更新查询是否成功?

[英]How to check whether mysql update query is successful or not?

function insertNewBidPrice($code, $newBidPrice)
{
  global $conn;
  $sql = "update auctionitem set highestbid=$newBidPrice where code=$code";
  //echo $sql;
  if($conn->query($sql))
  {
    return 1;
  }
  else
  {
    require 0;
  }
}

我有这个php功能,尽管更新查询成功更新了表,但所有结果始终为0。

我使用PDO。

$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

首先使用prepare创建一个语句对象。

$stmt = $conn->prepare($sql);
$worked = $stmt->execute();
if ($worked == TRUE) {
    //I did stuff
} else {
    // nope
}
function insertNewBidPrice($code, $newBidPrice) {
    global $conn;
    // write your query
    $sql = "UPDATE auctionitem SET highestbid='$newBidPrice' WHERE code='$code'";

    // run the query
    $result = $conn->query($sql);

    // check the result of the query
    if ($result == true) {
        // it was a success
        return 1;
    } else {
        return 0;
    }
}

global $conn;
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "my_db";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} else {
    // it worked
}

在这里,您应该这样做。 我注意到您在if else语句中需要返回“ require 0”。 除此之外,我不确定您的问题,但我可以想到两种可能性。

  1. 您的$ conn变量无效。 检查它是否具有正确的凭据,请参阅$ conn-> connect_error行,它在我编写的代码中,也位于此处: http : //php.net/manual/en/mysqli.connect-error.php
  2. 查询可能不正确。 我个人用大写等格式化所有内容。 因为当我这样做时,文本编辑器会为它着色,所以我很容易看到是什么。 此外,SQL语句中的变量周围没有引号,如果它是整数,这很好,但是如果您不小心传递了一个字符串,它将失败。

还要检查您是否选择了正确的表格。 在您的SQL中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM