lak Posted November 17, 2011 Share Posted November 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/251335-singledouble-quotation-use-witin-sql-stmts/ Share on other sites More sharing options...
phporcaffeine Posted November 17, 2011 Share Posted November 17, 2011 Firstly, $sql2="SELECT attrb2 fromt table2 where attrb3=" . $sql['attrb1']; Here is the part of the manual that deals with quoting: http://php.net/manual/en/language.types.string.php Quote Link to comment https://forums.phpfreaks.com/topic/251335-singledouble-quotation-use-witin-sql-stmts/#findComment-1289083 Share on other sites More sharing options...
xyph Posted November 17, 2011 Share Posted November 17, 2011 You should be able to do this with a single query. Quote Link to comment https://forums.phpfreaks.com/topic/251335-singledouble-quotation-use-witin-sql-stmts/#findComment-1289086 Share on other sites More sharing options...
lak Posted November 17, 2011 Author Share Posted November 17, 2011 Hold on will let u know if it works out fine. Quote Link to comment https://forums.phpfreaks.com/topic/251335-singledouble-quotation-use-witin-sql-stmts/#findComment-1289089 Share on other sites More sharing options...
lak Posted November 17, 2011 Author Share Posted November 17, 2011 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/251335-singledouble-quotation-use-witin-sql-stmts/#findComment-1289102 Share on other sites More sharing options...
Adam Posted November 17, 2011 Share Posted November 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/251335-singledouble-quotation-use-witin-sql-stmts/#findComment-1289108 Share on other sites More sharing options...
Psycho Posted November 17, 2011 Share Posted November 17, 2011 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)"; Quote Link to comment https://forums.phpfreaks.com/topic/251335-singledouble-quotation-use-witin-sql-stmts/#findComment-1289112 Share on other sites More sharing options...
phporcaffeine Posted November 17, 2011 Share Posted November 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/251335-singledouble-quotation-use-witin-sql-stmts/#findComment-1289118 Share on other sites More sharing options...
lak Posted November 18, 2011 Author Share Posted November 18, 2011 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/251335-singledouble-quotation-use-witin-sql-stmts/#findComment-1289203 Share on other sites More sharing options...
lak Posted November 18, 2011 Author Share Posted November 18, 2011 OK DONE. SOLVED. Quote Link to comment https://forums.phpfreaks.com/topic/251335-singledouble-quotation-use-witin-sql-stmts/#findComment-1289296 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.