Mysqli a little bit about the problem, let's take a look

Source: Internet
Author: User
Mysqli a bit of a problem, let's see.
I wrote a sqlhelper.class.php tool class, he is mainly instantiated after the database can be directly queried. Then I use mysqli written, out of the wrong, do not know how to solve, see the code:

 
This is a tool class, he is mainly to complete the direct operation of MySQL

Class sqlhelper{


Public $hosts;
public $username;
Public $password;
Public $database;


The constructor is basically the initialization of the object that completes the Mysqli
Public function __construc ($a, $b, $c, $d) {

$mysqli =new mysqli ($a, $b, $c, $d);
if ($mysqli->connect_error) {
Die ("Database connection Error! ". $mysqli->connect_error);
}

$mysqli->query ("Set names UTF8") or Die ("database set character Set error! ");

}


Here is a query method, you send me an SQL statement, I return you a two-dimensional array
Public Function Dql ($mysql) {

$res = $mysqli->query ($mysql) or Die ("Database query Error! //DQL Query statement here Returns a result set, we process the result set as a two-dimensional array, freeing the result set memory
$resarr =array ();

while ($row = $res->fetch_row ()) {//Here is the result set self-disassembly, and each time we split it into an array of arrays, we put it in a two-dimensional array

$resarr []= $row;

}

If the code goes here, indicating that the two-dimensional array has been exhausted, we immediately release the result set memory and return the two-dimensional array
$res->free ();
return $resarr;

}


Close Resource
Public Function Closeresource () {

$mysqli->close ();

}



}

?>



After instantiating an object, it is primarily a hint that the $mysqli inside the constructor is undefined, and then he is not an object, then the code behind cannot go.

notice:undefined variable:mysqli in D:\web\notebook\model\sql.php on line 30

Fatal Error:call to a member function query () on a non-object in D:\web\notebook\model\sql.php on line 30

Share to:


------Solution--------------------
Change $mysqli to $this->mysqli
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.