I’ve been trying to make a dynamic class for my mysqli queries because I’m pretty sick of typing them in all the time. I’ve got far enough to make it throw no errors until trying to parse the returned data on the page. The above error is throwing when I try to output my query on the page. I’m leaving my old code in there to try and show what I was doing.
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;
}
}
}
?>
Again, I’m not getting any errors at least loading home.php except the error in the title of this question. If I remove the foreach then it just doesn’t return anything at all, but no errors. If I forget something, please, let me know kindly. I and my son have been pretty sick and I’m really tired.
Your foreach() does not work because either there is no matching data, or your query is malformed somehow.
In the case of the latter, build your sql statement into a var and echo it out onto the screen as a form of debug: eg:
Paste that into your db and see, does it look well formed, does it create errors, is there matching data? (a positive result?) look closely at the error messages.