I’m having problems with mysqli_query. I have methods set up in a singleton database class call connect() and querydb(), querydb takes a $query parameter which I’m sure is fine as I used the query to add data to my table in phpMyAdmin. here is my PHP method for when I attempt to add data to the table in my register.php class.
public function register() {
$databaseinst = database:: getinstance();
$conn = $databaseinst->connect();
$query = "INSERT INTO Customer (email_address,firstname,lastname,address_line_1,address_line_2,city,postcode,country,password) VALUES ('$this->email','$this->firstname','$this->secondname','$this->address1','$this->address2','$this->city','$this->postcode','$this->country','$this->password1')";
$numrows = $databaseinst->querydb($query);
if ( false===$numrows ) {
$this->errors[]= mysqli_error($conn);
}
if ($numrows < 1) {
$this->errors[] = "could not process query";
}
if($conn == 0)
{
$this->errors[] = "couldnt connect";
}
if($conn == 1)
{
$this->errors[] = " connected";
}
}
public function showerrors() {
echo "<h3> ERRORS!!</h3>";
echo "<p>" . var_dump($this->errors) . "</p>";
foreach ($this->errors as $key => $value)
{
echo $value . "</br>";
}
}
The second method is just how I print out the errors when they occur.
Next are the methods from my database singleton class which returns an instance fine I think the issue is with the mysqli_query() method call in my querydb() method just can figure out what it is I keep getting a “could not process query” reply. Anyway here is the singleton class I used which is pretty much an edited version of one I found online.
<?php
class database {
private static $instance;
private $dbcon;
private $result;
private $numrows;
private function __construct()
{
}
static function getinstance()
{
if(!self::$instance)
{
self::$instance = new self();
}
return self::$instance;
}
public function connect()
{
$this->dbcon = mysqli_connect('localhost','********','****');
if (!$this->dbcon)
{
return 0;
}
else
{
return 1;
}
}
public function querydb($query)
{
mysqli_select_db($this->dbcon,'theislan_testdatabase');
$this->result = mysqli_query($this->dbcon,$query);
$this->numrows = $this->result->num_rows;
if ($this->numrows < 1)
{
return 0;
}
else
{
return 1;
}
}
}
?>
Is the user you use to connect to MySQL allowed to INSERT? In other words, is it MySQL permission that cause the problem, not PHP? Try testing with root user to establish whether that’s the case (or check permissions in PHPMyAdmin).