fetch()正在关闭mysqli连接

时间:2015-08-28 17:36:38

标签: php mysqli

我的php中有一个问题,我需要一直包含config.php(mysqli php连接脚本)

<?php

$root = $_SERVER['DOCUMENT_ROOT']; 
include("$root/config.php"); // INCLUDE CONFIG

$stmt = $mysqli_link->prepare("SELECT id, categoria FROM categoria order by rand() limit 10");
$stmt->execute();
$stmt->bind_result($id1, $categoria);


include("$root/config.php"); // NEED TO INCLUDE CONFIG AGAIN TO AVOID - Fatal error: Call to a member function bind_param()


while($stmt->fetch()) {

$stmt2 = $mysqli_link->prepare("SELECT id, categoria, cover, fotos, titulo, descricao, data FROM posts where categoria = ? order by id desc limit 1");
$stmt2->bind_param('i', $id1);
$stmt2->execute();
$stmt2->bind_result($id, $categoria, $cover, $pictures, $titulo, $descricao, $data);
$stmt2->fetch();

    echo"<li class=liside><img src=/$picture class=row><a href=/category/$categorialink>$categoria</a></li>";
}

?>

我做错了什么?我需要先在$ stmt中选择类别,然后在$ stmt2里面选择类别,同时从post表格中获取该类别的内容。

的config.php

$hostname="localhost";
$titulo="config";
$user="root";
$pass="";
$bd="site";

$mysqli_link = new mysqli($hostname, $user, $pass, $bd);
$mysqli_link->set_charset("utf8");
ini_set('default_charset','utf8');

1 个答案:

答案 0 :(得分:1)

问题是你有2个查询同时运行而且MySQLi不喜欢这样。您的include("$root/config.php");&#34;已修复&#34;因为它为第二个查询打开了第二个MySQL连接。

这不建议,解决方案是你需要&#34;缓冲&#34;第一个查询的结果,以便MySQL可以运行其他(s)。 store_result()应解决此问题。

有关详细信息,请参阅此问题:Commands out of sync; you can't run this command now

另外,作为建议,您只需要prepare()一次查询。你可以重复使用它。

所以,试试这个:

<?php

$root = $_SERVER['DOCUMENT_ROOT']; 
include("$root/config.php"); // INCLUDE CONFIG

$stmt = $mysqli_link->prepare("SELECT id, categoria FROM categoria order by rand() limit 10");
$stmt->execute();

$stmt->bind_result($id1, $categoria);
$stmt->store_result();  // Added to buffer result set

// You can re-use the same prepared statment in a loop
$stmt2 = $mysqli_link->prepare("SELECT id, categoria, cover, fotos, titulo, descricao, data FROM posts where categoria = ? order by id desc limit 1");
$stmt2->bind_param('i', $id1);
$stmt2->bind_result($id, $categoria, $cover, $pictures, $titulo, $descricao, $data);

while($stmt->fetch()){
    $stmt2->execute();
    $stmt2->fetch();

    echo"<li class=liside><img src=/$picture class=row><a href=/category/$categorialink>$categoria</a></li>";
}

// You should close your statements when you're done with them
$stmt->free_result();
$stmt->close();
$stmt2->close();

?>

P.S。这里没有真正需要运行2(或更多)查询。只需一个就可以得到你想要的东西。你只需JOIN表。

注意:这可能不会100%有效,但它只是建议如何清理此代码并减少查询次数。

<?php

$root = $_SERVER['DOCUMENT_ROOT']; 
include("$root/config.php"); // INCLUDE CONFIG

$stmt = $mysqli_link->prepare("
    SELECT categoria.id, categoria.categoria,
        posts.id, posts.categoria, posts.cover, posts.fotos, posts.titulo, posts.descricao, posts.data
    FROM categoria
    JOIN posts ON posts.categoria = categoria.id
    GROUP BY categoria.id
    ORDER BY rand()
    LIMIT 10
");

$stmt->execute();
$stmt->bind_result($id1, $categoria, $id, $posts_categoria, $cover, $pictures, $titulo, $descricao, $data);

while($stmt->fetch()){
    echo"<li class=liside><img src=/$picture class=row><a href=/category/$categorialink>$categoria</a></li>";
}


$stmt->close();
?>