Hi guys, i have a problem

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

adrianst
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Apr 25, 2013 8:45 am

Hi guys, i have a problem

Postby adrianst » Tue May 14, 2013 7:24 am

I use mysqli with my script. I have a class:

<?php
/**
* The MySQL Improved driver extends the Database_Library to provide
* interaction with a MySQL database
*/
class MysqlImproved_Driver extends Database_Library
{
/**
* Connection holds MySQLi resource
*/
private $connection;

/**
* Query to perform
*/
private $query;

/**
* Result holds data retrieved from server
*/
private $result;

/**
* Create new connection to database
*/
public function connect()
{
//connection parameters
$host = 'localhost';
$user = 'root';
$password = 'r3v3l@t104';
$database = 'my_db';

//your implementation may require these...
$port = NULL;
$socket = NULL;

//create new mysqli connection
$this->connection = new mysqli
(
$host , $user , $password , $database , $port , $socket
);

return TRUE;
}

/**
* Break connection to database
*/
public function disconnect()
{
//clean up connection!
$this->connection->close();

return TRUE;
}

/**
* Prepare query to execute
*
* @param $query
*/
public function prepare($query)
{
//store query in query variable
$this->query = $query;

return TRUE;
}

/**
* Sanitize data to be used in a query
*
* @param $data
*/
public function escape($data)
{
return $this->connection->real_escape_string($data);
}

/**
* Execute a prepared query
*/
public function query()
{
if (isset($this->query))
{
//execute prepared query and store in result variable
$this->result = $this->connection->query($this->query);

return TRUE;
}

return FALSE;
}
public function update(){
if(isset($this->result)){
return $this->connection->affected_rows;
}
return FALSE;
}

public function insert(){
if(isset($this->result)){
return $this->connection->insert_id;
}
return FALSE;
}

public function delete(){
if(isset($this->result)){
return $this->connection->affected_rows;
}
return FALSE;
}
/**
* Fetch a row from the query result
*
* @param $type
*/
public function fetch($type = 'object')
{
if (isset($this->result))
{
switch ($type)
{
case 'array':

//fetch a row as array
$row = $this->result->fetch_array();

break;

case 'object':

//fall through...

default:

//fetch a row as object
$row = $this->result->fetch_object();

break;
}

return $row;
}

return FALSE;
}
}

The functions update() and delete() return -1, regardless of the query, and function insert() don't work. How would these three functions have posted better be harmonized with the class?

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Hi guys, i have a problem

Postby johnj » Tue May 14, 2013 3:59 pm

update and delete is returning false, which is -1. You need to check why the "false" condition is getting executed.

isset($this->result)).....you need to check if this condition has a proper value in insert, delete and update.


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 3 guests