Jump to content

Retrieving data from MySQL


jimmyelewis

Recommended Posts

Hey I've made a database in mysql which has several different fields one containing the name of the categories i have on the site. I'm trying to code the page so that it does the correct category name this is what i have.

Nav. Bar

[code]if ($results = mysql_query ($query))
    {
        //Retrieves and prints every record
        while ($row = mysql_fetch_array ($results))
        {
            echo '<li><a class="nav" href="' . 'index.php?cat_num=' . $row['cat_num'] . '">' . $row['cat_name'] . '</a></li>';
        }
    }
[/code]

Content of Category
[code]if (isset($_GET['cat_num'])) { //makes sure that the code is only executed when the option variable is present
$query = 'SELECT cat_name FROM admin_cat';
$results = mysql_query ($query);
$row = mysql_fetch_array ($results);
    switch ($_GET['cat_num']) {
        case '1':
            echo '<div class="title">' . $row['cat_name'] . '</div>';
        break;
        case '2':
            echo '<div class="title">' . $row['cat_name'] . '</div>';
        break;
        case '3':
            echo '<div class="title">' . $row['cat_name'] . '</div>';
        break;
    }
}
else {
            echo '<div class="title">' . $row['cat_name'] . '</div>';    }
[/code]

When i click on each link it shows the same category name and i'm not sure how to get it to display a certain field based on another field in the database. Thanks for any help.
Link to comment
https://forums.phpfreaks.com/topic/5096-retrieving-data-from-mysql/
Share on other sites

Put it in the WHERE clause:

[code]//makes sure that the code is only executed when the option variable is present
if (isset($_GET['cat_num'])) {
   $query = 'SELECT cat_name FROM admin_cat WHERE cat_num="' . $_GET['cat_num'] . '"';
   $results = mysql_query ($query);
   $row = mysql_fetch_array ($results);
   echo '<div class="title">' . $row['cat_name'] . '</div>';
} else {
   echo '<div class="title">No category selected.</div>';
}[/code]

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.