mysqli and pdo support in hook files

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
peebee
AppGini Super Hero
AppGini Super Hero
Posts: 356
Joined: 2013-03-21 04:37

mysqli and pdo support in hook files

Post by peebee » 2015-06-03 08:02

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.

User avatar
a.gneady
Site Admin
Posts: 1354
Joined: 2012-09-27 14:46
Contact:

Re: mysqli and pdo support in hook files

Post by a.gneady » 2015-06-04 10:10

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).
:idea: AppGini plugins to add more power to your apps:

Post Reply