PHP Classes

File: data_mysql_ex.txt

Recommend this page to a friend!
  Classes of Carlos Falo Hervás   mysql_db   data_mysql_ex.txt   Download  
File: data_mysql_ex.txt
Role: Documentation
Content type: text/plain
Description: Some examples
Class: mysql_db
MySQL Database access & control classes
Author: By
Last change: text formatting
Date: 21 years ago
Size: 2,418 bytes
 

Contents

Class file image Download
===== SOME EXAMPLES FOR MYSQL_DB CLASS ===== I'll be adding some examples to the class from now on... keep an eye on this file EXAMPLE 1: Creating a select with mysql_db classes NOTE: use an existing database <? require("mysql_conn.php") ; ?> <? require("mysql_recordset.php") ; ?> <? // FUNCTION THAT CREATES A SELECT FROM $table USING $valuefld AS SOURCE // FOR VALUES AND $optionfld FOR OPTION NAMES - REQUIRES NAME OF A VALID // MYSQL_CONN OBJECT AS STRING function create_select($connobj,$table,$selected,$name,$valuefld,$optionfld) { echo "<SELECT NAME=\"" . $name . "\">"; $sql = "SELECT " . $valuefld . "," . $optionfld . " FROM " . $table ; echo $sql ; $rs = new mysql_recordset($connobj,$sql) ; if ($rs->query()) { while (!$rs->movenext()) { echo "<OPTION value=\"" . $rs->value($valuefld) . "\"" ; if ($rs->value($valuefld)==$selected) { echo " SELECTED" ; } echo ">" . $rs->value($optionfld) ; } } echo "</select>" ; } $conn = new mysql_conn("localhost","user","pwd","database") ; $conn->init() ; create_select("conn","tblname",1,"myselect","id","name") ; $conn->destroy() ; ?> SOME EXPLANATION... I divided the example in 2 to better focus on each part as well as to provide an usable function for creating selects ;) First we need a new connection object thus we call the constructor (mysql_conn()) providing server, user, password and database as params. When done we ask for a select... let's examine the function... we construct the SQL command normally and use it to create a recordset (that is a database result containing the data we just asked) $recordset->query() method returns recordcount thus allowing for 0 matches on the database on the if clause. Remember $recordset->query() does not initialize the result pointer to the first row (fetching the first row in fact), this is done by $recordset->movenext() that returns a false (0) when no more rows can be fetched allowing for a while condition directly. The rest is quite simple, general echoing for the results... $recordset->value RETURNS the value for the field while $recordset->field() actually echoes this value on screen have it in mind if you get weird echoes while executing your script.