Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Should this work?

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
Should this work?
$query = "SELECT Orderdate, '$op1' FROM Orders";

$op1 being a variable containing a column name. Does not seem to work, might be something else if its an allowed syntax. Thanks
 
No, that should not work. Column names aren't placed inside singlequotes.

Try:

$query = "SELECT Orderdate, " . $op1 . " FROM Orders";



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks, however:

This goes through silently
$query = "SELECT Orderdate, '$op1' FROM Orders";

This has an error report:

$query = "SELECT Orderdate, " . $op1 . " FROM Orders";

Problem with query: SELECT Orderdate, FROM Orders
You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right
syntax to use near 'FROM Orders' at line 1

Any idea why? Thanks
 
C'mon, you can do better than that.

Your own post includes:

Problem with query: SELECT Orderdate, FROM Orders
You have an error in your SQL syntax; check the manual

where is the value from $op1 in the query "SELECT Orderdate, FROM Orders"?



Want the best answers? Ask the best questions! TANSTAAFL!
 
Hello Sleipnir, thanks.

The code is on a form which gets posted to from a previous page. The code is:

<?
session_start()
?>
<?

$op1=$_SESSION['OP1'];
print $op1;

$username="root";
// $password="password";
$database="";
mysql_connect(localhost,$username);
//$query = "SELECT Orderdate, '$op1' FROM Orders";
$query = "SELECT Orderdate, " . $op1 . " FROM Orders";

@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query($query) or die('Problem with query: ' . $query . '<br>'. mysql_error());
$numrows=mysql_numrows($result);

Hope that makes it easier. Regards
 
I'm sorry, I was unclear.

When I said, "C'mon, you can do better than that." I mean, "You should have the skills to debug this one yourself"

If you need basic help, try faq434-2999



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks, I'll just bury my head in the manual for a few hours. So few lines of code, obviously a simple answer somewhere as I am only trying to replace the word OrderNumber in a query that worked perfectly before with the word typed in.
 
So few lines of code, obviously a simple answer
And yet, your first instinct is to post your question here. You need to learn to debug this type of code, and this is a very good place to start.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Just a hint: although you're printing the variable $op1, you're not checking a value exists before plugging it into your query.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
I'll add a hint;

Problem with query: SELECT Orderdate, [tab] FROM Orders
You have an error in your SQL syntax; check the manual


Help me $op1-kanobe you're my only hope....

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top