Jump to content

Old Newbie select record/row with URL problem


phatmambo

Recommended Posts

I am learning PHP/MySQL and am modifying a tutorial to make a contact database.

 

All I want is to be able to make a web page for each contact(record) and display one record on each webpage. I want to use the URL feature:

 

"http://www.mysite.com/script.php?=item1" type of thing

 

Can any help me as I can't get it to work. I get no errors - it just doesnt display the info. I'm sure I cant be far off as I've only modified a working script ever so slightly.

 

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
?>

<?
$query="SELECT * FROM contacts WHERE id='$id'";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Phone</font></th>
<th><font face="Arial, Helvetica, sans-serif">Mobile</font></th>
<th><font face="Arial, Helvetica, sans-serif">Fax</font></th>
<th><font face="Arial, Helvetica, sans-serif">E-mail</font></th>
<th><font face="Arial, Helvetica, sans-serif">Website</font></th>
</tr>

<?
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$first $last"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$phone"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$mobile"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$fax"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="mailto:<? echo "$email"; ?>">E-mail</a></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="<? echo "$web"; ?>">Website</a></font></td>
</tr>
<?
++$i;
}
echo "</table>";


?>

Link to comment
Share on other sites

Lets say your page url was : "http://www.mysite.com/script.php?id=1"  where 1 is the id of the contact information in the database you want to retrieve.

 

first off you want to retrieve the "id" parameter which we have set to '1' in the url (script.php?id=1)

we do that by using $_GET['id'];  $_GET['urlParameterNameHere'] retrieves an url parameter from the url address, in this case id.

 

$id = $_GET['id']; // will be equal to '1' if the url ends in ?id=1  it would be '2' if it ended in ?id=2 etc.

$query="SELECT * FROM contacts WHERE id='$id'";
$result=mysql_query($query);

 

 

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.