Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php fetch()正在关闭mysqli连接_Php_Mysqli - Fatal编程技术网

Php fetch()正在关闭mysqli连接

Php fetch()正在关闭mysqli连接,php,mysqli,Php,Mysqli,我的php中有一个问题,需要始终包含config.php(mysqli php连接脚本) 问题是您有两个查询同时运行,MySQLi不喜欢这样。您的包括(“$root/config.php”)“修复”了它,因为它打开了第二个MySQL连接以运行第二个查询 不建议这样做,解决方案是您需要“缓冲”第一个查询的结果,以便MySQL可以运行其他查询。政府应该解决这个问题 有关更多信息,请参见此问题: 另外,作为建议,您只需要prepare()一次查询。你可以重复使用它 那么,试试这个: <?php

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


问题是您有两个查询同时运行,MySQLi不喜欢这样。您的
包括(“$root/config.php”)“修复”了它,因为它打开了第二个MySQL连接以运行第二个查询

不建议这样做,解决方案是您需要“缓冲”第一个查询的结果,以便MySQL可以运行其他查询。政府应该解决这个问题

有关更多信息,请参见此问题:

另外,作为建议,您只需要
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();

?>

@renatacosta:这与问题无关,但如果你只做一个查询,将分类和帖子连接起来,而不是循环,那会更好。
var\u dump($stmt2)
看看你得到了什么。可能
false
P.S.您不需要调用
$stmt2=$mysqli\u link->prepare(“选择…”)在循环中。您可以在循环外使用
$stmt2->bind_param
$stmt2->bind_result()。然后在循环中,您只需执行
获取
@RocketHazmat谢谢!谢谢各位朋友。答案很好!哦,这个很好用!唯一的问题是选择是获得相同的类别很多次…我认为小组的职位。分类解决这个问题!非常感谢。friend@renatacosta:是的,我对
JOIN
ed查询不是很确定。这与其说是一个正确的答案,不如说是一个建议。很高兴你让它工作了:-)
$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');
<?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();

?>
<?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();
?>