Page 1 of 1

mysqli and pdo support in hook files

Posted: 2015-06-03 08:02
by peebee
I understand that Appgini 5.40 supports mysqli and pdo connections but I am having trouble updating an old mysql connection that I'd like to replace in my hooks files.

This mysql connection and query IS working in my __global.php hook file:

Code: Select all

mysql_connect("localhost", "username", "password") or die(mysql_error()); 
      	mysql_select_db("databasename") or die(mysql_error());      
      	mysql_query( "INSERT INTO etc, etc, etc...") or die(mysql_error());
but if I try to update the query to a mysqli connection with:

Code: Select all

$db = new mysqli('localhost', 'username', 'password', 'databasename'); 
	 if($db->connect_errno > 0){
    	die('Unable to connect to database [' . $db->connect_error . ']');
	}      
      	mysqli_query($db, "INSERT INTO etc, ec, etc...") or die('Unable to connect to database [' . $db->connect_error . ']');
I receive the error: Fatal error: Class 'mysqli' not found in /folder/hooks/__global.php

Can anyone help me with I'm dong wrong please? Am I missing something?

Even better still, I'd like to update to a PDO connection. Is that possible with Appgini 5.40 and assuming it is,what's required to get the connection working? Thanks.

Re: mysqli and pdo support in hook files

Posted: 2015-06-04 10:10
by a.gneady
You don't need to initialize the connection in __global.php ... you should use the AppGini-provided sql() function directly ... So, instead of this code:

Code: Select all

mysql_connect("localhost", "username", "password") or die(mysql_error());
         mysql_select_db("databasename") or die(mysql_error());     
         mysql_query( "INSERT INTO etc, etc, etc...") or die(mysql_error());
Just use:

Code: Select all

sql("INSERT INTO etc, etc, etc...", $eo);
The second parameter $eo above can be left empty (it's an options array provided for future use).