Page 1 of 1

class problem :: mysql function

Posted: Mon Nov 10, 2003 2:20 pm
by qads
i have made up a small class for mysql functions, i have a insert function in it which is taking over 30 secs to insert 1 single record, i been looking at it for quite a while but i cant see the problem, maybe someone else can see it :?

Code: Select all

<?php
/**
 * @return noting
 * @param $table table name
 * @param $fields table field names
 * @param $values values
 * @desc takes table name, field names and values for the fields, the number of
 values must match number of fields.
 */
function insert($table, $fields, $values)
{
$into = explode(",", $fields);
$value = explode(",", $values);
if(count($value) != count($into))
{
die("<b>Mysql Error: Fields Count Mismatch!</b><br />Unable to insert record.");
}
for($x = 0; $x<=count($value); $x++)
{
$value[$x] = addslashes(trim($value[$x]));
$into[$x] = trim($into[$x]);
if(!empty($value[$x]))
{
if($x == 0)
{
$fields .= "`$into[$x]`";
$gen_query .= "'$value[$x]'";
}
else
{
$fields .= ", `$into[$x]`";
$gen_query .= ", '$value[$x]'";
}
}
}
$query = mysql_query("INSERT INTO `$table` ($fields) VALUES($gen_query)")or die("<b>Mysql Error: Invild Query!</b><br />Unable to insert record.");
}
//this is how i am useing it
$insert = $mysql->insert("useronline", "timestamp,ip,username", "$timestamp,$ip,guest"); 

?>
its not the database btw.

thanks in adv for your help.

Posted: Mon Nov 10, 2003 4:19 pm
by Gen-ik
Try this.....

Code: Select all

<?php 

function insert($table, $fields, $values) 
{ 
    $into = explode(",", $fields); 
    $value = explode(",", $values); 

    if(count($value) != count($into)) 
    { 
        die("<b>Mysql Error: Fields Count Mismatch!</b><br />Unable to insert record.");
    }

    for($x = 0; $x<count($value); $x++) // CHANGED <= TO <
    { 
        $value[$x] = addslashes(trim($value[$x])); 
        $into[$x] = trim($into[$x]);

// REMOVED THE if(!empty) BIT BECAUSE IT'S NOT REALLY NEEDED.

        if($x == 0) 
        {
            $fields .= "`$into[$x]`"; 
            $gen_query .= "'$value[$x]'"; 
        } 
        else 
        { 
            $fields .= ", `$into[$x]`"; 
            $gen_query .= ", '$value[$x]'"; 
        } 
    } 

$query = mysql_query("INSERT INTO `$table` ($fields) VALUES($gen_query)")or die("<b>Mysql Error: Invild Query!</b><br />Unable to insert record.");

} 

?>

I can't really see what's eating up the time though.
It doesn't appear to be anything to do with this function.

Posted: Tue Nov 11, 2003 2:47 am
by qads
very weird!

it was this part:

Code: Select all

<?php
for($x = 0; $x<=count($value); $x++)
?>
Gen-ik changed it to

Code: Select all

<?php
for($x = 0; $x<count($value); $x++)
?>
now its working :D, i use <= in for loops all the time, never had a problem with it :?.

thanks guys

Posted: Tue Nov 11, 2003 2:51 am
by twigletmac
One potential area for inefficiency (not saying this is the prob, just pointing it out) is here:

Code: Select all

for($x = 0; $x<=count($value); $x++)
each time the loop runs the value of count($value) has to be reevaluated, you only really need it evaluated once though:

Code: Select all

$value_size = count($value);
for($x = 0; $x<=$value_size; $x++)
It could make a difference if you have a lot of fields.

Mac