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

}

 

?>

Link to comment
Share on other sites

<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
Share on other sites

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.