Using a table's ID to pull up a page

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
PoolWhirlz
Forum Newbie
Posts: 3
Joined: Thu Apr 22, 2010 4:29 am

Using a table's ID to pull up a page

Post by PoolWhirlz »

Greetings, all!

I have a database table with an individual ID number for each row. In a file such as "example.php", I'd like to pull up row #100 by entering the url "/example.php?id=100"

Would anyone please show me how to do this? I'd also like help with what sort of query I would need in order to pull up information for that row, such as "field1", and "field2" for me to display those fields in an html document. An example using the examples I provided in this post should be sufficient to help me accomplish what I need now and in the future. Much appreciation to anyone who can help me here!
thamizh
Forum Newbie
Posts: 18
Joined: Wed Apr 14, 2010 7:25 am

Re: Using a table's ID to pull up a page

Post by thamizh »

you can get the id using get method
$id = $_GET['id];
and the query should be like this
$sql = "select * my table_name where id='$id'";
$result = mysql_query($sql) or die ("Error in selection");
$result1 = $result['field1];
$result2 = $result['field2];

print the fields like this
echo $result1;
echo $result2;

url should be like this http://example.com/example.php?id=100

i hope this helps you :)
PoolWhirlz
Forum Newbie
Posts: 3
Joined: Thu Apr 22, 2010 4:29 am

Re: Using a table's ID to pull up a page

Post by PoolWhirlz »

Thanks, but I currently get this error:
Parse error: syntax error, unexpected T_VARIABLE, expecting ']' on line 6.

I think I fixed t by adding in a couple of apostrophes that were missing., but now I have this coming up:

[phpBB Debug] PHP Notice: in file (my file) on line 7: mysql_query() [function.mysql-query]: Access denied for user ''@'localhost' (using password: NO)
[phpBB Debug] PHP Notice: in file (my file) on line 7: mysql_query() [function.mysql-query]: A link to the server could not be established
Error in selection

It's something with this line:
$result = mysql_query($sql) or die ("Error in selection");

Also, how do I go about using variables such as {field} in the html to pull the info from "field1"?
thamizh
Forum Newbie
Posts: 18
Joined: Wed Apr 14, 2010 7:25 am

Re: Using a table's ID to pull up a page

Post by thamizh »

1: @localhost error - for this u should connect to the database
the db should be connected with proper user name and pwd

2. T_STring error comes because u were missing semicolon in the line
if possible pls post the code

for more help :)
PoolWhirlz
Forum Newbie
Posts: 3
Joined: Thu Apr 22, 2010 4:29 am

Re: Using a table's ID to pull up a page

Post by PoolWhirlz »

It is connected to the server, because I use the same DB connection for all other pages and even the page with the error is pulling up my session data, which is from the same exact database saying it can't connect to.
Post Reply