Jump to content

Display only one row from db with PHP


BrianM

Recommended Posts

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'];

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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! ;D

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.