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
https://forums.phpfreaks.com/topic/181829-solved-fetching-row-with-get-method/
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.

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

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.

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?

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>';

?>

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.