簡體   English   中英

mysql查詢有時但並非總是有效

[英]mysql query works sometimes but not always

我正在構建一個供自己使用的簡單的“小”電影編目數據庫系統。 現在的問題是我有一個mySQL查詢,該查詢在大多數情況下都有效,但不是全部。 基本上,它通過使用第三方IMDB API來工作。 我用它來搜索並提取所需的值,這很好。 它顯示在我的預覽屏幕和所有內容上。 我遇到的問題是,盡管大多數電影都可以正常工作,但有些電影卻不能工作,我無法弄清原因。

例如,“戒指的團契”存儲很好,而“國王的歸來”根本不會通過查詢。 我找不到任何區別。

這是我的查詢:

    $query = "INSERT INTO movies
(title, year, releaseDate, actors, image, runtime, genre, director, rating, watchedDate, category, series, comments, owned, ownedFormat, seen, plot, favorite, uploadDate)
VALUES ('$title', '$year', '$releaseDate', '$actors', '$newImg', '$runtime', '$genre', '$director', '$rating', '$watchedDate', '$category', '$series', '$comments', '$owned', '$ownedFormat', '$seen', '$plot', '$favorite', '$curDate')";

    mysql_query($query) or die ('Error');

我不確定還需要提供什么。 電影中的某種差異似乎導致了錯誤,但我不知道。

謝謝!

** 編輯 ** *

所以我嘗試切換到mysqli。 這是我的新代碼:

    /* Create the prepared statement */
if ($stmt = $mysqli->prepare("INSERT INTO movies (title, year, releaseDate, actors, image, runtime, genre, director, rating, watchedDate, category, series, comments, owned, ownedFormat, seen, plot, favorite, uploadDate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {

    /* Bind our params */
    $stmt->bind_param('sssssssssssssssssis', $title, $year, $releaseDate, $actors, $newImg, $runtime, $genre, $director, $rating, $watchedDate, $category, $series, $comments, $owned, $ownedFormat, $seen, $plot, $favorite, $curDate);

    /* Execute the prepared Statement */
    $stmt->execute();

    /* Echo results */
    echo "Inserted {$title} into database\n";
}

但是,現在我收到一條錯誤消息:致命錯誤:在if語句開始的行的非對象上調用成員函數prepare()。

我假設這是因為我的查詢不是對象嗎?

謝謝

造成這種情況的最可能原因是您將原始值放在單引號之間。 如果其中一個值中有單引號,則將出現語法錯誤。

綁定參數是執行所需操作的更好方法。 您可以在此處閱讀更多有關此內容的信息

沒有關於實際返回值的更多信息,這很難說。 返回的值可能包含一個破壞您代碼的字符,例如分號或引號。 嘗試使用正則表達式刪除所有非字母數字字符。

$result = preg_replace("/[^a-zA-Z0-9]+/", "", $s);

而且您有一個SQL注入漏洞。 考慮使用PDO代替不推薦使用的mysql函數。

http://php.net/manual/zh/book.pdo.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM