Forum Moderators: coopster

Message Too Old, No Replies

Pulling info from specific mysql

         

esanger21

10:05 pm on Mar 20, 2010 (gmt 0)

10+ Year Member



okay, i have a login script for my computer business so i can track customers computer serial numbers on the go and warranty information. I have a file cust_list.php which displays all the customers in the database. In the table that contains all of that info, next to it i have a view button. How do you make it pull from that specific customers name and display the rest of the information in the mysql about their computer and what not? Please help!

Matthew1980

10:29 pm on Mar 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there esanger21,

Welcome to the forum, basic syntax would be:-

$customerQuery = "SELECT * FROM `your_table_name` WHERE `customer_id` 'submitted_or_got_from_url_customer _id' LIMIT 1",

$QueryDb = mysql_query($customerQuery, $databaseConnectionReference) or die(mysql_error());

if($QueryDb)
{//If query successful start building the page
while ($getData = mysql_fetch_array($QueryDb))
{
//process data in this loop

}//close while

}//close if
else{
echo "Something went wrong, query was empty";
}
This would basically select everything from your table that is associated with the customer Id number. The LIMIT 1 would just make sure that the information returned would be specific to that ID number ;-p

Hopefully that's what you were meaning ;-p

Cheers,
MRb

rocknbil

8:19 pm on Mar 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Expanding (enhancing? :- ) on the previous . . .

How do you make it pull from that specific customers name and display the rest of the information in the mysql about their computer and what not?


Your "display all" page should look something like this.

<a href="detail.php?cust=1234">View</a>

Where "1234" is extracted from "customer_id" in my modified example.

Then in detail.php,

if (isset($_get['cust']) and is_numeric($_get['cust']) and ($_get['cust'] > 0)) {
$id = $_get['cust'];
}
else {
echo "<p>Oops. Invalid customer id.</p>";
exit;
}

A note here - since you can rely on an id being unique (if it's not, you're doing something wrong) no need for a limit or a while. It's one record. Since you die on mysql_query, there's no practical need for an if/else on whether $QueryDb is null.

$customerQuery = "select * from `your_table_name` where `customer_id`=$id;

Note that, numeric fields need no quotes, text fields/date fields do.


// Make your connection previously, then
$QueryDb = mysql_query($customerQuery) or die(mysql_error());
if ($getData = mysql_fetch_array($QueryDb)) {
echo '
<p>' . $getData['first_name'] . ' ' . $getData[last_name'] . '</p>
<p>' . $getData['email'] . '</p>
<p>' . $getData['phone'] . '</p>
'; // and so on
}//close if
else{
echo "Something went wrong, no results";
}