Php reference id's with Mysql
Hey Im trying to created a dynamic php page that gets a varible from the url such as site.com/page.php?id=1 so that whatever the number is corresponds to the the id of the field in my table. And that it will print the appropriate field text but if there is no information filled in for the id is says "not found" or whatever. Thanks in advance.
Edit. Also the ids i want to look at are the three most recent.
Renegade posted this at 10:32 — 17th March 2005.
He has: 3,022 posts
Joined: Oct 2002
I don't get what you mean but... you can try this:
if(strlen($_GET['id']) == "0"){
echo "Not found";
}
RenardMF posted this at 17:36 — 17th March 2005.
They have: 13 posts
Joined: Mar 2005
ok sounds good for not found thing. But what i what i was trying to do was basically have a page that recieved this "id=x" numbers and obtained the correspoding information from the database entry with the same id number. I understand how to connect to the database and get the table information I'm after and Id assume the best way to do what i want is create and array and have it echo the correct information but hats what im not sure how to do.
ex. site.com/page.php?id=1 would grab the information in the array marked with 1 as the id and display a particular field within the table.
Thanks for your help.
RenardMF posted this at 22:59 — 17th March 2005.
They have: 13 posts
Joined: Mar 2005
Well i got the code to work:
<?php
include("./config.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT *
FROM films ORDER BY id DESC LIMIT 3";
$result=mysql_query($query);
if( !$result )
{
die('SQL Statement Error: '. mysql_error());
exit();
}
$line = array();
while( $row = mysql_fetch_array($result) )
{
$line[] = $row;
}
if(isset($_GET['f'])){
$film="$_GET[f]";
$name=mysql_result($result,$film,"name");
$synopsis=mysql_result($result,$film,"synopsis");
echo"<strong>";
echo"$name";
echo"</strong>";
echo"<br>";
echo"$synopsis";
}else{
echo "A film was not selected.";
}
mysql_close();
?>
But I'm not sure how to catch the error when an id over the amount that actually exists is given which gives "Mysql ...cant jump to table..etc"
Any ideas?
dk01 posted this at 00:16 — 18th March 2005.
He has: 516 posts
Joined: Mar 2002
The easiest thing to do is to query the database with the id and if it returns less than 1 result have it throw an error. You will need to put an @ in front of mysql_query() to supress the system error messages.
-dk
RenardMF posted this at 02:40 — 18th March 2005.
They have: 13 posts
Joined: Mar 2005
Ok do you mean like move the rest of the code up and have the query come once the id is recieved? What would be the code for that?
dk01 posted this at 06:41 — 18th March 2005.
He has: 516 posts
Joined: Mar 2002
<?php
include("./config.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT *
FROM films WHERE id='".$_GET['f']."' ORDER BY id DESC LIMIT 3";
$result=mysql_query($query);
if( !$result )
{
die('SQL Statement Error: '. mysql_error());
exit();
}
$line = array();
while( $row = @mysql_fetch_array($result) )
{
$line[] = $row;
}
if(@mysql_num_rows($result) > 0){
$film="$_GET[f]";
$name=mysql_result($result,$film,"name");
$synopsis=mysql_result($result,$film,"synopsis");
echo"<strong>";
echo"$name";
echo"</strong>";
echo"<br>";
echo"$synopsis";
}else{
echo "A film was not selected.";
}
mysql_close();
?>
Try that out. It's not tested but should work.
-dk
RenardMF posted this at 20:11 — 18th March 2005.
They have: 13 posts
Joined: Mar 2005
Hmm it still makes the code work but doesnt catch the error if and id is manually entered.
Warning: mysql_result(): Unable to jump to row 3 on MySQL result index 4
I guess its not too big of a deal since within the popup people don't have access to the address bar. But on that note what do you put in the onclick= part of the hyperlink to make it so the location isn't displayed in the title bar?
heartsaffection posted this at 07:04 — 21st April 2005.
They have: 26 posts
Joined: Apr 2005
Try this:
A popup window
Cheers,
Andy - http://www.heartsaffection.com your online guide to matchmaking and friend finder sites
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.