所以我试图创建一个UNION query,这样我可以从两个表(同一个DB)中获取数据,然后我试图用mysqli_bind_result()来提取数据,但是我得到一个错误:
mysqli_stmt_bind_result():绑定变量的数目与数字不匹配
这是我的代码:

$sql = mysqli_stmt_init($con);
if(mysqli_stmt_prepare($sql, "(SELECT `naslov`, `id_lang` FROM `naslovi` WHERE `id_vijesti` = ?) UNION ALL (SELECT `text`, `id_lang` FROM `tekstovi` WHERE `id_vijesti` = ?)")) {
    mysqli_stmt_bind_param($sql, "ii", $id, $id);
    mysqli_stmt_execute($sql);
    mysqli_stmt_bind_result($sql, $naslov, $id_lang1, $text, $id_lang2);
    $vijest = array();

    while(mysqli_stmt_fetch($sql)) {
        echo $naslov;
        if($id_lang1 == $id_lang2) {
            $vijest[] = array($naslov, $text, $id_lang1);
        }

    }

    mysqli_stmt_close($sql);

最佳答案

这个错误说明变量的数目不匹配。联合查询合并内部查询中的所有列。
我想您正在这里寻找一个连接查询。查看此页:http://www.sitepoint.com/understanding-sql-joins-mysql-database/
编辑:添加示例

"SELECT
    `naslovi`.`naslov`, `naslovi`.`id_lang`, `tekstovi`.`text`, `tekstovi`.`id_lang` AS tekstovi_id_lang
    FROM `naslovi` JOIN `tekstovi`
    ON `naslovi`.`id_vijesti` = `tekstovi`.`id_vijesti`
    WHERE `naslovi`.`id_vijesti` = ?"

关于php - PHP mysqli_stmt_bind_result与mysqli联合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30275624/

10-13 00:49