note 92656 added to function.mysql-query
| From: | hidendraatrs2reborndotnet at osu1 dot php dot net | Date: | Sun, 02 Aug 2009 18:33:50 +0000 |
| Subject: | note 92656 added to function.mysql-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159016@lists.php.net to get a copy of this message | ||
i made two useful functions (well to me, anyway) to build and insert a query based on an array..
instead of having long queries that could become hard to read.
example:
<?php
insert_array('users', array( 'username' => 'bob',
'password' => 'pass123' ));
?>
would return a query executed with:
INSERT INTO
users SET username='bob',
password='pass123';
<?php
select_array('users', array(), array('username' => 'bob',
'password' => 'pass123'));
?>
would return a query executed with:
SELECT * FROM users WHERE username='bob' AND
password='pass123';
an empty array for the second parameter for select_array uses *, but it can be used like so:
array('username', 'password', 'email', 'etc')
hopefully someone finds these useful!
<?php
function insert_array($table, $arr) {
$temp = null;
$arrstr = "";
$c = 0;
if(!is_array($arr)) {
return null;
}
/*
* Generate the insert clause
*/
foreach($arr as $name => $value) {
if(strlen($arrstr) == 0) {
$arrstr .= 'SET ';
}
$arrstr .= ($c > 0 ? ', ' : '') .
'' . $name . '=' . '\'' . $value .
'\'';
if($c == 0) {
$c = 1;
}
}
/*
* Now execute the query and then return it.
*/
$temp = mysql_query('INSERT INTO ' . $table . ' ' . $arrstr .
';');
return $temp;
}
function select_array($table, $arr, $where = array()) {
$temp = null;
$arrstr = "";
$wherestr = "";
$c = 0;
if(!is_array($arr) || !is_array($where)) {
return null;
}
/*
* If it's an empty array we want everything from the table (greedy har har).
*/
if(count($arr) == 0) {
$arrstr = "*";
}
/*
* Generate the values to grab from the table
*/
foreach($arr as $name) {
$arrstr .= ($c > 0 ? ',' : '') .
'' . $name . '';
if($c == 0) {
$c = 1;
}
}
$c = 0;
/*
* Generate the where clause
*/
foreach($where as $name => $value) {
if(strlen($wherestr) == 0) {
$wherestr .= 'WHERE ';
}
$wherestr .= ($c > 0 ? ' AND ' : '') .
'' . $name . '=' . '\'' . $value .
'\'';
if($c == 0) {
$c = 1;
}
}
/*
* Build the query now with the generated strings
*/
$temp = mysql_query('SELECT ' . $arrstr . ' FROM ' . $table .
' ' . $wherestr . ';');
return $temp;
}
?>
----
Server IP: 216.235.13.26
Probable Submitter: 24.138.27.162
----
Manual Page -- http://www.php.net/manual/en/function.mysql-query.php
Edit -- https://master.php.net/note/edit/92656
Del: integrated -- https://master.php.net/note/delete/92656/integrated
Del: useless -- https://master.php.net/note/delete/92656/useless
Del: bad code -- https://master.php.net/note/delete/92656/bad+code
Del: spam -- https://master.php.net/note/delete/92656/spam
Del: non-english -- https://master.php.net/note/delete/92656/non-english
Del: in docs -- https://master.php.net/note/delete/92656/in+docs
Del: other reasons-- https://master.php.net/note/delete/92656
Reject -- https://master.php.net/note/reject/92656
Search -- https://master.php.net/manage/user-notes.php