note 99061 deleted from function.die by googleguy
| From: | googleguy@php.net | Date: | Tue, 15 Jan 2013 04:39:43 +0000 |
| Subject: | note 99061 deleted from function.die by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193041@lists.php.net to get a copy of this message | ||
Note Submitter: admin at studio-gepard dot pl
----
Example of PHP function with SELECT query using.
It simplifies usage of SQL and makes code much much cleaner. (similar functions can be made for
other queries)
<?php
define("sql_error_reporting",true); //true to display errors, false to hide them
function SQLselect ($TABLE,$WHERE){
if (empty($WHERE)){
$sql = "SELECT * FROM
$TABLE";
}else{
$sql = "SELECT * FROM $TABLE WHERE $WHERE";
}
$query=mysql_query($sql);
if ($query===false){ if (sql_error_reporting) {
die ("Sql error of <b>".$_SERVER["SCRIPT_NAME"]."</b>. Call to
$TABLE in ".__FILE__." on line:
".__LINE__."<br>".mysql_error()."<br><b>SQL:</b><br>$sql<br>");
}else{ die(); } }
return mysql_fetch_array($query);
}
?>
What this function does is taking the table and your query, execute the mysql query and provide the
result. If error happen during the query, than provide detailed output using code provided by
matthias dot schniedermeyer at telefonica dot de
---
Usage:
$id=1;
$result = SQLselect ("pages","id='$id'");
---
Error output example:
Sql error of /website/index.php. Call to pages in C:\www\website\functions\sql.inc on
line: 43
Unknown column 'id' in 'where clause'
SQL:
SELECT * FROM pages WHERE id='1'