Jump to content

[SOLVED] Fetching Row with get method


eugene2009

Recommended Posts

Hey so if i have a table with all the rows numbered with a customerid.

 

Say I wanted to fetch each row on a seperate page with a get method.

 

how will I be able to do that? for instance i have rows with customer information. how will I be able to display them on seperate pages.

 

?customerid=343 and from there it will show all that customers information in a table.

 

Im not sure how to explain this. Please help.

Link to comment
Share on other sites

yes you can get the records by doing that. You would create a page customer_records.php(whatever you want). Then when the user visits that page you pass it there customerid so it would look like customer_records.php?id=12345. Then on that page you would do something like this:

 

<?php
$id = $_GET['id']; //gets the id from url

$query = "SELECT * FROM table WHERE id='$id'"; //select records with passed id
mysql_query($query); //perform query

 

Then you can do whatever you want with the data.

Link to comment
Share on other sites

thank you so much..

 

and another question.. say the previous page "customer_list.php" lists all the names of the customers. when you click on one of the names it will take them to customer_records.php well how exactly can I link all the names with the proper id's ? thank you for the help

Link to comment
Share on other sites

Kind of the same way but you would get the customer list. So it would be something like this:

 

<?php
$query = "SELECT * FROM customer_list"; //get the customer list
$result = mysql_query($query); //perform the query

//display the customer list
while($row = mysql_fetch_array($result)){
     echo '<a href="customer_records.php?id='.$row['customer_id'].'">'.$row['customer_name'].'</a>'; //create a link with the customers id as the $_GET['id']
}

 

So pretty much all you are doing is appending the customers id to the end of the url that you are taking them to. Hopefully that helps.

Link to comment
Share on other sites

i ran into a problem...

 

Just a quick narrowed down example..

$query = mysql_connect ($db_hostname, $db_username, $db_pass) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ($db_name);

$id = $_GET['id']; //gets the id from url

$query = "SELECT * FROM comments WHERE id='$id'"; //select records with passed id
$result = mysql_query($query); //perform the query
$row = mysql_fetch_row($result);


echo 'The customers name is, '.$row['name'].' and he made a purchase on, '.$row['date'].'.<BR>';

?>

 

What am I doing wrong?

Link to comment
Share on other sites

Your first line is assigning mysql_connect to $query. What is or isnt happening. Also try this:

 

mysql_connect ($db_hostname, $db_username, $db_pass) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ($db_name);

$id = $_GET['id']; //gets the id from url

$query = "SELECT * FROM comments WHERE id='$id'"; //select records with passed id
$result = mysql_query($query); //perform the query
$row = mysql_fetch_assoc($result);


echo 'The customers name is, '.$row['name'].' and he made a purchase on, '.$row['date'].'.<BR>';

?>

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.