help with a mysql query

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
uilleann
Forum Newbie
Posts: 10
Joined: Fri Jul 03, 2009 10:51 am

help with a mysql query

Post by uilleann »

hello, I am trying to create a form that will query mysql to result in defining a location(three in all: country, state, city). This is all based upon drop down bars and so far I have the country select already working. It is controlled through javascript and I am not to sure if this is my problem why as I contunue, nothing is working. (do not know any javascript).
This question is directed towards the query that is for the state drop down. $countryfix = $countryList[$selected country];. print_r($countryfix) works great!It keeps up with every country as selected, although when I run this, this will not work. I have tried print_r($cityList) it returns as ( [0] => ) . might someone send me in the right direction, it would be greatly appreciated.



$querystate = ("SELECT state FROM location_root WHERE country = '$countryfix'") or die("something messed up");
$resultstate = mysql_query($querystate);


while ($rowstate = mysql_fetch_array($resultstate))
{

$categoriesstate[] = $rowstate['state'];

}

$stateArray = array_unique($categoriesstate);


$cityList = ($stateArray) ;
return $cityList;
}
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Re: help with a mysql query

Post by Skara »

I'm not sure what your problem is, so I'll just talk about your code. Btw, put everything in [ php ] tags.

Code: Select all

   //mysql_error() will give you what's really wrong
    //Simpler to use one line
    $resultstate = mysql_query("SELECT state FROM location_root WHERE country = '$countryfix';") or die(mysql_error());
 
    while ($rowstate = mysql_fetch_array($resultstate)) {
        $categoriesstate[] = $rowstate['state'];
    }
 
    //Unless you're not posting part of the code, there's no need for all the extra variables here...
    return array_unique($categoriesstate);
}
also...

Code: Select all

$cityList = ($stateArray) ;  //what's with the () ????
//should be:
$cityList = $stateArray;
I don't quite understand why you're copying all the variables around.
In any case, the code above will work fine. If it doesn't, it's a problem somewhere else.

Try running something like this to track where something doesn't add up. If this data comes out correctly, it's a problem with the javascript. If it doesn't, it's a problem with the preceding code or the database.

Code: Select all

   echo $countryfix;
    $resultstate = mysql_query("SELECT state FROM location_root WHERE country = '$countryfix';") or die(mysql_error());
 
    while ($rowstate = mysql_fetch_array($resultstate)) {
        print_r($rowstate);
        $categoriesstate[] = $rowstate['state'];
    }
 
    print_r($categoriesstate);
    $categoriesstate = array_unique($categoriesstate);
    print_r($categoriesstate);
    return $categoriesstate;
}
uilleann
Forum Newbie
Posts: 10
Joined: Fri Jul 03, 2009 10:51 am

Re: help with a mysql query

Post by uilleann »

hey, Thank you. I have tried what you have given me and it works the same way before. This leaves me to believe that it is a javascipt issue. I will find a place to trouble shoot this. thank you!
Post Reply