Jump to content

grabbing tables from a database, need help.


Lee-Bartlett

Recommended Posts

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>";

}

 

?>

<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>

 

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.

<?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);
?>

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.

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);
?>

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

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?

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.