$_GET[id]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Qaid
Forum Commoner
Posts: 33
Joined: Wed Feb 15, 2006 12:04 pm

$_GET[id]

Post by Qaid »

Hi,

When you browse my website I liked to have this while browsing domain.com/index.php?id=1

When accessing id?=1 it will give me the url from the table ‘2k5’ defined by the id. So when the page has generated I can just use this <? echo $print_id ?> to display the url for id?=1.

This is my code so far…

Code: Select all

$connection = mysql_pconnect("$host","$username","$password") or die ("Error Connecting the Database ".mysql_error."");
$db = mysql_select_db("$database", $connection) or die("Could not select the SQL Database.");
//connects to database

$result = mysql_query("SELECT url FROM 2k5 WHERE id='$_GET[id]'");
if (!$result) {
die('Error:' . mysql_error());
}
while ($result=mysql_fetch_array($result)){ 
$print_id = $result[????];
}
Can anyone please give me some advice? Please note that im not a hardcore coder! :)

Thanks..
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

well,to use the $print_id,you would first have to set up the variable as a value( like $_GET['variable']) You would then need to use

echo '$print_id';
Qaid
Forum Commoner
Posts: 33
Joined: Wed Feb 15, 2006 12:04 pm

Post by Qaid »

Please, can you make a little code for me, I've had looked at this code for some while, and now im really sick and tired of it, if you now what i mean? :)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

// you don't need double quotes to expand variables, also mysql_error is a function so it needs to be mysql_error()
$connection = mysql_pconnect($host,$username,$password) or die ("Error Connecting the Database ".mysql_error());
// You do not need quotes on the variable again
$db = mysql_select_db($database, $connection) or die("Could not select the SQL Database.");
//connects to database
// You need to do it like this:
$result = mysql_query('SELECT url FROM 2k5 WHERE id=\''.(int)$_GET['id'].'\' limit 1');
if (!$result) {
die('Error:' . mysql_error());
}
while ($result=mysql_fetch_array($result)){
$print_id = $result[????];
}
Please use [php] tags from now on
Last edited by josh on Wed Feb 15, 2006 12:28 pm, edited 2 times in total.
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

ill respond later,ciz im in the skool
Qaid
Forum Commoner
Posts: 33
Joined: Wed Feb 15, 2006 12:04 pm

Post by Qaid »

jshpro2 wrote:

Code: Select all

// you don't need double quotes to expand variables, also mysql_error is a function so it needs to be mysql_error()
$connection = mysql_pconnect($host,$username,$password) or die ("Error Connecting the Database ".mysql_error());
// You do not need quotes on the variable again
$db = mysql_select_db($database, $connection) or die("Could not select the SQL Database.");
//connects to database
// You need to do it like this:
$result = mysql_query('SELECT url FROM 2k5 WHERE id=\''.(int)$_GET['id'].'\' limit 1');
if (!$result) {
die('Error:' . mysql_error());
}
while ($result=mysql_fetch_array($result)){
$print_id = $result[????];
}
Please use [php] tags from now on
Sorry about the

Code: Select all

,
What should be in this $result[????]
Just ????

Thanks..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$result['url'] which is the only field you're selecting...

You may want to know about var_export(), var_dump() and print_r()
Qaid
Forum Commoner
Posts: 33
Joined: Wed Feb 15, 2006 12:04 pm

Post by Qaid »

The last question in this topic..

I'll get this warning:

Code: Select all

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /path/to/file/ on line xx
Thanks everyone for the quick answers.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql_error() will tell you what is wrong with your query.
Post Reply