【问题标题】:MySQLi not returning results from databaseMySQLi没有从数据库返回结果
【发布时间】:2011-05-16 17:02:50
【问题描述】:

这是我的代码:

<?php

include('config.php');

$mysqli = new mysqli($db['host'], $db['user'], $db['pass'], $db['name']);
if ($mysqli->connect_error)
    throw new Exception('Couldn\'t connect to MySQL: ' . $mysqli->connect_error);

// Check for a ID parameter
if(isset($_GET['id']) && !empty($_GET['id']))
{
    $skinId = (int)$_GET['id'];

    // Build a query to get skin info from the database
    $stmt = $mysqli->prepare('SELECT name, description, author, timestamp, url FROM `skins` WHERE id=?');
    $stmt->bind_param('i', $skinId);

    // Execute query
    $stmt->execute();
    $stmt->bind_result($result['name'], $result['desc'], $result['auth'], $result['time'], $result['url']);

    echo $result['name'];
} else {
    // Show a 404 page
    echo "bad";
}

$mysqli->close();

?>

当它运行时,我没有得到任何结果。我可以验证$skinId 在我尝试时确实持有有效的皮肤 ID,并且它不是一个空变量。当我使用 phpMyAdmin 在 localhost 上运行相同的语句时,我得到了正确的行,其中包含查询中请求的所有信息。当我做print_r($result) 时,我得到了这个:

Array
(
    [name] => 
    [desc] => 
    [auth] => 
    [time] => 
    [url] => 
)

有人知道发生了什么吗?提前致谢。 :)

【问题讨论】:

  • 你确定id在数据库里吗?

标签: php sql select mysqli


【解决方案1】:

您应该在 $stmt->bind_result 命令之后执行 $stmt->fetch() 以便将数据从数据库获取到您的结果数组

【讨论】:

  • 该死!知道这会很简单。谢谢你:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多