BrianM Posted July 4, 2008 Share Posted July 4, 2008 Would somebody please show me an example of a database connection to MySQL via PHP selecting and displaying one row from a table and not every row in it? I'm wanting to select only one row from a table, selected by it's "id" and display only that one row. And the id will be pulled from a query string using $_GET['id'] and storing that as a variable at the top of the script; ie. "../index.php?table=some_table&id=1" - $id = $_GET['id']; Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 4, 2008 Share Posted July 4, 2008 Since I've seen some of your posts, I know that you know how to connect to MySQL and stuff. You'll want a query like: $id = $_GET['id']; //Hopefully you'd do some checks first SELECT * FROM some_table WHERE id=$id; //That's the query Quote Link to comment Share on other sites More sharing options...
BrianM Posted July 4, 2008 Author Share Posted July 4, 2008 Yeah, I was going to say, while waiting I Googled this little example from w3schools.com SELECT * FROM person WHERE FirstName='Peter' Alright, well hopefully all will go well from here. Thank you very much for your help and the quick reply! Quote Link to comment Share on other sites More sharing options...
BrianM Posted July 4, 2008 Author Share Posted July 4, 2008 Okay, a little problem ... can't figure this one out. Here is my code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>MPS Project Tracking - by Brian Medley</title> </head> <?php mysql_connect('localhost', 'brian', 'some_pass') or die(mysql_error()); mysql_select_db('reports') or die(mysql_error()); $table = $_GET['table']; $id = $_GET['id']; $sql = mysql_query("SELECT * FROM `$table` WHERE id=`$id`"); while($row = mysql_fetch_array($sql)) { echo $row['date'] . " " . $row['report']; } ?> <body> </body> </html> And I get this error when I display the page: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\new_mps\report\edit_report.php on line 16 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 4, 2008 Share Posted July 4, 2008 $sql = mysql_query("SELECT * FROM `$table` WHERE id=`$id`") OR die(mysql_error()); You must have an SQL error, such as a bad table name or the lack of an ID column in said table. Quote Link to comment Share on other sites More sharing options...
BrianM Posted July 4, 2008 Author Share Posted July 4, 2008 Unknown column '1' in 'where clause' - that's the error I get. That doesn't make since, I have an 'id' column. Here is what my table looks like at the moment to give you a visual. id date report 1 07-04-08 test report. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 4, 2008 Share Posted July 4, 2008 Remove the ` ` around $id. This causes MySQL to treat it as a column name, and 1 is not a column name. Quote Link to comment Share on other sites More sharing options...
BrianM Posted July 4, 2008 Author Share Posted July 4, 2008 Ah, thank you again for your help! Learn something new everyday. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 4, 2008 Share Posted July 4, 2008 No problem. =) Please mark this topic as solved though. =P Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.