By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,780 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,780 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!!!

Aug 27 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Aug 27, 2:57 pm, TechieGrl <cschal...@gmail.comwrote:
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.

Aug 28 '07 #2

P: n/a
On Aug 28, 3:57 pm, "burgermeiste...@gmail.com"
<burgermeiste...@gmail.comwrote:
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!

Aug 28 '07 #3

P: n/a
On Aug 28, 4:25 pm, TechieGrl <cschal...@gmail.comwrote:
On Aug 28, 3:57 pm, "burgermeiste...@gmail.com"

<burgermeiste...@gmail.comwrote:
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.

Aug 29 '07 #4

P: n/a
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.
js*******@attglobal.net
==================
Aug 29 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.