note 69598 added to function.mysql-result

From: Date: Tue, 12 Sep 2006 11:18:22 +0000
Subject: note 69598 added to function.mysql-result
Groups: php.notes 
Request: Send a blank email to php-notes+get-117052@lists.php.net to get a copy of this message
two simple but very useful functions, for converting a query to a value or an array: <?php function mysql_evaluate($query, $default_value="undefined") { $result = mysql_query($query); if (mysql_num_rows($result)==0) return $default_value; else return mysql_result($result,0); } function mysql_evaluate_array($query) { $result = mysql_query($query); $values = array(); for ($i=0; $i<mysql_num_rows($result); ++$i) array_push($values, mysql_result($result,$i)); return $values; } ?> http://tora.us.fm/_script/highlight.php?file=sql Usage examples: <?php $customer_count = mysql_evaluate("SELECT COUNT(*) FROM customers"); $customer_names = mysql_evaluate_array("SELECT name FROM customers"); $customer_type = mysql_evaluate("SELECT type FROM customers WHERE name='$name'", "DEFAULT_TYPE"); ?> ---- Server IP: 192.117.122.43 Probable Submitter: 85.250.7.162 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-result.php Edit -- https://master.php.net/note/edit/69598 Del: integrated -- https://master.php.net/note/delete/69598/integrated Del: useless -- https://master.php.net/note/delete/69598/useless Del: bad code -- https://master.php.net/note/delete/69598/bad+code Del: spam -- https://master.php.net/note/delete/69598/spam Del: non-english -- https://master.php.net/note/delete/69598/non-english Del: in docs -- https://master.php.net/note/delete/69598/in+docs Del: other reasons-- https://master.php.net/note/delete/69598 Reject -- https://master.php.net/note/reject/69598 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#117052) next »