Appending a variable to a SQL Statement

Ask about a PHP problem here.
Post Reply
lakc
Posts: 32
Joined: Fri Oct 21, 2011 6:05 pm

Appending a variable to a SQL Statement

Post by lakc »

I have a problem of appending a variable to be used within the SQL Statment:

$sqlOptions="SELECT options.optionid,options.optiontext,options.optionvalue FROM options,questions WHERE options.questionid="$row[questionid];

Whts the correct way to do it?

Here is the code:
<?php
//Prepare SQL Statement queryset
$sqlQuestion="SELECT questionid, questiontext FROM questions";
$resultQuestion = mysql_query($sqlQuestion);

// Write out the HTML for the page
?>
<html>
<body>
<form method="POST" action="process.php">
 <!-- Write out the question Text -->
 <table border="1" cellpadding="6">
 <?php
   
  while($row = mysql_fetch_array($resultQuestion ))
  {
  echo "<tr>";
  echo "<td width=525>" . $row['questiontext'] . "</td>";
  echo "Hello"; //<---code works fine upto here
  
	//Prepare statement to fetch options for each question as a queryset

   //----->The error is here<-----
    $sqlOptions="SELECT options.optionid,options.optiontext,options.optionvalue FROM options,questions WHERE options.questionid=".$row['questionid']. ; 
	
$resultOption = mysql_query($sqlOptions);

  	/*while($col = mysql_fetch_array($resultOption))
	{
		if($row['questionid']==1)

In SQL-T I hv used " ' " varname " ' " to use it within the SQL, in some +varname+,(but this is string concatenation).

Please enlighten me.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Appending a variable to a SQL Statement

Post by Temor »

$sqlOptions="SELECT options.optionid,options.optiontext,options.optionvalue FROM options,questions WHERE options.questionid=".$row['questionid']. ;
you're actually ending the SQL statement after WHERE options.questionid=

what you want to do is this:
$sqlOptions="SELECT options.optionid,options.optiontext,options.optionvalue FROM options,questions WHERE options.questionid='{$row['questionid']}'";
At least that's how I do it.
lakc
Posts: 32
Joined: Fri Oct 21, 2011 6:05 pm

Re: Appending a variable to a SQL Statement

Post by lakc »

Thank you again Temor.

May I ask another with regard to append/concatenation.

In this particular code, I want to display 2 queryset variables alongside each other in an inline-HTML code.
echo "<td> <input name= text type=radio value=1/>" .$col[firstname].+.$col[lastname]. "</td>";
What would be the correct syntax?
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Appending a variable to a SQL Statement

Post by Temor »

lakc wrote:Thank you again Temor.

May I ask another with regard to append/concatenation.

In this particular code, I want to display 2 queryset variables alongside each other in an inline-HTML code.

echo "<td> <input name= text type=radio value=1/>" .$col[firstname].+.$col[lastname]. "</td>";

What would be the correct syntax?
There are basically two ways ( that I know of anyway ) to do this.

First one is this:
echo "<td> <input name= text type=radio value=1/>".$array['key1']." ".$array['key2']." </td>";
And second one is closing the <?php tag before the html starts and then call php again when you need it. Like this:
<?php
php code here
?>

<td> <input name= text type=radio value=1/> <?php echo $array['key1']." ".$array['key2']; ?> </td>

<?php
More php code here
?>
Please not the only reason for me doing the whole double quote thingy between the variables is to get a space in there. If you don't need a space there you can skip that and just do this:
<td> <input name= text type=radio value=1/> <?php echo $array['key1'].$array['key2']; ?> </td>
or this:
echo "<td> <input name= text type=radio value=1/>".$array['key1'].$array['key2']." </td>";
I hope this was helpful and not just a long nonsensical piece of jibberish. I'm very ill and have little or no patience to actually read what I'm typing. Sorry for any errors.
lakc
Posts: 32
Joined: Fri Oct 21, 2011 6:05 pm

Re: Appending a variable to a SQL Statement

Post by lakc »

Sorry for the late appreciation. Hope u enjoyin the season leading to Xmas.
Post Reply