PHP Class SqlQuery


Synopsis

SQL Query is a PHP Class that helps when you have to work with SQL queries. The actual version handels only MySQL. Please inform yourself about the license of you MySQL version (and distribution) before using this class. A version of MySQL must be installed in your host server. This class was developed with MySQL Version 4.0.22.

License

GPL-2
Please read the license information before you use this source code!

PHP Versions

The class was developed with PHP Version 4.3.10 and 5.0.3, the class functions with both versions.

Why do I use it?

When you use SELECT FROM WHERE queries then you have to fetch row by row the entrie information. Sometimes you want to get everything in a single array (like LDAP-queries with OpenLDAP). With this class you get such a flavor, the class executes the SQL querie (only by SELECT FROM WHERE queries) you will get an array with the whole result of the query with a LDAP flavor.

Download

Example

I use following content in the example table.

BASH
mysql> SELECT * FROM example;
+----+------+----------+-----------+
| id | nick | password | name      |
+----+------+----------+-----------+
|  1 | Nick | ssc      | Alexander |
|  2 | Mei  | abcde    | Michael   |
|  3 | Yugi | machomen | Daniela   |
+----+------+----------+-----------+
3 rows in set (0.00 sec)

This example has only one file:
  1. index.php

1. index.php

index.php
<?php
 
require_once( "class.SqlQuery.php" );
 
 
?>
<html>
<head> 
<title >SqlQuery Example</title>
</head>
<body>
<?php
 
$sql = new SqlQuery( "localhost", "<your username>", "<your password>");
 
$err = $sql->getError ();
 
if($err['errno' ])
{
    $txt = "Error <b>$err[errno]</b> while connecting with MySQL Database<br>"; 
    $txt.= "<span style=\"color: #ff0000\"><i>$err[error]</i></span>" ;
    echo $txt ;
} else {
 
    $sql ->selectDB( "test");
 
     $err = $sql ->getError();
    if( $err['errno' ])
    {
     $txt = "Error <b>$err[errno]</b> while changing the database<br>"; 
    $txt.= "<span style=\"color: #ff0000\"><i>$err[error]</i></span>" ;
    echo $txt ;
    } else {
    echo "The content of <tt>example</tt> in the SqlQuery-Array is:<p>\n" ;
    $sql ->setSqlQuery( "SELECT * FROM example");
 
    if(! $sql->execute ())
    {
         $err = $sql ->getError(); 
        $txt = "Error <b>$err[errno]</b> while executing the SQL query<br>" ;
        $txt .= "<span style=\"color: #ff0000\"><i>$err[error]</i></span>" ;
        echo $txt ;
    } else {
        echo "<pre>\n";
         print_r($sql ->getData()); 
        echo "</pre>\n" ;
    }
    }
 
     $sql->closeConnection ();
}
 
?>
</body>
</html>

When this code is parsed you get this:

output <html>
<head>
<title>SqlQuery Example</title>
</head>
<body>
The content of
<tt>example</tt> in the SqlQuery-Array is:<p>
<pre>
Array
(
    [rows] => 3
    [cols] => 4
    [data] => Array
        (
            [id] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                )

            [0] => id
            [nick] => Array
                (
                    [0] => Nick
                    [1] => Mei
                    [2] => Yugi
                )

            [1] => nick
            [password] => Array
                (
                    [0] => ssc
                    [1] => abcde
                    [2] => machomen
                )

            [2] => password
            [name] => Array
                (
                    [0] => Alexander
                    [1] => Michael
                    [2] => Daniela
                )

            [3] => name
        )

)
</pre>
</body>
</html>

Valid XHTML 1.0 Strict   Valid CSS!