Help - Search - Members - Calendar
Full Version: SQL Query
HTMLHelp Forums > Programming > Server-side Scripting
hewstone
Hey guys,

I have the code below which inputs data in a database, however instead of inputted the values i want, it inputs the string: Is there someting wrong with $RowName?

$ColName = "Col1,Col2,Col3";
$RowName = "'$line[0]','$line[1]','$line[2]'";

$data = file_get_contents($table);
$rows = explode("\n", $data);
foreach($rows as $row){
$line = explode(',', $row);
$SQLCODE = "INSERT INTO TempData($ColName) VALUES($RowName)";
$rS = $db_conn->execute($SQLCODE);

}
Brian Chandler
QUOTE
I have the code below...


What does this mean? Did you write it yourself? Are you trying to learn how to write php programs yourself?

Anyway...

QUOTE
$ColName = "Col1,Col2,Col3";


Do you understand what the = sign means? It does _not_ mean "equals", in any normal mathematical sense. (Blame the Americans for this: every programming language designed in Europe uses a distinctive "assignment" symbol, such as := )

It means: take the value on the right hand side, and assign it to the _location_ on the left hand side. So this results in the location called $ColNama having the value which is the _string_ (because of the quotes) "Col1,Col2,Col3". I think this is what you meant.

Now do the second line yourself, and you should understand the problem.

... hmm, it's not really that simple. The problem is in the PHP string replacement stuff. When you assign a value like "'$line[0]','$line[1]','$line[2]'" what happens? First, you can't use $line[0] within a double-quoted string to get the value of $line[0]. Second, the value of $line[0], or $line is what currently? Um, empty!

SteveL
With the assumption that $line is an array...try:

CODE
$RowName = "'{$line[0]}', '{$line[1]}', '{$line[2]}'";


The reason for this is that so called 'complex variables', (like a reference to an array index), aren't interpreted inside the double-quoted strings without the help of the brackets around the variables.

That suggestion aside though:

$ColName = "Col1,Col2,Col3";
$RowName = "'$line[0]','$line[1]','$line[2]'";

$data = file_get_contents($table);
$rows = explode("\n", $data);
foreach($rows as $row){
$line = explode(',', $row);
$SQLCODE = "INSERT INTO TempData($ColName) VALUES($RowName)";
$rS = $db_conn->execute($SQLCODE);

You can't use a variable before you assign it a value... :-)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.