Page 1 of 1

Using a table's ID to pull up a page

Posted: Thu Apr 22, 2010 4:36 am
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!

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

Posted: Thu Apr 22, 2010 5:05 am
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 :)

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

Posted: Thu Apr 22, 2010 5:27 am
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"?

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

Posted: Thu Apr 22, 2010 6:17 am
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 :)

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

Posted: Wed Apr 28, 2010 4:30 am
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.