Jump to content

Need Help Using Mysql_Fetch_Assoc


eldan88

Recommended Posts

Hey,

 

I have a simple PHP website below that fetches the an item name through an id and displays it onto the broweser, via GET requests through links Below are just categories and and items that belong to the catgeories.

 

I have created a function called called get_item_by_id with an argument that will pass the item id from the $_GET super global located on the find selected page. (The code is under the comment "Find Selected Page")

 

 

When I click on any of the item link it returns the text "m" for each link I click on. But the function get_category_by_id() works perfectly fine.

Can anyone please help me resolve this. Below is the code, and attached is a screenshot of what I am getting when clicking on an item name.

 

<?php require_once("includes/connection.php"); ?>

<?php
// This function below is written to confirm if a query has been successfully made
function confirm_query($result){// 1 variable is being past
if(!$result) { // If the arguemnt variable has not made a successful query
die ("Database Query Failed " . mysql_error()); // The die and display mysql_error
}
}
/* The function below is a function that is passed into the
find selected page below. Its passes in the category ID into the argument
and then uses a fetch array to fetch to value from the catagories table.
*/
function get_category_by_id($categoryid) {
global $connection;//passed in from the connection file
$query = "SELECT * FROM catagories ";
$query .= " WHERE id = {$categoryid} ";
$query .= "LIMIT 1 ";// Limit 1 to make sure we get 1 result.
$result = mysql_query($query, $connection);
confirm_query($result);
if ($fetched_category = mysql_fetch_assoc($result)) {
return $fetched_category;
} else {
return NULL;
  }
 }

 /* The function below acts as the same function above but uses the fetch_array for the items table instead to fetch the items for the catgeory. 
*/
function get_item_by_id($pageid) {
 global $connection;//passed in from the connection file
$query = "SELECT * FROM items ";
$query .= "WHERE id= {$pageid} ";
$query .= "LIMIT 1 ";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
if ($fetched_pages = mysql_fetch_assoc) {
return $fetched_pages; 
} else {
return NULL;
}



}

//*********** FIND SELECTED PAGE *************//
if(isset($_GET['category_id'])) // This grabs the category_id from the URL from $categories_fetch_array below.
{$sel_category = get_category_by_id($_GET['category_id']); // If it is set then $sel_category will be assigned to the category ID
$sel_items = NULL;}// If somone click on a category then $sel_items will be set to NULL
elseif (isset($_GET['item_id'])) // This grabs the item_id number from the $items_fetch_array below.
{$sel_items = get_item_by_id($_GET['item_id']); // Then the ID gets assigned to the vairbale $sel_items
$sel_category = NULL;} // If $sel_items get selected then $sel_category get set to NULL
else {
//Set both pages to NULL if either get selected.
$sel_items = NULL;
$sel_category = NULL;
}
//*********** END OF FIND SELECTED PAGE*******//
?>

<?php
// Return data from the DB
$category_query = "SELECT * FROM catagories ";
$category_result = mysql_query($category_query,$connection);
if(!$category_result) {
die("mySQL DB read failed" . mysql_error());
}
//use the return data
while($categories_fetch_array = mysql_fetch_assoc($category_result)) {
echo "<b>Catgeories:</b>" ."<a href=\"content.php?category_id=" . urlencode($categories_fetch_array['id']) . "\">" . $categories_fetch_array['category_name'] . "</a>" . "<br />"; // Links get outputed here


$items_query = "SELECT * FROM items WHERE category_id = {$categories_fetch_array["id"]}";
$items_result = mysql_query($items_query, $connection);
if (!$items_result) {
die("mysql could not make a query to the items table" . mysql_error()); }// end of  if (!$items_result) {
 while ($items_fetch_array = mysql_fetch_assoc($items_result)) {
// Links get outputed here
echo "<b>Items:</b>" . "<a href=\"content.php?item_id=" . urlencode($items_fetch_array['id']) . "\">" . $items_fetch_array['item_name'] . "</a>" . "<br />";
 }
}// End of while statment?>
<p>
<?php
// Text for the selected links get outputed here. 
echo $sel_category['category_name'];// This varible echos $_GET['category_id']
echo $sel_items['item_name']; // This varibale echo's out the $_GET['item_name']
?>

<?php mysql_close($connection); ?>

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.