Jump to content

Problem with executing a query


hno

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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.