Jump to content

Single/Double quotation use witin SQL Stmts?


Recommended Posts

Hi ppl,

 

I have problem in preparing the sql statement which requires attribrute from another sql statement.

 

This is the scenario as in PHP code.

 

$sql1="SELECT attrb1 from table1";

 

$sql2="SELECT attrb2 fromt table2 where attrb3=".$sql['attrb1']";

 

What and where is the correct placement of these s'quotes or d'quotes should be?

 

Please help.

Thank you.

The reason for 2 sql stmts is to allow the page to behave dynamically in creating the table. :)

 

Ok i give up i tried every possible way now

 

Is it:

 

$sql2="SELECT attrb2 fromt table2 where attrb3=.$sql['attrb1'].";

 

$sql2="SELECT attrb2 fromt table2 where attrb3=".$sql[attrb1].;

 

Can u please give me the correct syntax please?

 

I read the doc in 5 secs there was nothing of this sort where i can append the attribute of the first sql queryset.

 

I guess no 1 has tried this ever b4!!!

 

Can u please give me the correct syntax please?

 

I read the doc in 5 secs there was nothing of this sort where i can append the attribute of the first sql queryset.

 

No, try reading it for longer.

 

[...]I guess no 1 has tried this ever b4!!!

 

I read your post in five seconds so I have no idea what you're trying to do, but I very much doubt what you just said is true.

I guess no 1 has tried this ever b4!!!

 

Yeah, right. You appear to be simply trying to append a value onto the end of a string. That is as simple as can be.

 

First off, will the first query return one or multiple results? You are using $sql['attrb1'], but you don't show where that is defined. Plus, you never stated any of the errors you have gotten - which you would have.

 

phporcaffeine gave you the right syntax to append the string and the variable. If that is not working it is because $sql['attrb1'] is a varchar value that itself needs to be enclosed in quotes or it does not have the value you expect it to.

 

But, as xyph stated you do not need two queries - just one:

 

$query = "SELECT attrb2 fromt table2 where attrb3 IN (SELECT attrb1 from table1)";

 

I read the doc in 5 secs there was nothing of this sort where i can append the attribute of the first sql queryset.

I guess no 1 has tried this ever b4!!!

 

Although I will tell you that reading any sort of manual in 5 seconds is utterly preposterous and that I think your statement about no one ever having tried this before is rude and absurd (considering the heavyweight attention your post has got from some seriously sick LAMP (that's, Linux, Apache, MySQL and PHP) pros); I'll give you the answer (to your specific question, not that it is the best way to do it) just to satisfy myself.

 

$sql2="SELECT attrb2 fromt table2 where attrb3=".$sql[attrb1];

 

You had a period, at the end, right before the semi-colon.  I knew that because I have tried this before, thousands of times perhaps.

 

To all,

 

OMG, Sorry Sorry Sorry I just read my post after waking up, its horrible wht I have posted last, as I was in state of sleep looking for a quick fix, hence I did nt want to read that article in whole and let that make me go early to sleep. I do respect the fact it requires patience and time, but I am in a worse pit of snake problems which i do nt want to go into personal details.

 

Should have paid more attention to the error

 

 

$sql2="SELECT attrb2 fromt table2 where attrb3=.$sql['attrb1'].";

$sql2="SELECT attrb2 fromt table2 where attrb3=".$sql[attrb1].;

 

 

I guess i was doing c+v in dreamland: It should read as:

 

$sql="SELECT attrb1 FROM table1";  //attrib1 coltype is int

 

$sql2="SELECT attrib2 FROM table2 WHERE attrib3=".$sql['attrb1'].;

 

@phporcaffeine: thank you and sorry

@mdjamato: yes the first query returns multiple queries. Secondly, no error msgs shown, simply a blank pg.

@Adam: i was looking for a quick fix, thru Ctrl+F.

 

Well with much reluctance I think i may hv to paste the code. here it is now:

 

//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";
  
//Prepare statement to fetch options for each question as a queryset

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

$resultOption = mysql_query($sqlOptions);

  	/*while($col = mysql_fetch_array($resultOption))
{
	if($row['questionid']==1)
                {
  			echo "<td> <input name= " .$row['questionid']. " type=radio value=1/>" .$col[optiontext]. "</td>";
		echo "<td> <input name= " .$row['questionid']. " type=radio value=0/>" .$col[optiontext]. "</td>";
                }
	else
	{
		echo "<td> <input name= " .$row['questionid']. " type=radio value=1/>" .$col[optiontext]. "</td>";
		echo "<td> <input name= " .$row['questionid']. " type=radio value=0/>" .$col[optiontext]. "</td>";
	}
}*/
  echo "</tr>";
  }
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.