Any thoughts on this prepared statement for $stmt_insert->bind_param ?????

Any thoughts on this prepared statement for $stmt_insert->bind_param ?????

am 27.08.2007 21:57:28 von CSTechie

Prepared statements are new to me and having to do this with a multi-
dimensional array is beyond me.

Here is the prepared statement block:

// Prepare to insert a record into table1
$flag_insert = false;
$sql_insert = "INSERT IGNORE INTO table1 ";
$sql_insert .= "(ID, url)";
$sql_insert .= "VALUES ";
$sql_insert .= "($field1, $field2)";
$stmt_insert = $db->stmt_init();
if ($stmt_insert->prepare($sql_insert)){
$flag_insert = true;
}

The array that I want to insert looks like this:

Array (
[0] => Array ( [0] => 1001 [1] => www.google.com )
[1] => Array ( [0] => 1002 [1] => www.yahoo.com )


Here is where I put the 2 together:

// Insert the items into the database
foreach ($rss->items_array as $i){

if($flag_insert){

$stmt_insert->bind_param($sql_insert, 'is', $field1, $field2);
$field1 = $rss->items_array[$i][0];
$field2 = $rss->items_array[$i][1];
echo "sql " . $sql_insert . "\n";
$stmt_insert->execute();
} // if
} // foreach ($ca as $location)



Here is the error:
Fatal error: Cannot pass parameter 2 by reference...


Any thoughts would be so VERY much appreciated!!!

Re: Any thoughts on this prepared statement for $stmt_insert->bind_param ?????

am 28.08.2007 22:57:54 von burgermeister01

On Aug 27, 2:57 pm, TechieGrl wrote:
> Prepared statements are new to me and having to do this with a multi-
> dimensional array is beyond me.
>
> Here is the prepared statement block:
>
> // Prepare to insert a record into table1
> $flag_insert = false;
> $sql_insert = "INSERT IGNORE INTO table1 ";
> $sql_insert .= "(ID, url)";
> $sql_insert .= "VALUES ";
> $sql_insert .= "($field1, $field2)";
> $stmt_insert = $db->stmt_init();
> if ($stmt_insert->prepare($sql_insert)){
> $flag_insert = true;
>
> }
>
> The array that I want to insert looks like this:
>
> Array (
> [0] => Array ( [0] => 1001 [1] =>www.google.com)
> [1] => Array ( [0] => 1002 [1] =>www.yahoo.com)
>
> Here is where I put the 2 together:
>
> // Insert the items into the database
> foreach ($rss->items_array as $i){
>
> if($flag_insert){
>
> $stmt_insert->bind_param($sql_insert, 'is', $field1, $field2);
> $field1 = $rss->items_array[$i][0];
> $field2 = $rss->items_array[$i][1];
> echo "sql " . $sql_insert . "\n";
> $stmt_insert->execute();
> } // if
> } // foreach ($ca as $location)
>
> Here is the error:
> Fatal error: Cannot pass parameter 2 by reference...
>
> Any thoughts would be so VERY much appreciated!!!

If I'm reading your code right, it should just be a simple matter of
correcting your array references.
I think the following lines of code need to change like so...

...
$field1 = $i[0];
$field2 = $i[1];
...

$i is already assigned to each subsequent value of $rss->items_array,
so in the code that you posted, you're iterating through the array and
then using one of the array's 2nd dimension arrays as an array
position. I haven't used the method you're employing to make sql
statements, so there could be further problems, but this one seems
fairly glaring to me.

Re: Any thoughts on this prepared statement for $stmt_insert->bind_param ?????

am 28.08.2007 23:25:16 von CSTechie

On Aug 28, 3:57 pm, "burgermeiste...@gmail.com"
wrote:

> > Here is the error:
> > Fatal error: Cannot pass parameter 2 by reference...
>
> > Any thoughts would be so VERY much appreciated!!!
>
> If I'm reading your code right, it should just be a simple matter of
> correcting your array references.
> I think the following lines of code need to change like so...
>
> ...
> $field1 = $i[0];
> $field2 = $i[1];
> ...
>
> $i is already assigned to each subsequent value of $rss->items_array,
> so in the code that you posted, you're iterating through the array and
> then using one of the array's 2nd dimension arrays as an array
> position. I haven't used the method you're employing to make sql
> statements, so there could be further problems, but this one seems
> fairly glaring to me.


Unfortunately, I made the changes as suggested and still get the same
error - Fatal error: Cannot pass parameter 2 by reference...


I replaced this:
$stmt_insert->bind_param($sql_insert, 'is', $field1, $field2);
$field1 = $rss->items_array[$i][0];
$field2 = $rss->items_array[$i][1];
echo "sql " . $sql_insert . "\n";
$stmt_insert->execute();

With this:

$stmt_insert->bind_param($sql_insert, 'is', $field1, $field2);
$field1 = $i[0];
$field2 = $i[1];
echo "sql " . $sql_insert . "\n";
$stmt_insert->execute();



Thanks!

Re: Any thoughts on this prepared statement for $stmt_insert->bind_param ?????

am 29.08.2007 16:05:59 von burgermeister01

On Aug 28, 4:25 pm, TechieGrl wrote:
> On Aug 28, 3:57 pm, "burgermeiste...@gmail.com"
>
>
>
> wrote:
> > > Here is the error:
> > > Fatal error: Cannot pass parameter 2 by reference...
>
> > > Any thoughts would be so VERY much appreciated!!!
>
> > If I'm reading your code right, it should just be a simple matter of
> > correcting your array references.
> > I think the following lines of code need to change like so...
>
> > ...
> > $field1 = $i[0];
> > $field2 = $i[1];
> > ...
>
> > $i is already assigned to each subsequent value of $rss->items_array,
> > so in the code that you posted, you're iterating through the array and
> > then using one of the array's 2nd dimension arrays as an array
> > position. I haven't used the method you're employing to make sql
> > statements, so there could be further problems, but this one seems
> > fairly glaring to me.
>
> Unfortunately, I made the changes as suggested and still get the same
> error - Fatal error: Cannot pass parameter 2 by reference...
>
> I replaced this:
> $stmt_insert->bind_param($sql_insert, 'is', $field1, $field2);
> $field1 = $rss->items_array[$i][0];
> $field2 = $rss->items_array[$i][1];
> echo "sql " . $sql_insert . "\n";
> $stmt_insert->execute();
>
> With this:
>
> $stmt_insert->bind_param($sql_insert, 'is', $field1, $field2);
> $field1 = $i[0];
> $field2 = $i[1];
> echo "sql " . $sql_insert . "\n";
> $stmt_insert->execute();
>
> Thanks!

Does it matter if you declare $field1 and $field2 before calling
bind_param(), e.g.
....
$field1 = $i[0];
$field2 = $i[1];
$stmt_insert->bind_param($sql_insert, 'is', $field1, $field2);
....

I'm not sure what database engine (mysql?pgsql?) or function
library(mysqli?3rd party?) you're using to do this. In fact, if this
suggestion doesn't help, that would be good information to post next
time.

Re: Any thoughts on this prepared statement for $stmt_insert->bind_param?????

am 29.08.2007 17:48:53 von Jerry Stuckle

TechieGrl wrote:
> Prepared statements are new to me and having to do this with a multi-
> dimensional array is beyond me.
>
> Here is the prepared statement block:
>
> // Prepare to insert a record into table1
> $flag_insert = false;
> $sql_insert = "INSERT IGNORE INTO table1 ";
> $sql_insert .= "(ID, url)";
> $sql_insert .= "VALUES ";
> $sql_insert .= "($field1, $field2)";
> $stmt_insert = $db->stmt_init();
> if ($stmt_insert->prepare($sql_insert)){
> $flag_insert = true;
> }
>
> The array that I want to insert looks like this:
>
> Array (
> [0] => Array ( [0] => 1001 [1] => www.google.com )
> [1] => Array ( [0] => 1002 [1] => www.yahoo.com )
>
>
> Here is where I put the 2 together:
>
> // Insert the items into the database
> foreach ($rss->items_array as $i){
>
> if($flag_insert){
>
> $stmt_insert->bind_param($sql_insert, 'is', $field1, $field2);
> $field1 = $rss->items_array[$i][0];
> $field2 = $rss->items_array[$i][1];
> echo "sql " . $sql_insert . "\n";
> $stmt_insert->execute();
> } // if
> } // foreach ($ca as $location)
>
>
>
> Here is the error:
> Fatal error: Cannot pass parameter 2 by reference...
>
>
> Any thoughts would be so VERY much appreciated!!!
>

Amongst the array problem noted by burgermeister, your sql statement is
incorrect. You didn't leave any placeholders.

When using prepared statements with placeholders, you use "?" to
indicate where the parameters go. So your INSERT statement should look
something like:

INSERT IGNORE INTO table1 (ID, url) VALUES (?, ?)

Why this would cause the error you're seeing is beyond me, though.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================