php - SQL 查询在 PHP 中返回 false

标签 php mysql

我试图在 PHP 中执行此查询,但它一直返回 false。我已经在 phpMyAdmin 中尝试了查询,它工作正常,所以如果有人能发现问题所在,那就太好了。另外,对于此类问题,我如何才能获得更好的错误消息,以便尝试解决问题?

$stmt = $conn->prepare("SELECT * FROM artist WHERE artID != ?;");
if(!$stmt)
{
    echo "Error creating SQL statement";
    return 1;
}

我已经在同一 PHP 代码块中使用了 $stmt = $conn->prepare(query); 进行不同的查询,它运行良好,所以我不知道这是否是什么用它来做。

提前致谢:)

编辑:有人问我在哪里绑定(bind)“?”在查询中使用。 $stmt->bind_param('i', $albumArtID); 我最初没有将它包含在问题中,因为 if 中的 echo > 语句运行,所以我假设它在 bind_param 之前遇到错误。

编辑 2:这里要求的是用于建立连接的代码:

<?php
error_reporting(-1);
ini_set('display_errors', 'On');

$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'psyjb6';

$conn = new mysqli('localhost', 'root', '', 'psyjb6');
if ($conn->connect_errno)
    echo"<p>failed to connect to database</p>";
?>

编辑 3:这是该页面的整个主要代码部分,希望我们能解决这个问题:

<form name="editAlbum" method="get" onsubmit="return validateForm(this)">

                        <div class="row">
                            <?php
                            error_reporting(E_ALL);
                            ini_set('display_errors', 1);
                            include 'connection.php';

                            if(isset($_GET["album"]))
                            {
                                /* If album was passed in the URL then get current values
                                    for that album */
                                $stmt = $conn->prepare("SELECT cd.artID, artName, cdTitle, cdPrice, cdGenre, cdTracks FROM cd INNER JOIN artist ON (cd.artID = artist.artID AND cdID = ?);");
                                if(!$stmt)
                                {
                                    echo "Error creating SQL statement";
                                    exit;
                                }

                                $albumID = htmlspecialchars($_GET["album"]);

                                $stmt->bind_param('i', $albumID);
                                $stmt->execute();

                                $stmt->bind_result($albumArtID, $albumArtName, $albumTitle,
                                                   $albumPrice, $albumGenre, $numTracks);

                                $stmt->fetch();

                                /* Create input fields */
                                // Album Title
                                echo "<div class=\"row horizontal-center\">" . 
                                    "<input type=\"text\" value=\"" . htmlspecialchars($albumTitle) . "\" name=\"albumTitle\"/>" .
                                    "</div>";

                                // Artist Name
                                echo "<div class=\"row horizontal-center\">" .
                                        "<h6>By Artist:</h6>" .
                                        "</div>";

                                echo "<div class=\"row horizontal-center\">" .
                                        "<select name=\"artID\">";

                                /* Create option for current artist so it will be first in list */
                                echo "<option value=\"$albumArtID\">$albumArtName</option>\n";

                                /* Generate list of artists except artist currently associated with the album */
                                $stmt = $conn->prepare("SELECT * FROM artist WHERE artID != ?");
                                if($stmt === false)
                                {
                                    echo $conn->error;
                                    echo "hi";
                                    exit;
                                }

                                $stmt->bind_param('i', $albumArtID);
                                $stmt->execute();

                                $stmt->bind_result($artID, $artName);

                                /* Check if no artists were found */
                                if(!$stmt->fetch())
                                    echo "<p>No artists were found!</p>";
                                else
                                {
                                    /* Create options for artists that were found */
                                    do
                                    {
                                        echo "<option value=\"$artID\">$artName</option>\n";
                                    }while($stmt->fetch());
                                }

                                echo "</select>" .
                                    "</div>";

                                // Album Price
                                echo "<div class=\"row horizontal-center\">" .
                                        "<input type=\"number\" step=\"0.01\" value=\"" . htmlspecialchars($albumPrice) . "\" name=\"albumPrice\"/>" .
                                    "</div>";

                                // Album Genre
                                echo "<div class=\"row horizontal-center\">" . 
                                        "<input type=\"text\" value=\"" . htmlspecialchars($albumGenre) . "\" name=\"albumGenre\"/>" .
                                    "</div>";

                                // Number of Tracks
                                echo "<div class=\"row horizontal-center\">" . 
                                        "<input type=\"number\" value=\"" . htmlspecialchars($numTracks) . "\" name=\"numTracks\"\n/>" .
                                    "</div>";

                                // Delete checkbox
                                echo "<div class=\"row\">" .
                                        "<div class=\"col-2\">" .
                                            "<h6>Delete:</h6>" .
                                        "</div>" .
                                        "<div class=\"col-1\">" .
                                            "<input type=\"checkbox\" name=\"delete\" value=\"Delete\"/>" .
                                        "</div>" .
                                    "</div>";

                                /* Create hidden field to submit the album ID with the form */
                                echo "<input type=\"hidden\" value=\"" . htmlspecialchars($albumID) . "\" name=\"albumID\"\n/>";
                            }
                            else
                            {
                                /* Send browser back to artists page if they somehow accessed
                                    the edit page without going through the "Edit" link next
                                    to an artist in the table. This would be the artName variable
                                    would not be sent via the URL.*/
                                header("Location: artists.php");
                            }
                            ?>
                        </div>

                        <div class="row">
                            <div class="col-2">
                                <h6>Delete:</h6>
                            </div>
                            <div class="col-1">
                                <input type="checkbox" name="delete" value="Delete"/>
                            </div>
                        </div>
                        <div class="row">
                            <input type="submit" name="submit" value="Update"/>
                        </div>      

                        <!-- PHP to edit album data -->
                        <?php
                        include 'connection.php';                                               

                        if(isset($_GET["delete"]))
                        {
                            $albumID = $_GET["albumID"];

                            /* Create DELETE query */
                            $stmt = $conn->prepare("DELETE FROM cd WHERE cdID = ?;");
                            if(!$stmt)
                            {
                                echo "Error creating SQL statement";
                                exit;
                            }

                            $stmt->bind_param('i', $albumID);

                            $stmt->execute();
                        }
                        else if(isset($_GET["albumTitle"]) && isset($_GET["albumGenre"])
                            && isset($_GET["albumPrice"]) && isset($_GET["numTracks"]))
                        {
                            $albumTitle = htmlspecialchars($_GET["albumTitle"]);
                            $artID = htmlspecialchars($_GET["artID"]);
                            $albumGenre = htmlspecialchars($_GET["albumGenre"]);
                            $albumPrice = htmlspecialchars($_GET["albumPrice"]);
                            $numTracks = htmlspecialchars($_GET["numTracks"]);

                            /* Create INSERT query */
                            $stmt = $conn->prepare("UPDATE cd SET (cdTitle = ?, artID = ?,
                                cdGenre = ?, cdPrice = ?, cdTracks = ?) WHERE cdID = ?;");
                            if(!$stmt)
                            {
                                echo "Error creating SQL statement";
                                exit;
                            }

                            $stmt->bind_param('sisdi', $albumTitle, $artID, $albumGenre,
                                                $albumPrice, $numTracks);

                            $stmt->execute();
                        }
                        ?>  

                    </form>

