c1t1z3n Posted August 18, 2009 Share Posted August 18, 2009 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: /** * 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"); /** * 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; /** * 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; 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? Quote Link to comment https://forums.phpfreaks.com/topic/170835-solved-mysql_insert_id-not-working-across-classes/ Share on other sites More sharing options...
c1t1z3n Posted August 19, 2009 Author Share Posted August 19, 2009 i've fixed the problem by changing the query syntax from: $q = "INSERT INTO table ('data') VALUES ('$data')"; to: $q = "INSERT INTO table (data) VALUES ('$data')"; the quotes around the field element were throwing mysql, so the initial query was not processed and as a result mysql_insert_id() was rendered useless - other than that my original code worked fine thereafter... Quote Link to comment https://forums.phpfreaks.com/topic/170835-solved-mysql_insert_id-not-working-across-classes/#findComment-901763 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.