MySQLI准备好的声明给了我奇怪的结果

时间:2014-07-23 15:46:30

标签: php mysqli

我刚刚发现了关于mysqli准备好的陈述,我试图自己做一个,我得到了一些奇怪的结果:

<?php

    $mysqli = new mysqli("localhost", "root", "password", "database");
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: " . $mysqli->connect_error;
    }

    $stmt = $mysqli->prepare("SELECT * FROM `media`");

    $stmt->execute();

    while ($row = $stmt->fetch()) {
        print_r($row);
    }

?>

这会返回111111111111我期待来自数据库的数据,我在print_r上做了$stmt并返回此信息:mysqli_stmt Object ( [affected_rows] => -1 [insert_id] => 0 [num_rows] => 0 [param_count] => 0 [field_count] => 8 [errno] => 0 [error] => [sqlstate] => 00000 [id] => 1 )

请帮助,我如何从我的数据库中获取数据而不是1吨?

更新

我尝试了以下内容:

<?php
$mysqli = new mysqli("localhost", "losanihomesweb", "8tsy2esk", "losanihomesweb");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if ($stmt = $mysqli->prepare("SELECT * FROM `in-the-press`")) {
    $stmt->execute();

    $stmt->bind_result($col1, $col2);

    while ($stmt->fetch()) {
        printf("%s %s\n", $col1, $col2);
    }

    $stmt->close();
}
$mysqli->close();

?>

我只是得到一个空白的白色屏幕:(我不明白bind_results是如何工作的,它是否在运行中受欢迎这些变量?

1 个答案:

答案 0 :(得分:2)

您未正确使用mysqli_stmt::fetch功能。这是你应该采取的方法:

$stmt->execute();

$result = $stmt->get_result();

while ($row = $result->fetch_array()) {
    print_r($row);
}