-1

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

主页.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();
                */
            ?>

mysqli.class.php

<?php
class DATABASE
{
private $db_host;
private $db_user;
private $db_pass;
private $db_name;
private $connection;
private $paramaters = array();
private $results = array();
private $numrows;

public function __construct($db_host, $db_user, $db_pass, $db_name)
{
    $this->host = $db_host;
    $this->user = $db_user;
    $this->pass = $db_pass;
    $this->name = $db_name;
    $this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->name) or die('There was a problem connecting to the database!');
}
public function __destruct()
{
    $mysqli->close();
}
public function select($fields, $from, $where, $whereVal, $type, $orderByVal, $ASDESC, $limitVal)
{
    if ($whereVal != null)
    {
        if (is_int($whereVal))
        {
            $bindVal = 'i';
        } elseif (is_string($whereVal)){
            $bindVal = 's';
        } else {
            return 'Invalid variable type!';
        }
    }       if (count($whereVal) > 1)
    {
        $y = array();
        foreach($whereVal as $type => $brand)
        {
            $y[$type] = $brand;
        }
    } else 
    {
        $y = $bindVal;
    }
    switch($type)
    {
        case "regular":
            $queryPre = "SELECT " . $fields;
            $querySuff = " WHERE " . $where . " =  ?";
            break;
        case "orderByLimit":
            $queryPre = "SELECT " . $fields;
            $querySuff = " ORDER BY " . $orderByVal . " " . $ASDESC . " LIMIT " . $limitVal;
            break;
    }
    $stmt = $this->mysqli->prepare($queryPre . " FROM " . $from . " " . $querySuff) or die('There was a problem preparing the Query!');

    //bind parameters for markers
    if($y&&$queryPre&&$querySuff)
    { 
        $bind_names[] = $y; 
        for ($i=0; $i<count($whereVal);$i++)  
        { 
            $bind_name = 'bind' . $i; 
            $$bind_name = $whereVal[$i]; 
            $bind_names[] = &$$bind_name; 
        } 
        $return = call_user_func_array(array($stmt,'bind_param'),$bind_names); 
    }
    $stmt->execute();
    $meta = $stmt->result_metadata();

    while ($field = $meta->fetch_field())
    {
        $parameters[] = &$row[$field->name];
    }
    call_user_func_array(array($stmt, 'bind_result'), $parameters);

    while ($stmt->fetch()) 
    {
        $x = array();
        foreach($row as $key => $val)
        {
            $x[$key] = $val;
        }
        $results[] = $x;
    }
    $stmt->close();
    return $results;
}
/*
 * Function Insert
 * @param into
 * @param values
 * @returns boolean
*/
public function insert($into, $values)
{
    $query = "INSERT INTO " . $into . " VALUES(" . $values . ")";

    $this->last_query = $query;

    if($this->mysqli->query($query))
    {
        return true;
    } else {
        return false;
    }

}

/*
 * Function Delete
 * @param from
 * @param where
 * @returns boolean
*/
public function delete($from, $where)
{
    $query = "DELETE FROM " . $from . " WHERE " . $where;

    $this->last_query = $query;

    if($this->mysqli->query($query))
    {
        return true;
    } else {
        return false;
    }
}
}
?>

同样,我至少在加载 home.php 时没有收到任何错误,除了这个问题的标题中的错误。如果我删除 foreach 那么它根本不会返回任何东西,但没有错误。如果我忘记了什么,请告诉我。我和我儿子病得很重,我真的很累。

4

1 回答 1

1

您的 foreach() 不起作用,因为没有匹配的数据,或者您的查询格式不正确。

在后者的情况下,将您的 sql 语句构建为 var 并将其作为调试形式回显到屏幕上:例如:

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

echo $sql;

将其粘贴到您的数据库中,看看它看起来是否格式正确,是否会产生错误,是否有匹配的数据?(一个积极的结果?)仔细查看错误消息。

于 2012-08-07T01:06:20.930 回答