GetTables(); if (!in_array('test', $tables)) { $qry = 'CREATE TABLE `test` ( `TestID` int(10) NOT NULL auto_increment, `Color` varchar(15) default NULL, `Age` int(10) default NULL, PRIMARY KEY (`TestID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;'; $db = new Mysql(); $db->query($qry); } /* -- -------------------------------------------- -- SQL to generate test table -- -------------------------------------------- CREATE TABLE `test` ( `TestID` int(10) NOT NULL auto_increment, `Color` varchar(15) default NULL, `Age` int(10) default NULL, PRIMARY KEY (`TestID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf-8; -- -------------------------------------------- */ // --- Open the database -------------------------------------------- // (Also note that you can fill in the variables in the top of the class // if you want to automatically connect when the object is created. If // you fill in the values when you create the obect, this is not needed.) if (! $db->Open()) { $db->Kill(); } echo "You are connected to the database
\n"; // --- Insert a new record ------------------------------------------ $sql = "INSERT INTO Test (Color, Age) Values ('Red', 7)"; if (! $db->Query($sql)) { $db->Kill(); } echo "Last ID inserted was: " . $db->GetLastInsertID(); // --- Or insert a new record with transaction processing ----------- $sql = "INSERT INTO Test (Color, Age) Values ('Blue', 3)"; $db->TransactionBegin(); if ($db->Query($sql)) { $db->TransactionEnd(); echo "Last ID inserted was: " . $db->GetLastInsertID() . "

\n"; } else { $db->TransactionRollback(); echo "

Query Failed

\n"; } // --- Query and show the data -------------------------------------- // (Note: $db->Query also returns the result set) if ($db->Query("SELECT * FROM Test")) { echo $db->GetHTML(); } else { echo "

Query Failed

"; } // --- Getting the record count is easy ----------------------------- echo "\n

Record Count: " . $db->RowCount() . "

\n"; // --- Loop through the records ------------------------------------- while ($row = $db->Row()) { echo $row->Color . " - " . $row->Age . "
\n"; } // --- Loop through the records another way ------------------------- $db->MoveFirst(); while (! $db->EndOfSeek()) { $row = $db->Row(); echo $row->Color . " - " . $row->Age . "
\n"; } // --- Loop through the records with an index ----------------------- for ($index = 0; $index < $db->RowCount(); $index++) { $row = $db->Row($index); echo "Row " . $index . ":" . $row->Color . " - " . $row->Age . "
\n"; } // --- We can even grab array data from the last result set --------- $myArray = $db->RecordsArray(); // --- List all of the tables in the database ----------------------- $tables = $db->GetTables(); foreach ($tables as $table) { echo $table . "
\n"; } // --- Show the columns (field names) in a table -------------------- $columns = $db->GetColumnNames("test"); foreach ($columns as $column) { echo $column . "
\n"; } // --- Find a column (field) type and length ------------------------ echo "Type: " . $db->GetColumnDataType("Color", "Test") . "
\n"; echo "Length: " . $db->GetColumnLength("Color", "Test") . "
\n"; // --- Get a column's ordinal position (the column number) ---------- echo $db->GetColumnID("Age", "Test") . "
\n"; // --- Check for errors --------------------------------------------- if ($db->Error()) { echo "

" . $db->Error() . "

\n"; } else { echo "

There were no errors

\n"; } // --- Format some values ready for SQL ----------------------------- // You do not have to create the object to use these. Simply include // the class in your PHP file. These are called "Static" methods. echo "

\n\n"; echo Mysql::SQLValue("Let's format some text") . "
\n"; echo Mysql::SQLValue(date("m/d/Y"), Mysql::SQLVALUE_DATE) . "
\n"; echo Mysql::SQLValue(123, Mysql::SQLVALUE_NUMBER) . "
\n"; // --- Format some values ready for SQL based on a boolean value ---- echo Mysql::SQLBooleanValue(false, "1", "0", Mysql::SQLVALUE_NUMBER); echo Mysql::SQLBooleanValue("ON", "Ya", "Nope"); echo Mysql::SQLBooleanValue(1, '+', '-');