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