Salagir's Blog

Old name was: "Why do witches burn?"

This is french section. Go to English version

My first code is totally uninteresting for real developers, but I will use this class often in my next scripts, so you need to know a little about this class.

Plus, it’s certainly a rip-off of PHP 5 mysql class, but well, I still use 4.

It still can do a few good things.

  • Good display of errors.
  • Use easily Transactions (a great feature of mysql, on innoDB tables only)
  • Usable thru an instance, or not

Exemple of use :

$SQL = new sqlQueries();
 
// one element directly
echo $SQL->query2cell("SELECT firstname FROM persons WHERE id=$id LIMIT 1");
 
// loop on result
foreach($SQL->query2assoc("SELECT * FROM smalltable") as $element) {
    echo "<p>$element[champ1]: <code>$element[champ2] ($element[champ3])</p>";
}
 
// one-liner !
echo "Ids: ".implode(", ", $SQL->query2list("SELECT id FROM table WHERE $conditions") );
 
// escaping (you of course disabled this horrible magic quotes thing)
$name = $SQL->e( $_REQUEST['name'] );
 
// take no risks : if the 1st query miss, the 2nd wont do anything.
// and even: vice versa.
$SQL->tr_begin();
$SQL->query("UPDATE persons SET name='$name' WHERE id=$id");
$SQL->query("INSERT INTO actions (type, infos) VALUES('changename', '$id -> $name')");
$SQL->tr_end();

Change the constructor code as you with. Usually, I delete it and make mysql_connect() elsewhere.

Download code of version 0.95 here :

Leave a Reply