note 101655 added to function.mysql-query
| From: | jack dot thomson at bgmc dot ca | Date: | Sun, 02 Jan 2011 18:28:37 +0000 |
| Subject: | note 101655 added to function.mysql-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-175400@lists.php.net to get a copy of this message | ||
Here is something I use all the time. It save on typing and adding extra if..else blocks to my
code. Basically, you use the function in a specific if statement to determine if there are any
records in your table, and if there are continue with a mysql_fetch_array loop.
Common usage:
<?php
if (get("select * from contacts where verified='on'", $q_contact) > 0)
{
while ($r_contact = mysql_fetch_array($q_contact))
{
// commands to apply to each record
}
} else {
// There are no records that match
}
?>
The "$q_contact" is passed by reference, so that any results of the query are put into it
via mysql_query. The function itself returns the number of rows that are returned. If there is a
mysql_error, -1 is returned.
<?php
// This function returns the number of rows returned in the SQL query, and assigns the results array
to the array provided
// Returns -1 if there is an error and prints the error
function get($sql, &$array)
{
$array = "";
$q = mysql_query($sql);
if (mysql_error())
{
$ret = -1;
print "<div><font style='font-family: arial; font-size: 12px;'><font
style='color: red;'>Error:</font> " . mysql_error() .
"</font></div>";
exit(1);
} else {
$ret = mysql_num_rows($q);
if ($ret > 0)
{
$array = $q;
}
}
return $ret;
}
?>
----
Server IP: 72.55.140.6
Probable Submitter: 68.150.39.137
----
Manual Page -- http://www.php.net/manual/en/function.mysql-query.php
Edit -- https://master.php.net/note/edit/101655
Del: integrated -- https://master.php.net/note/delete/101655/integrated
Del: useless -- https://master.php.net/note/delete/101655/useless
Del: bad code -- https://master.php.net/note/delete/101655/bad+code
Del: spam -- https://master.php.net/note/delete/101655/spam
Del: non-english -- https://master.php.net/note/delete/101655/non-english
Del: in docs -- https://master.php.net/note/delete/101655/in+docs
Del: other reasons-- https://master.php.net/note/delete/101655
Reject -- https://master.php.net/note/reject/101655
Search -- https://master.php.net/manage/user-notes.php