Lee-Bartlett Posted September 17, 2008 Share Posted September 17, 2008 Ok i wanna grab certain feilds from the database and put them into a webpage they should look like this about widget corp (indented and in a list) history (indented and in a list) our mission products (indented and in a list) widget 2000 services atm im only getting about widget corp products services ok so the code can anyone see anything wrong with this ?? <?php $subject_set = mysql_query("SELECT * FROM subjects", $connect); if(!$subject_set) { die ("database query failed: " . mysql_error()); } while ($subject = mysql_fetch_array($subject_set)) { echo "<li>{$subject["menu_name"]}</li>"; /* Page table */ $page_set = mysql_query("SELECT * FROM tblpages WHERE subject_id = {$subject["id"]}", $connect); if(!$page_set) { die ("database query failed: " . mysql_error()); } echo "<ul class=\"tblpage\">"; while ($page = mysql_fetch_array($subject_set)) { echo "<li>{$page["menu_name"]}</li>"; } echo "</ul>"; } ?> Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/ Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 What to you see when you view the source of the page? Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643841 Share on other sites More sharing options...
Lee-Bartlett Posted September 17, 2008 Author Share Posted September 17, 2008 <html> <head> <title> widget corp </title> <link href="stylesheets/public.css" media="all" rel="stylesheet" type="text/css" /> </head <body> <div id="header"> <h1> Widget Corp </h1> </div> <div id="main"> <table id="structure"> <tr> <td id="navigation"> <li>Products</li><ul class="tblpage"><li>About Widget Corp</li><li>Services</li></ul> </td> <td id="pages"> <h2> Content Area</h2> </td> </tr> </table> </div> <div id="footer"> Copyright 2008, Widget Corp </div> </body </html> Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643852 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 OK, it's gotta be with the select statement. Try this for your select: "SELECT * FROM tblpages WHERE subject_id = {$subject['id']}" I just replaced your double quotes with singles around the id key. If that still doesn't work, try this: $q = "SELECT * FROM tblpages WHERE subject_id = {$subject['id']}"; echo "$q<br>"; $page_set = mysql_query($q, $connect); If you print the query, you can see if there's a problem with it. If you don't see a problem, try running the query manually. Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643856 Share on other sites More sharing options...
Lee-Bartlett Posted September 17, 2008 Author Share Posted September 17, 2008 if this helps, my $subject isnt blued/purpled coloured like all my other ones, im guessing it thinks its just text. there is one more like that, will that effect it ? Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643864 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 Show me all of your code, but use the code button with the # so it color-codes it. That will help with troubleshooting. Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643866 Share on other sites More sharing options...
Lee-Bartlett Posted September 17, 2008 Author Share Posted September 17, 2008 <?php $connect = mysql_connect("localhost", "removed", "removed"); if (!$connect) { die("database connection failed: " . mysql_error()); } $db_select = mysql_select_db("widget_corp", $connect); if (!$db_select) { die("database connection failed: " . mysql_error()); } ?> <?php require_once("includes/functions.php"); ?> <?php include("includes/header.php"); ?> <table id="structure"> <tr> <td id="navigation"> <?php $subject_set = mysql_query("SELECT * FROM subjects", $connect); if(!$subject_set) { die ("database query failed: " . mysql_error()); } while ($subject = mysql_fetch_array($subject_set)) { echo "<li>{$subject["menu_name"]}</li>"; /* Page table */ $page_set = mysql_query("SELECT * FROM tblpages WHERE subject_id = {$subject['id']}", $connect); if(!$page_set) { die ("database query failed: " . mysql_error()); } echo "<ul class=\"tblpage\">"; while ($page = mysql_fetch_array($subject_set)) { echo "<li>{$page["menu_name"]}</li>"; } echo "</ul>"; } ?> </td> <td id="pages"> <h2> Content Area</h2> </td> </tr> </table> <?php include("includes/footer.php"); ?> <?php mysql_close ($connect); ?> Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643868 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 OK, it's not color-coding your subject variables, because they are inside the quotes of the query string. That's OK. I would still suggest printing your query, see if it looks OK. If it doesn't look OK, post an example of it here and we'll fix it. If it does look OK, try running it against your DB manually and see if you get any results or errors from the DB. Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643871 Share on other sites More sharing options...
Lee-Bartlett Posted September 17, 2008 Author Share Posted September 17, 2008 im quite new to php, what do you mean by print out your query? Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643878 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 Yes. Replace this: <?php $page_set = mysql_query("SELECT * FROM tblpages WHERE subject_id = {$subject['id']}", $connect); ?> with this: <?php $q = "SELECT * FROM tblpages WHERE subject_id = {$subject['id']}"; echo "$q<br>"; $page_set = mysql_query($q, $connect); ?> Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643881 Share on other sites More sharing options...
Lee-Bartlett Posted September 17, 2008 Author Share Posted September 17, 2008 all it did was put SELECT * FROM tblpages WHERE subject_id = 6 on my page, just to clarify what im trying to do, the database is a relation database so 2 titles should go under 1, where there relations match and the other under the other 1 Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643892 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 Right, I understand, it's a loop within a loop. The problem is that it looks like your subqueries are not returning any results. If you manually run "SELECT * FROM tblpages WHERE subject_id = 6" on your DB, do you get any results or errors? Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643900 Share on other sites More sharing options...
Lee-Bartlett Posted September 17, 2008 Author Share Posted September 17, 2008 nope still has the same as before when i had {$subject['id'] Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643903 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 I don't think you understand what I'm saying. Are you using some program to admin your MySQL DB? Maybe MySQL Admin, or PHP MySQL Admin? If so, there's a query tool within those tools. Copy your "SELECT * FROM tblpages WHERE subject_id = 6" select statement and run it using the admin tool, NOT in your PHP code. I suspect that you're not getting any results from the DB and the PHP code is not the problem. Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643906 Share on other sites More sharing options...
Lee-Bartlett Posted September 17, 2008 Author Share Posted September 17, 2008 i got this You have to choose at least one column to display i filled in the collums, i think correctly Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643910 Share on other sites More sharing options...
Lee-Bartlett Posted September 17, 2008 Author Share Posted September 17, 2008 if i put one of on and, it shows the raltion numbers 1, 1, and 2 Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643913 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 So when you ran that select, you got at least one row and no errors? If so, what are the table's field names? Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643918 Share on other sites More sharing options...
Lee-Bartlett Posted September 17, 2008 Author Share Posted September 17, 2008 the row i got was subject_id at top of the query, feild names to main table is id subject_id menu_name position visible if u ment that, they have 3 rows inseted in by phpmyadmin Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643921 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 AAAAHHHHHH!!!!!!!! I just realized the problem. The mysql_fetch_array() function returns the rows in columns with numbered indexes, rather than named indexes. Try using the mysql_fetch_assoc() function instead, or replace $page["menu_name"] with $page["2"]. With mysql_fetch_array(), this would be the case: id = key 0 subject_id = key 1 menu_name = key 2 position = key 3 visible = key 4 Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643939 Share on other sites More sharing options...
Lee-Bartlett Posted September 17, 2008 Author Share Posted September 17, 2008 nop think im gonna re watch the lynda tutorials and make sure i nothing wrong, i copied his code in the end and still didnt work Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643942 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 How many rows does mysql_num_rows() return? Also, try doing a print_r($page) inside your loop. That will print the entire contents of the array and you can see what it's doing. Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643945 Share on other sites More sharing options...
Lee-Bartlett Posted September 17, 2008 Author Share Posted September 17, 2008 3 rows Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643962 Share on other sites More sharing options...
F1Fan Posted September 17, 2008 Share Posted September 17, 2008 OK. Now add this inside the second loop: <?php echo "<pre>"; print_r($page); echo "</pre>"; ?> Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643974 Share on other sites More sharing options...
Lee-Bartlett Posted September 17, 2008 Author Share Posted September 17, 2008 nothing, dw mate ill re do all the tuts, ty for help Link to comment https://forums.phpfreaks.com/topic/124657-grabbing-tables-from-a-database-need-help/#findComment-643978 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.