Salagir's Blog

Bon le blog est devenu tout simple tout moche, mais j’ai passé 24h à le mettre à jour. Il a peu de visites. Son ancien design n’était pas foufou. On va en rester là, hein. C’est surtout du texte après tout.

The 1468456th PHP class that overloads mysql

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 "

$element[champ1]: $element[champ2] ($element[champ3])

"; } // 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 :


Commentaires

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *