[使用准备好的语句时为SQLi NO DATA(NULL)

问题描述 投票:0回答:1

[嗨,我正在为sqli准备好的选择而苦苦挣扎。 (我习惯于使用pdo)。

SQL:

$sql = "SELECT id, ext FROM `images` WHERE id = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param("s", $uri);
$stmt->execute();

$result = $stmt->get_result();

$image = $result->fetch_assoc();


print_r($image); // NULL
die;

结果:NULL

存在证明数据:enter image description here

调试错误和代码显示在下面的屏幕快照中。请确定是否需要更多信息information

enter image description here

我一直在尝试各种不同的sqli函数,但是我无法正常工作

证明我正在传递数据(Strlen):enter image description here

亲切的问候

杰克

----------------------------------

以旧尝试编辑:

$stmt = mysqli_prepare($db, 'SELECT id, ext FROM `images` WHERE id = ?');

mysqli_stmt_bind_param($stmt, 's', $uri);
mysqli_stmt_execute($stmt);
++$db_queries;
mysqli_stmt_bind_result($stmt, $img_id, $ext);
mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);


var_dump($img_id);
echo '<br />';
var_dump($ext);

die;

结果:

enter image description here

php sql mysqli
1个回答
0
投票

您的代码没有问题,请重新填充您的代码并运行小型测试。这些都可以在我的PC上找到。请再次检查您的数据库连接,或尝试trim($uri)。也许可以帮忙。

PHP

<?php
$db = new mysqli('localhost','root','','test');
if ($db->connect_error) {
    exit("Error {$db->connect_errno} : {$db->connect_error}");
}

if (isset($_POST['id'])) {
    // sample post data from FORM
    $uri = $_POST['id'];

    // query Statement
    $sql = "SELECT id, ext FROM `images` WHERE id=?";
    $stmt = $db->prepare($sql);
    $stmt->bind_param("s", $uri);
    $stmt->execute();

    // generate result
    $result = $stmt->get_result();
    $image = $result->fetch_assoc();

    // output data
    echo "<pre>";
    print_r ($image);
    echo "</pre>";
    echo '<a href="index.php">back</a>';
} else {
    exit("No direct script access allowed");
}
?>

HTML表单

<!-- JUST SIMPLE FORM TEST -->
<form action="test.php" method="POST">
    <p><input type="text" name="id" required="" placeholder="put id here"></p>
    <p><button type="submit">RUN</button></p>
</form>

然后结果

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.