【问题标题】:mysqli fetch result as assoc arraymysqli 获取结果作为关联数组
【发布时间】:2016-05-27 12:24:34
【问题描述】:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

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

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5";

if ($stmt = $mysqli->prepare($query)) {

    /* execute statement */
    $stmt->execute();

    /* bind result variables */
    $stmt->bind_result($name, $code);

    /* fetch values */
    while ($stmt->fetch()) {
        printf ("%s (%s)\n", $name, $code);
    }

    /* close statement */
    $stmt->close();
}

/* close connection */
$mysqli->close();

有人可以帮我解决这个语句/语法吗?我希望结果为关联数组(由字段名索引)并且不想写这个“$stmt->bind_result($name, $code);”语句。谢谢

【问题讨论】:

  • 你只是混淆了PDOmysqli,看看PDO(忘记mysqli
  • @SZenc PDO 适用于 windows Sir.....没有适用于 linux 的驱动程序
  • 这根本不是真的,我一直在 Linux PC 和服务器上使用 PDO。见stackoverflow.com/questions/13375061
  • 听起来您还没有安装新的mysqlnd 驱动程序。见this post for help installing你用的是旧版本的PHP吗?
  • @RiggsFolly 我用的是最新版的 php Sir..

标签: php mysql


【解决方案1】:

由于您实际上没有使用任何参数,即?,因此您实际上不需要执行->prepare

所以你可以只使用->query()->fetch_assoc() 机制

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5";

$result = $mysqli->query($query)

if ( $result === FALSE ) {
    echo $mysqli->error;
    exit;
}

/* fetch values */
while ($row = $result->fetch_assoc()) {
   printf ("%s (%s)\n", $row['name'], $row['code']);
}

在您发表评论后

这是另一种选择,如果你必须保持准备

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5";

// there seems to be some confusion in the manual as to 
// whether this next statement is needed or not
// see if that helps

$stmt = $mysqli->stmt_init();

if ($stmt = $mysqli->prepare($query)) {   

    /* execute query */
    if (!$stmt->execute()) {
        echo $stmt->error;
        exit;
    }

    $result = $stmt->get_result();

    /* fetch values */
    while ($row = $result->fetch_assoc()) {
        printf ("%s (%s)\n", $row['name'], $row['code']);
    }

} else {
    echo 'Failed to prepare statement';
    exit;
}

$stmt->get_result() 的调用基本上将mysql_stmt 对象转换为mysql_result 对象

【讨论】:

  • 我需要使用 '->prepare' Sir Riggs..需要过滤我在这个例子中忘记的条件 stmt。我的坏...谢谢....顺便说一句,如何在注释中键入倾斜的代码......?
  • 使用我键盘上1 键左侧的反引号
  • ay..thanks...现在我明白了...Comments use mini-Markdown formatting: [link](http://example.com) _italic_ **bold** code. The post author will always be notified of your comment. To also notify a previous commenter, mention their user name: @peter or @PeterSmith will both work. Learn more… 这个框没有出现在我之前用来复制和粘贴符号的评论中..呵呵
  • 如果您需要保留准备,添加了另一个选项
  • 先生,我已经这样做了......get_result()fetch_assoc() 似乎未知......我在我的服务器上安装了 LAMP......
【解决方案2】:

试试这个。

    $stmt = $mySqli->query($query);
    $countries = mysqli_fetch_all($stmt, MYSQLI_ASSOC);
    $stmt->close();

【讨论】:

    【解决方案3】:

    试试这个:

    while ($stmt->fetch(PDO::FETCH_ASSOC)) {
        printf ("%s (%s)\n", $name, $code);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      相关资源
      最近更新 更多