Location: PHPKode > projects > Replex > replex-0.9.0/db/examples.txt
This is the e-mail message I had sent to someone that had questions about
how to use the class. They suggested to post this for others, and here it
is - Thanks Andrew!

To use the class, you will first need to include the class file:
include "_class.mysql.php";

Depending on your application, you could do something like:
if(!$MYSQL_CLASS_INC) require "_class.mysql.php";

to get the same results. The next step will be to set up a variable to
represent the object created. I usually use the name 'sql' for clarity,
but it can be whatever.

$sql=new mysql_class();

Using the line above, the user will be "phpuser", the password will be
"php", and the host is "localhost". If that doesn't fit with your
server, simply initiate the variable like this:

$sql=new mysql_class($sql_user,$sql_pass,$sql_host);

I usually use this format, and store the variables in a different
include file with other site-wide config settings. If you leave out
$sql_host from the above line, "localhost" (when MySQL server and web
server are on the same machine) is assumed.

The query format is that of mysql, so you don't need to learn any query
syntax with my class. All you need to do is figure out which query
function to use.

To get the results of a multiple row query, you would use something
similar to this:

$sql->Query(select * from table order by id);
for($i=0;$i<$sql->rows;$i++){   // for each row of results
        $sql->GetRow($i);       // get the data for row $i
        // store row elements in variables if necessary
        $id=$sql->data[id];
        $value=$sql->data[value];

        // do stuff with this row's data
}

The $sql->data array is an associative array, but if you has a query
similar to "select id, value, something from table", you could also get
the values in the for loop using something like this:
        $id=$sql->data[0];
        $value=$sql->data[1];
        $something=$sql->data[2];

When you want to do a query when you know there will only be one row
returned, use the following function:

$sql->QueryRow("select * from table where id = '$id'");

To get the values, just use the data array
$id=$sql->data[id];
$value=$sql->data[value];

If you want to query for a specific cell in your database, you can do so
like this:
$value=$sql->QueryItem("select value from table where id = '$id'");

A handy function I've included in my class is the Exists function. This
will return 1 if the query result exists, and 0 if it does not. I use
this function to test for results in things like searches:

if($sql->Exists("select item from table where id = '$id'")){
        // the item exists, so I want to get the result
        $item=$sql->QueryItem("select item from table where id = '$id'")
        // do something with it
}else{
        echo "No results found.";
}

Each time you do a query, $sql->rows & $sql->data are reset, so
becareful when doing a loop based on query results with additional
queries inside the loop (unless you initialize another variable like
$tmp_sql).

I have also included separate Insert, Update, and Delete queries. These
all change the $sql->a_rows value, but no other variables when executing
the query. This helps when you don't want $sql->data to be reset in a
for loop when doing inserts, deletes, or updates. It also makes the code
a little easier to read.

Hope this helps you out with the class.
Return current item: Replex