hno Posted May 12, 2010 Share Posted May 12, 2010 HI, I have write a script and it's make a query like this:"SELECT * FROM `questions` WHERE ( '1_1' = 1 or '1_1' = 2 ) and ( '1_2' = 1 ) and ( '1_3' = 1 or '1_3' = 2 ) and ( '1_4' = 1 or '1_4' = 2 ) " but When I run it with php it gives me this error:"You have an error in SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1" but When I copy the query to the phpmyadmin and run it manually it shoes me the rows. What's the problem?Why phpmyadmin run the query successfully but php couldn't run it ? Thanks Link to comment https://forums.phpfreaks.com/topic/201504-problem-with-executing-a-query/ Share on other sites More sharing options...
trq Posted May 12, 2010 Share Posted May 12, 2010 Can you post some relevant & actual code? Link to comment https://forums.phpfreaks.com/topic/201504-problem-with-executing-a-query/#findComment-1057161 Share on other sites More sharing options...
hno Posted May 12, 2010 Author Share Posted May 12, 2010 Can you post some relevant & actual code? $r=mysqli_query($conn,$q); if (mysqli_num_rows($r)>=1){ //some code } The error is from "mysqli_num_rows($r) " . Thanks Link to comment https://forums.phpfreaks.com/topic/201504-problem-with-executing-a-query/#findComment-1057168 Share on other sites More sharing options...
trq Posted May 12, 2010 Share Posted May 12, 2010 Relevant!! We need to see where you assign a value to $q Link to comment https://forums.phpfreaks.com/topic/201504-problem-with-executing-a-query/#findComment-1057170 Share on other sites More sharing options...
hno Posted May 12, 2010 Author Share Posted May 12, 2010 Relevant!! We need to see where you assign a value to $q I have a big form and I have to get many information . i get them by using this code for ($i=1;$i<=7;$i++){ for ($l=1;$l<=10;$l++){ $need_or=0; $ok=''; $k=''; $first_time=1; foreach($arr as $key=>$val){ if ($need_or)$k='or'; if ($_POST[$i .'_' . $l . '_' . $val]=='true'){ if ($first_time)$q.=' and ( '; $q.=" $k '". $i .'_' . $l . '\'' .' = ' .$key; $need_or=1; $first_time=0; } } if ($first_time==0)$q.=' ) '; } } $q{0}='';$q{1}='';$q{2}='';$q{3}=''; $q="SELECT * FROM `questions` WHERE $q "; And then I run the query by using those codes. Link to comment https://forums.phpfreaks.com/topic/201504-problem-with-executing-a-query/#findComment-1057175 Share on other sites More sharing options...
trq Posted May 12, 2010 Share Posted May 12, 2010 echo $q so we can see what it looks like. If your query is failing where it says, it is malformed. Link to comment https://forums.phpfreaks.com/topic/201504-problem-with-executing-a-query/#findComment-1057191 Share on other sites More sharing options...
hno Posted May 12, 2010 Author Share Posted May 12, 2010 echo $q so we can see what it looks like. If your query is failing where it says, it is malformed. I have posted a the query in the first post. It failed on the this line"if (mysqli_num_rows($r)>=1){" Link to comment https://forums.phpfreaks.com/topic/201504-problem-with-executing-a-query/#findComment-1057195 Share on other sites More sharing options...
trq Posted May 12, 2010 Share Posted May 12, 2010 If the query in your first post is actually what the query looks like then the problem is that field identifiers should NOT be surrounded by quotes. Link to comment https://forums.phpfreaks.com/topic/201504-problem-with-executing-a-query/#findComment-1057198 Share on other sites More sharing options...
Muddy_Funster Posted May 12, 2010 Share Posted May 12, 2010 Now, I know recycling variables is normaly not a bad thing....but $q is taking some serious abuse in that code. Link to comment https://forums.phpfreaks.com/topic/201504-problem-with-executing-a-query/#findComment-1057203 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.