Location: PHPKode > scripts > ArrayQry > example.php
<?php
require('class.ArrayQry.php');

//Define DB Connection Info
$db_creds = array(
    'db'       => "test",
    'user'     => "uname",
    'pw'       => "mypass",
    'host'     => "localhost"
);

/*
////////////////////
//Example 1: Simple Select w optional debugging
//return results in 2-dim Assoc Array
////////////////////
$qry = new ArrayQry($db_creds, true);
$qry->debug();          //optional
$qry->set_qry("SELECT * FROM contacts");
$rows = $qry->execute('s');
print_r($rows);
*/

/*
////////////////////
//Example 2: Insert Record
//Inserts an Assoc Array into DB using the keys as column names and values as column data
////////////////////
$insert_info = array(
    'fname'       => "Tater",
    'lname'       => "Salad",
    'phone'       => "555-5555",
    'email'       => "hide@address.com"
);
$qry = new ArrayQry($db_creds, true);
$qry->debug();          	//optional
$qry->set_tbl("contacts"); //the table to insert into
$qry->set_update_array($insert_info);//the array of data
$qry->execute('i');
*/

/*
////////////////////
//Example 3: Update Record
//Inserts an Assoc Array into DB using the keys as column names and values as column data
////////////////////
$update_info = array(
    'phone'       => "222-2222",
);
$qry = new ArrayQry($db_creds, true);
$qry->set_tbl("contacts"); //the table to insert into
$qry->set_update_array($update_info);//the array of data
$qry->set_where("WHERE id='9'");
$qry->execute('u');
*/


/*
////////////////////
//Example 4: Delete Record
////////////////////
$qry = new ArrayQry($db_creds, true);
$qry->set_qry("DELETE FROM contacts WHERE id='12'");
$qry->execute('d');
*/

///*
////////////////////
//Example 5: Multiple Insert with single database connection
////////////////////
$insert_info = array(
    0 =>array(
        'fname'       => "Ham",
        'lname'       => "Burgler",
        'phone'       => "555-5555",
        'email'       => "hide@address.com"
    ),
    1 =>array(
        'fname'       => "Frank",
        'lname'       => "Enstein",
        'phone'       => "555-5555",
        'email'       => "hide@address.com"
    ),
    2 =>array(
        'fname'       => "Pete",
        'lname'       => "Repeat",
        'phone'       => "555-5555",
        'email'       => "hide@address.com"
    )
);

//This example shows how YOU CAN define your connection info outside the class, if desired. This is preferred when performing multiple querys for performance:
$conn_array = $db_creds;
$conn = mysql_connect($conn_array['host'], $conn_array['user'], $conn_array['pw'])or die("Unable to connect to MYSQL because: ".mysql_error());
$db_select = mysql_select_db($conn_array['db'],$conn) or die("Could not select '".$conn_array['db']."' database because: ".mysql_error());

$qry = new ArrayQry();//note that since db connection is already made, no arguments used in creating the object
$qry->set_tbl("contacts"); //the table to insert into
    foreach($insert_info as $arr){
        $qry->set_update_array($arr);//the array of data
        $qry->execute('i'); //which action to execute
    }

//If you open the db conection outside the class you should also close it  when finished, else it is not necessary if class is handling connection
mysql_close($conn);
//*/
?>
Return current item: ArrayQry