php-general Digest 5 Dec 2009 00:56:16 -0000 Issue 6472
Topics (messages 300246 through 300255):
Good SQL builder class
300246 by: Anton Heuschen
300247 by: Tony Marston
300254 by: Daevid Vincent
php.ini session_auto.start = 1
300248 by: scotdiddle.silverdarkroom.com
300249 by: scotdiddle.silverdarkroom.com
300250 by: scotdiddle.silverdarkroom.com
json_last_error
300251 by: Kirby Bakken
300252 by: Jim Lucas
300253 by: Ryan Sun
Use specific IP for outbound traffic.
300255 by: shiplu
Administrivia:
To subscribe to the digest, e-mail:
[email protected]
To unsubscribe from the digest, e-mail:
[email protected]
To post to the list, e-mail:
[email protected]
----------------------------------------------------------------------
--- Begin Message ---
Good day.
I'm looking for a good class to handle building dynamically from and
array (and if it is good it will automatically determine / or even
have different methods) to handle mutli-dimensional arrays or simple
associative arrays ... and build the SQL statement :
for example I have an array :
$home[$suburb]["street"] = test1;
$home[$suburb]["housenr"] =2;
Ok to keep it simple to 2, then I want to build the SQL like
insert into homes (STREET, HOUSENR) VALUES ($val1,$val2);
something like that, but I could also pass some array like :
$home["street"] = test2;
$home["housenr"] = 2;
but the idea stays the same = the index is the name of the DB fields
and the assigned value the element
I have looked on hotscripts and phpclasses but I have no idea how good
the solutions are that I have found thus far - therefor need some
recommendation from someone else past experience of this
--- End Message ---
--- Begin Message ---
Take a look at http://www.tonymarston.net/php-mysql/databaseobjects.html
You can also download a working example of this code from
http://www.tonymarston.net/php-mysql/sample-application.html
--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
"Anton Heuschen" <[email protected]> wrote in message
news:[email protected]...
> Good day.
>
> I'm looking for a good class to handle building dynamically from and
> array (and if it is good it will automatically determine / or even
> have different methods) to handle mutli-dimensional arrays or simple
> associative arrays ... and build the SQL statement :
>
> for example I have an array :
>
> $home[$suburb]["street"] = test1;
> $home[$suburb]["housenr"] =2;
>
>
> Ok to keep it simple to 2, then I want to build the SQL like
>
> insert into homes (STREET, HOUSENR) VALUES ($val1,$val2);
>
>
> something like that, but I could also pass some array like :
>
> $home["street"] = test2;
> $home["housenr"] = 2;
>
>
> but the idea stays the same = the index is the name of the DB fields
> and the assigned value the element
>
>
>
> I have looked on hotscripts and phpclasses but I have no idea how good
> the solutions are that I have found thus far - therefor need some
> recommendation from someone else past experience of this
--- End Message ---
--- Begin Message ---
Multi-dimensional arrays generally indicate some kind of glue/hanging table, so
you'll have to special case them for your needs...
But here is a generic insert and update functions that may be a groundwork for
you....
/**
* Insert a single row into a $database.$table from an array hash of column =>
value pairs
*
* Usually these are the form field names corresponding to the equivallent SQL
database column/field name.
* Use the string 'null' to insert a true NULL.
*
* @access public
* @return mixed inserted ID on success or result on failure
* @param string $database the database to connect to (agis_core) is the
default
* @param string $table the name of the table to insert into
* @param hash $rows hash of column => value pairs (optionally columns
validated against $validate_columns)
* @param array $valid_columns array of column/field names. Also useful
to limit SQL to certain forced columns to prevent
unwanted tampering with 'default' columns for example.
* @author Daevid Vincent
* @date 11/23/09
* @see sql_update(), sql_insert_id()
*/
function sql_insert($database, $table, $rows, $valid_columns=null)
{
ksort($rows); //not required, just easier to debug and find appropriate
keys.
$validate_columns = (is_array($valid_columns)) ? true : false;
$temp = array();
$arrays = array();
foreach ($rows as $column => $val)
{
if (is_array($val))
{
$arrays[$column] = $val;
unset($rows[$column]);
continue;
}
if ($validate_columns && !in_array($column, $valid_columns))
{
unset($rows[$column]);
continue;
}
$val = trim($val);
if (!$val)
{
unset($rows[$column]);
continue;
}
if (strtolower($val) == 'null')
$temp[$column] = 'NULL';
else
$temp[$column] = "'".mysql_escape_string($val)."'";
}
$values = implode(', ',$temp);
$columns = "`".implode("`, `", array_keys($rows))."`";
$sql = "INSERT INTO `".$table."` (".$columns.") VALUES (".$values.")";
//echo $sql;
if (count($arrays))
echo "\n<br/>sql_insert() has arrays that need to be handled
still: ".implode(', ', array_keys($arrays));
$result = sql_query($database, $sql, null, false);
if ($result)
{
$iid = sql_insert_id();
if ($iid) return $iid;
}
return $result;
}
/**
* Update rows in $database.$table from an array hash of column => value pairs
*
* Usually these are the form field names corresponding to the equivallent SQL
database column/field name.
* Use the string 'null' to insert a true NULL.
*
* @access public
* @return mixed affected rows on success or result on failure
* @param string $database the database to connect to (agis_core) is the
default
* @param string $table the name of the table to insert into
* @param hash $rows hash of column => value pairs (optionally columns
validated against $validate_columns)
* @param mixed hash of ID column/field name and record ID value [such
as array('id_foo' => 69)] OR string to craft custom
WHERE clause
* @param array $valid_columns array of column/field names. Also useful
to limit SQL to certain forced columns to prevent
unwanted tampering with 'default' columns for example.
* @author Daevid Vincent
* @date 11/23/09
* @see sql_insert()
*/
function sql_update($database, $table, $rows, $where, $single=true,
$valid_columns=null)
{
ksort($rows); //not required, just easier to debug and find appropriate
keys.
$validate_columns = (is_array($valid_columns)) ? true : false;
$temp = array();
$arrays = array();
foreach ($rows as $column => $val)
{
if (is_array($val))
{
$arrays[$column] = $val;
unset($rows[$column]);
continue;
}
if ($validate_columns && !in_array($column, $valid_columns))
{
unset($rows[$column]);
continue;
}
$val = trim($val);
if (!$val)
{
unset($rows[$column]);
continue;
}
if (strtolower($val) == 'null')
$temp[$column] = '`'.$column."` = NULL";
else
$temp[$column] = '`'.$column."` =
'".mysql_escape_string($val)."'";
}
$sql = "UPDATE `".$table."` SET ".implode(', ', $temp);
if (is_array($where))
{
foreach ($where as $c => $v)
$w[] = '`'.$c."` = '".mysql_escape_string($v)."'";
$sql .= " WHERE ".implode(' AND ', $w);
}
else $sql .= ' '.$where;
if ($single) $sql .= ' LIMIT 1';
//echo $sql;
if (count($arrays))
echo "\n<br/>sql_update() has arrays that need to be handled
still: ".implode(', ', array_keys($arrays));
$result = sql_query($database, $sql, null, false);
if ($result)
{
$ar = sql_affected_rows($database);
if ($ar) return $ar;
}
return $result;
}
> -----Original Message-----
> From: Anton Heuschen [mailto:[email protected]]
> Sent: Friday, December 04, 2009 5:10 AM
> To: PHP General List
> Subject: [PHP] Good SQL builder class
>
> Good day.
>
> I'm looking for a good class to handle building dynamically from and
> array (and if it is good it will automatically determine / or even
> have different methods) to handle mutli-dimensional arrays or simple
> associative arrays ... and build the SQL statement :
>
> for example I have an array :
>
> $home[$suburb]["street"] = test1;
> $home[$suburb]["housenr"] =2;
>
>
> Ok to keep it simple to 2, then I want to build the SQL like
>
> insert into homes (STREET, HOUSENR) VALUES ($val1,$val2);
>
>
> something like that, but I could also pass some array like :
>
> $home["street"] = test2;
> $home["housenr"] = 2;
>
>
> but the idea stays the same = the index is the name of the DB fields
> and the assigned value the element
>
>
>
> I have looked on hotscripts and phpclasses but I have no idea how good
> the solutions are that I have found thus far - therefor need some
> recommendation from someone else past experience of this
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--- End Message ---
--- Begin Message ---
Hello All,
In my previous LAMP position, I developed an intranet system from
scratch, and chose to use session_auto.start = 1 for sessions.
My new employer has auto-start turned off, and I have been tasked with
re-writing the entire system. I would like to use auto-start, but
during testing I found a couple of system calls that don't work if the
session has " already been started ".
I there a resource somewhere out in google-land which will show me a
list of system functions don't play nice session_auto.start = 1 ?
Also, what are the thoughts of the folks on this list about using the
auto-start setting ?
Any advice / direction will be appreciated.
Scot L. Diddle, Richmonc VA
--- End Message ---
--- Begin Message ---
Hello All,
In my previous LAMP position, I developed an intranet system from
scratch, and chose to use session_auto.start = 1 for sessions.
My new employer has auto-start turned off, and I have been tasked with
re-writing the entire system. I would like to use auto-start, but
during testing I found a couple of system calls that don't work if the
session has " already been started ".
I there a resource somewhere out in google-land which will show me a
list of system functions don't play nice session_auto.start = 1 ?
Also, what are the thoughts of the folks on this list about using the
auto-start setting ?
Any advice / direction will be appreciated.
Scot L. Diddle, Richmond VA
--- End Message ---
--- Begin Message ---
Hello All,
In my previous LAMP position, I developed an intranet system from
scratch, and chose to use session_auto.start = 1 for sessions.
My new employer has auto-start turned off, and I have been tasked with
re-writing the entire system. I would like to use auto-start, but
during testing I found a couple of system calls that don't work if the
session has " already been started ".
I there a resource somewhere out in google-land which will show me a
list of system functions don't play nice session_auto.start = 1 ?
Also, what are the thoughts of the folks on this list about using the
auto-start setting ?
Any advice / direction will be appreciated.
Scot L. Diddle, Richmond VA
--- End Message ---
--- Begin Message ---
None of the servers I run on have PHP 5.3, and I have no idea when
they'll be updated. The json_last_error function is in 5.3. What can I
do to provide a temporary json_last_error function until my servers get
updated to 5.3? Is there source available for json_last_error somewhere?
Thanks,
Kirby
--- End Message ---
--- Begin Message ---
Kirby Bakken wrote:
> None of the servers I run on have PHP 5.3, and I have no idea when
> they'll be updated. The json_last_error function is in 5.3. What can I
> do to provide a temporary json_last_error function until my servers get
> updated to 5.3? Is there source available for json_last_error somewhere?
>
> Thanks,
>
> Kirby
>
I don't believe you will be able to provide this functionality without
recreating the json_decode() function also.
json_last_error() looks like it retrieves information that is from the last
attempt to run json_decode() on a given string. My guess is that json_decode()
set an internal variable that json_last_error() picks up and returns.
Without having json_decode() say what the problem was/is then you can never
recreate the json_last_error() function.
Jim Lucas
--- End Message ---
--- Begin Message ---
json_decode
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)
so json_decode will be avaliable for php 5.2.0+
php is open source, I believe you can get an idea from their C source
On Fri, Dec 4, 2009 at 2:16 PM, Jim Lucas <[email protected]> wrote:
> Kirby Bakken wrote:
> > None of the servers I run on have PHP 5.3, and I have no idea when
> > they'll be updated. The json_last_error function is in 5.3. What can I
> > do to provide a temporary json_last_error function until my servers get
> > updated to 5.3? Is there source available for json_last_error somewhere?
> >
> > Thanks,
> >
> > Kirby
> >
>
> I don't believe you will be able to provide this functionality without
> recreating the json_decode() function also.
>
> json_last_error() looks like it retrieves information that is from the last
> attempt to run json_decode() on a given string. My guess is that
> json_decode()
> set an internal variable that json_last_error() picks up and returns.
>
> Without having json_decode() say what the problem was/is then you can never
> recreate the json_last_error() function.
>
> Jim Lucas
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--- End Message ---
--- Begin Message ---
Hello,
I have 3 IPs in my VPS. Everytime I request to a site it uses only 1
IP as gateway. So all the outbound traffic goes thorough that IP.
What I want is one of my script will always communicate using an
specific ip. Not default one.
I'll use 3 scripts for 3 IPs. How can I do that?
I know it can be done by changing the routing table. I think this
technique has a problem. If one script changes the gate way to its own
IP other script will be using that IP too.
Is there any way to achieve this without changing the routing table gloablly?
--
Shiplu
Innovation distinguishes bet ... ... (ask Steve Jobs the rest)
--- End Message ---