class problem :: mysql function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

class problem :: mysql function

Post 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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply