mysqliクエリを入力するのにかなりうんざりしているので、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();
*/
?>
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を削除すると、何も返されませんが、エラーは返されません。何か忘れてしまったら、教えてください。私と私の息子はかなり病気で、私は本当に疲れています。