Downloads

 

Pkg: MySQL – End-User Documentation

This is a copy of the README.txt file which is included in the package. It will show you how to connect, query and retrieve results using the MySQL package.

< ?php
/**
 * Example of using the Mysql package
 *
 * Fully commented example of using the Mysql package to connect and query a
 * MySQL database.
 */

/**
 * First we load the Mysql Package
 */
require_once('mysql.php');
require_once('statement.php');
require_once('result.php');
require_once('exception.php');

/**
 * Setup db - edit values below to match your db server
 */
$db_user  = 'username';
$db_pword = 'password';
$db_host  = 'localhost';
$db_name  = 'test';
$db_table = 'table name';
/**
 * Create a new MySQL object
 */
$db_handle =& new Mysql($db_user, $db_pword, $db_host, $db_name);

/**
 * EXAMPLE OF USING Mysql TO QUERY DB
 */

/**
 * SQL Statement - Edit to whatever you like (use a SELECT statement) it makes
 * the rest of the script make sense
 */
$sql = 'SELECT * FROM '.$db_table.' LIMIT 10';

/**
 * If the SQL statement above is valid we will get back a Mysql_Result object.
 */
try {
    $result =& $db_handle->execute($sql);
}
catch (Mysql_Exception $e) {
    /**
     * If there is an error in the SQL we receive a Mysql_Exception object
     * so print the message returned.
     */
    echo $e->getMessage();
    exit;
}

/**
 * fetch all the results from the SQL statement and place into variable
 */
$rows = $result->fetchAll();

/**
 * fetch number of returned rows and print
 */
$num_rows = $result->fetchNumRows();
print_r($num_rows);

/**
 * EXAMPLE OF USING Mysql_Statement TO QUERY DB
 */

/**
 * In this SQL statement we have two places to bind values. To setup binding
 * place a colon ':' and either a name or number. (Numbers must start at 1 then
 * 2, 3, and so on. Here we just have the number one.
 *
 * This is the statement we will send to be 'prepared'
 */
$sql = 'SELECT * FROM :table_name LIMIT :1';

/**
 * When we prepare a statement we will recieve a Mysql_Statement object
 */
$stmnt =& $db_handle->prepare($sql);

/**
 * Now that we have the Mysql_Statement object first we must bind a value to
 * 'table_name'
 */
$stmnt->addBind('table_name', $db_table);

/**
 * We could also add a value to bind to '1' but we do not have to do that
 * explicitly we do that when we call execute().  When we call execute we should
 * receive a Mysql_Result object.
 */
try {
    /**
     * Notice that the value 20 sent to execute will bind to the '1' in the SQL
     * statement. If we had a :2 and :3 in our statement we would send them as
     * the second and third arguments to the execute statement.
     */
    $result =& $stmnt->execute(20);
}
catch (Mysql_Exception $e) {
    /**
     * If there is an error in the SQL we receive a Mysql_Exception object
     * so print the message returned.
     */
    echo $e->getMessage();
    exit;
}
?>

/**
 * fetch number of returned rows and print
 */
$num_rows = $result->fetchNumRows();
print_r(''.$num_rows);
exit;

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Improve the web with Nofollow Reciprocity.