problem with select statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

problem with select statement

Post by timoteo »

does anyone know what is wrong with this select statement?
$querynew = "SELECT DISTINCT recoentryid FROM businessdetails WHERE recoentryid IN('1','73','20','39','40','41','48','50','71','72') AND (city IN('queretaro') OR city LIKE '%queretaro%')
this gives no result yet if I write
SELECT DISTINCT recoentryid FROM businessdetails WHERE (city IN('queretaro') OR city LIKE '%queretaro%')
I get Array ( [0] => 58 [1] => 67 [2] => 73 ). And if I write:
SELECT DISTINCT recoentryid FROM businessdetails WHERE recoentryid IN('1','73','20','39','40','41','48','50','71','72')
I get Array ( [0] => 73 )
Therefore by my logic the first statement above should give Array ( [0] => 73 ) but it gives nothing..... why?
ouchiko
Forum Commoner
Posts: 35
Joined: Sun Oct 09, 2011 6:54 pm
Location: London

Re: problem with select statement

Post by ouchiko »

I tried your top query - it works fine.

Try running through a mysql client [HeidiSQL or some such] and make sure the query work then look into the PHP code - which I suspect will be the problem area .. as the query looks a-ok.
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: problem with select statement

Post by aravona »

If you're having problems with a query, best to eliminate the obvious first and try the query in your MySQL command line, or in phpmyadmin or some other such control panel.
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: problem with select statement

Post by timoteo »

thanks for the tips - checked it in php myadmin - works perfect. So what's wrong - apparently the array is not recording the first result therefore no result when there is only one. this is what comes next in my code:

Code: Select all

$resultnew = mysql_query($querynew, $recommendingpeople) or die(mysql_error());
$rownew = mysql_fetch_array($resultnew);


if(isset($rownew['recoentryid'])) {

while ($rownew = mysql_fetch_array($resultnew)) {
   
    $ranew[] = $rownew['recoentryid'];
   }}
    
     $renew = array_unique($ranew);
echo $querynew;
print_r($renew);
die();
any ideas?
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: problem with select statement

Post by timoteo »

got it!!!!

Code: Select all

if(isset($rownew['recoentryid'])) {
$ranew[] = $rownew['recoentryid'];
while ($rownew = mysql_fetch_array($resultnew)) {
   
    $ranew[] = $rownew['recoentryid'];
   }}
    
     $renew = array_unique($ranew);
thank you for your help. I was missing the first result to my array before looping.
ouchiko
Forum Commoner
Posts: 35
Joined: Sun Oct 09, 2011 6:54 pm
Location: London

Re: problem with select statement

Post by ouchiko »

You'd be better off doing something like:

Code: Select all

while ( $row = @mysql_fetch_array($resultnew)) if ( $row ) $ranew[]=$row;
Still, glad it worked out
Post Reply