簡體   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