为第59行errno 1064上的/home/content/20/9628620/html/home.php中的foreach()提供的参数无效

为第59行errno 1064上的/home/content/20/9628620/html/home.php中的foreach()提供的参数无效,php,oop,mysqli,prepared-statement,Php,Oop,Mysqli,Prepared Statement,我一直在尝试为我的mysqli查询创建一个动态类,因为我已经厌倦了一直输入它们。在尝试解析页面上返回的数据之前,我已经足够让它不会抛出错误。当我试图在页面上输出我的查询时,上面的错误被抛出。我将我的旧代码留在那里,试图展示我在做什么 home.php <?php require('config/dbconfig.php'); include('classes/mysqli.class.php');

我一直在尝试为我的mysqli查询创建一个动态类,因为我已经厌倦了一直输入它们。在尝试解析页面上返回的数据之前,我已经足够让它不会抛出错误。当我试图在页面上输出我的查询时,上面的错误被抛出。我将我的旧代码留在那里,试图展示我在做什么

home.php

<?php
                require('config/dbconfig.php');
                include('classes/mysqli.class.php');
                $mysqli = new DATABASE($db_host,$db_user,$db_pass,$db_name);
                $results = $mysqli->select('id,title,category,desc,postdate,author','news',null,null,'orderByLimit','id','DESC',4);
                /*
                $query = "SELECT * FROM news ORDER BY id DESC LIMIT 4";
                if ($stmt = $mysqli->prepare($query)) {
                    // execute statement
                    $stmt->execute();

                    // bind result variables
                    $stmt->bind_result($idn, $titlen, $categoryn, $descn, $postdaten, $authorn);

                    // fetch values
                    while ($stmt->fetch()) {
                    */
                        //echo 'id: '. $id .' title: '. $title;
                        echo "<table border='0'>";
                        foreach($results as $row)
                        {

                        $shortDescLengthn = strlen($row['desc']);
                        if ($shortDescLengthn > 106) {
                            $sDCutn = 106 - $shortDescLengthn;
                            $shortDescn = substr($row['desc'], 0, $sDCutn);
                        } else {
                            $shortDescn = $row['desc'];
                        }
                        echo "<h1>" . $row['title'] . "</h1>"; 
                        echo "<tr><td>$shortDescn...</td></tr>"; 
                        echo '<tr><td><a href="javascript:void(0);" onclick="' 
                        . 'readMore(' . $row['id'] . ',' . htmlspecialchars(json_encode($row['title'])) . ',' 
                        . htmlspecialchars(json_encode($row['category'])) . ',' 
                        . htmlspecialchars(json_encode($row['desc'])) . ',' . htmlspecialchars(json_encode($row['postdate'])) . ',' 
                        . htmlspecialchars(json_encode($row['author'])) . ')">Read More</a></td></tr>'; 
                        echo "<tr><td>Written by: " . $row['author'] . "</td></tr>"; 
                        echo '<tr><td><img src="images/hardcore-games-newsbar-border.png" width="468px" /></td></tr>'; 
                    }
                    echo "</table><br />";

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

                //close connection
                $mysqli->close();
                */
            ?>
您的foreach()无法工作,原因可能是没有匹配的数据,或者您的查询格式不正确

在后一种情况下,将sql语句构建到一个var中,并将其作为调试的一种形式显示在屏幕上:例如:

$sql = $queryPre . " FROM " . $from . " " . $querySuff;

echo $sql;

将其粘贴到数据库中,然后查看,它是否格式良好,是否会产生错误,是否有匹配的数据?(阳性结果?)仔细查看错误消息。

在复制/粘贴代码之前,我没有看到foreachI hit undo中使用的
$results
变量的初始化,我想这就是我没有看到的。在调用mysqli->connect时添加。是否尝试转储$results的内容?
var\u dump($results)
output是什么?把它放在这里:$stmt->close();var_dump($结果);返回$results;这只是classes/mysqli.class.php上的一个空白页。在home.php中取出foreach并在调用后立即转储结果,我得到以下结果:string(22)“Invalid variable type!”,并且我通过添加一个检查来确定whereVal是否为null来修复该问题。它现在没有准备查询。正在获取以下内容:字符串(81)“按id描述从新闻顺序中选择id、标题、类别、描述、发布日期、作者限制4”。但是,如果我删除该调试,它将在准备查询时停止。这正是我在home.php中注释出来的有效查询,不包括被命名的字段,而只是*。很抱歉,我在queryPre、from和querySuff以及orderby值和limit值周围加了单引号,希望能有所帮助,但仍然没有任何帮助。我被难住了。是引用和我忘了在配置中使用一个新的mysqli类。谢谢我花了很长时间才抓住lol。
$sql = $queryPre . " FROM " . $from . " " . $querySuff;

echo $sql;