php mysql query issues

Codes here !

Moderators: macek, egami, gesf

clint6998
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Sep 19, 2012 8:20 am

php mysql query issues

Postby clint6998 » Wed Sep 19, 2012 8:22 am

not sure it this is the right place but here is my problem. I need to list a query in a while loop. My query consists of checking two different tables, and then matching items from them. In this case, I need to pull category information from one category and category descriptions from another.

Here is what I have that is not working.

Code: Select all

//get parent categories
$categories_results = mysql_query("SELECT `categories_id`,`parent_id` FROM `categories` WHERE `parent_id` = '0' and `categories_status` = '1'") or die(mysql_error());


Code: Select all

//get category descriptions
$categories_description_results = mysql_query("SELECT `categories_id`, `categories_name` FROM `categories_description` WHERE `categories_id` = '$categories_results[categories_id]'") or die(mysql_error());



categories_description_results is the final query I need to run my loop.

My output code looks like this:

Code: Select all

while ($row_categories_description_results = mysql_fetch_array($categories_description_results)) { ?>
//some html code goes here
<?php echo $row_categories_description_results['categories_name'];?>
//some html code goes here
<?php  /* eof if 1 */ } /* eof get parent category names and list */ ?>


Any ideas on what I did wrong. Any help would be appreciated.

Thanks,

Clint

NigelRen
php-forum Active User
php-forum Active User
Posts: 450
Joined: Fri Aug 05, 2011 9:53 am

Re: php mysql query issues

Postby NigelRen » Wed Sep 19, 2012 11:00 pm

One thing which will probably cause odd results is
//get category descriptions

Code: Select all

$categories_description_results = mysql_query("SELECT `categories_id`, `categories_name` FROM `categories_description` WHERE `categories_id` = '$categories_results[categories_id]'") or die(mysql_error());

When you want to access the results of a query - you have to use quotes round the name of the field - which is what you did later, but not in this query. Try changing it to something like
//get category descriptions

Code: Select all

$categories_description_results = mysql_query("SELECT `categories_id`, `categories_name` FROM `categories_description` WHERE `categories_id` = '".$categories_results['categories_id']."'") or die(mysql_error());


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 2 guests