最佳答案

如果您使用参数化查询,则必须在执行准备好的查询时传递参数值。

您还必须执行 准备好的查询。 prepare只是将查询传递给数据库进行编译和优化,它并不实际执行查询。

此外,如果您在这些数据库访问语句中遇到错误,您应该使用一些函数/方法来显示实际的错误消息,这比输出您自己编写的内容(例如 echo "Error)有用得多创建 SQL 语句";

; 也不是必需的。

$stmt = $conn->prepare("SELECT * FROM artist WHERE artID != ?");
if ( $stmt === false ){
    echo $conn->error;
    exit;
}


$stmt->bindParam('i', $some_variable)

$result = $stmt->execute();

if ( $result === false ) {
    echo $stmt->error;
    exit;
}

关于php - SQL 查询在 PHP 中返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36531569/

相关文章:

php - 如何验证是否为使用 PHP 从 MYSQL 查询中获得的每个单独值选中了复选框?

php - 正则表达式匹配 PHP 中字符串的前 3 个字符

php - Laravel Eloquent : how to select datas from a relation in a collection or change their names before being turned into JSON?

php - MySQL中Select语句的默认上限是多少?

php - 在 PHP 中使用准备好的语句时发生 fatal error

php - 阻止循环脚本在中止页面时停止?

php - 如何在 CakePHP 3 中保存之前格式化日期字段?

mysql - 错误 1005 : Can't create table can't find where the issue is

MySQL自动增量列-删除记录后无法重新使用数字

mysql - 声明在 MySql 错误中创建触发器的变量