Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8334139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:14:59+00:00 2026-06-09T03:14:59+00:00

I’ve been trying to make a dynamic class for my mysqli queries because I’m

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:15:01+00:00Added an answer on June 9, 2026 at 3:15 am

    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:

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

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.