Jump to content

PHP Loop in MYSQL


dizzy1

Recommended Posts

I am trying to loop an array of results into the VALUES of a MySql, and then insert into the DB. I think my syntax is wrong around

 echo "'".{'$Value'}."'";	

 

 

More of the MYSQL Code:

 

$mysql = mysql_query(

" INSERT INTO 'TestTable'
(
foreach( $Array1 as $key => $Value){
	echo $Value;
		if ($key != $SizeOfArray1){
		echo ",";
		}
}

)

VALUES (

foreach( $Array2 as $key => $Value){
 echo "'".{'$Value'}."'";
	if ($key != $SizeOfArray2){
	echo ",";
	}
}
)


");

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/183019-php-loop-in-mysql/#findComment-965937
Share on other sites

You are way off on your syntax:

 

<?php
$cols = ""
foreach( $Array1 as $key => $Value){
$cols .= $Value;
if ($key != $SizeOfArray1){
	$cols .= echo ",";
}
}
$vals = ""
foreach( $Array2 as $key => $Value){
$vals .= "'".{'$Value'}."'";
if ($key != $SizeOfArray2){
	$vals .= ",";
}
}
$sql = mysql_query(" INSERT INTO TestTable ($cols) VALUES ($vals)");
?>

 

Notice in the mysql_query statement you had single quotes ( ' ) around TestTable, in MySQL it should be backticks ( ` ) around column names and tables which are really only needed if you are using a reserved word.

 

Questions let me know.[/code]

Link to comment
https://forums.phpfreaks.com/topic/183019-php-loop-in-mysql/#findComment-965962
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.