ram4nd Posted January 16, 2009 Share Posted January 16, 2009 I need to get some rows from mysql table. I have 2 tables: categories and subcategories. I need to get categorie and subcategories what are linked with that catagorie. I have table where are: subcategorie name and categorie_id. I wonder how can I get all those subcategories. When i tryed i got only the first subcategorie name. This code takes categore names and prints them all. $mysql = new mysql(); $num_rows1 = $mysql->doQueryC('SELECT * FROM category'); for($i = 0; $i <= $num_rows1; $i++) { $cat = $mysql->doQueryA('SELECT name FROM category WHERE id = ' . $i); echo $cat['name'] . '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/141067-get-rows-from-mysql-table/ Share on other sites More sharing options...
Zephyr_Pure Posted January 16, 2009 Share Posted January 16, 2009 I have table where are: subcategorie name and categorie_id. I wonder how can I get all those subcategories. When i tryed i got only the first subcategorie name. This code takes categore names and prints them all. $mysql = new mysql(); $num_rows1 = $mysql->doQueryC('SELECT * FROM category'); for($i = 0; $i <= $num_rows1; $i++) { $cat = $mysql->doQueryA('SELECT name FROM category WHERE id = ' . $i); echo $cat['name'] . '<br>'; } Instead of doing a num_rows and a for loop with a second MySQL statement, just do this: $mysql = new mysql(); $result = $mysql->doQueryA('SELECT id,name FROM category'); foreach ($result as $r) { echo $r['name']; // Then, do a second query to pull your subcategories... something like this: $result2 = $mysql->doQueryA('SELECT name FROM subcategorie WHERE categorie_id='.$r['id']); foreach ($result2 as $r2) { echo 'Subcategory: '.$r2['name']; } } Just kinda guessed at the subcategory table, since you didn't really describe the layout of the tables. Quote Link to comment https://forums.phpfreaks.com/topic/141067-get-rows-from-mysql-table/#findComment-738297 Share on other sites More sharing options...
ram4nd Posted January 16, 2009 Author Share Posted January 16, 2009 Shows couple of errors, I think if I copye my sql code to here than that's enought. function doQueryA($query) { $this->doConnect(); $result = mysql_fetch_assoc(mysql_query($query, $this->connect)); $this->doDisconnect(); return $result; } Quote Link to comment https://forums.phpfreaks.com/topic/141067-get-rows-from-mysql-table/#findComment-738302 Share on other sites More sharing options...
abdfahim Posted January 16, 2009 Share Posted January 16, 2009 ast make sure ur SQL is working fine. for that, use the following syntax, whenever u run any query mysql_query($query, $this->connect) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/141067-get-rows-from-mysql-table/#findComment-738318 Share on other sites More sharing options...
Zephyr_Pure Posted January 16, 2009 Share Posted January 16, 2009 Shows couple of errors, I think if I copye my sql code to here than that's enought. function doQueryA($query) { $this->doConnect(); $result = mysql_fetch_assoc(mysql_query($query, $this->connect)); $this->doDisconnect(); return $result; } You're not checking to see that the result actually contains a result row. Use a conditional statement to test that the $result has num_rows() > 0 before you fetch_assoc() it. Quote Link to comment https://forums.phpfreaks.com/topic/141067-get-rows-from-mysql-table/#findComment-738321 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.