mysql_insert_id not working across classes
Posted: Tue Aug 18, 2009 11:54 am
i am using a third party class to handle database access, and when trying to use mysql_insert_id to obtain the last auto_increment value it always returns a zero - here are the .inc files i am using:
the latter of the three is the code i have written to utilise the other scripts - the database table i am using (table) consists of two fields: id(int, auto_increment, primary key) and data(varchar)
can anybody shed light on why mysql_insert_id() does not appear to work as it should?
Code: Select all
/**
* Constants.inc
*/
/**
* Database Constants - these constants are required
* in order for there to be a successful connection
* to the MySQL database. Make sure the information is
* correct.
*/
define("DB_SERVER", "localhost");
define("DB_USER", "username");
define("DB_PASS", "password");
define("DB_NAME", "database");Code: Select all
/**
* Database.inc
*/
include("constants.inc");
class MySQLDB
{
var $connection; //The MySQL database connection
/* Class constructor */
function MySQLDB(){
/* Make connection to database */
$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());
}
/**
* query - Performs the given query on the database and
* returns the result, which may be false, true or a
* resource identifier.
*/
function query($query){
return mysql_query($query, $this->connection);
}
};
/* Create database connection */
$database = new MySQLDB;Code: Select all
/**
* main.inc
*/
include ("database.inc");
class Main
{
function insertQuery($data){
global $database;
$q = "INSERT INTO table ('data') VALUES ('$data')";
$database->query($q);
return mysql_insert_id($database->connection);
}
};
/* Initialize main object */
$object = new Main;can anybody shed light on why mysql_insert_id() does not appear to work as it should?