jimmyelewis Posted March 16, 2006 Share Posted March 16, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/5096-retrieving-data-from-mysql/ Share on other sites More sharing options...
wickning1 Posted March 16, 2006 Share Posted March 16, 2006 Put it in the WHERE clause:[code]//makes sure that the code is only executed when the option variable is presentif (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] Quote Link to comment https://forums.phpfreaks.com/topic/5096-retrieving-data-from-mysql/#findComment-18077 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.