View Full Version : display all fields by calling one field
loonatik
04-13-2003, 10:45 PM
I created a testing db with three fields: id, title, text, where id is auto increment. I have this on my .php page:
$dbh=mysql_connect ("localhost", "username", "password");
mysql_select_db ("testing_db");
$result = mysql_query("SELECT id FROM demoTable");
while ( $row = mysql_fetch_array($result) ) {
echo("<P>" . $row["id"] . "</P>");
}
this only displays all the id of each row of data. the way i want it to work is to call the id number (eg. page.php?id=1) and then display both the title and text defined for that id. i reckon this is a php syntax problem...would i hv more luck posting in the mysql forum?
thanks.
Galdo
04-13-2003, 11:09 PM
Well firstly you need to get the values of the title and text fields by adding them to the select.
$dbh=mysql_connect ("localhost", "username", "password");
mysql_select_db ("testing_db");
$result = mysql_query("SELECT id,title,text FROM demoTable");
while ( $row = mysql_fetch_array($result) ) {
echo("<P>" . $row["id"] . " " .$row["title"]. " " .$row["text"]."</P>");
}
Then you can use them as above.
missing-score
04-13-2003, 11:14 PM
Actually, it is in your SQL:
You have "SELECT id FROM demoTable"
Try
SELECT * FROM `demoTable` ( the ` are optional, but I always put them )
When you put SELECT id, it selects ONLY the id field, no other info is available, so your new code should look something like this:
<?php
$mdb = mysql_connect("host", "user", "pass");
mysql_select_db("Database", $mdb);
$info = "SELECT * FROM `demoTable` ORDER BY id ASC;"; // Added order by to ensure the info comes out in numerical order
while( $row = mysql_fetch_array($info) )
{
echo 'The Row with Id '.$row["id"].' Contains the following information<br />
<strong>Title: </strong>'.$row["title"].'<br />
<strong>Text: </strong>'.$row["text"].'<br /><br />';
}
?>
I just put that as an example, but that should give you the idea of how to do what you want...:thumbsup:
missing-score
04-13-2003, 11:15 PM
Oh, you got there first!
loonatik
04-14-2003, 12:10 AM
thanks. but i want a specific row to display depending on which id# is called. i've posted at the mysql forum.
Spookster
04-14-2003, 12:26 AM
If you are looking for a specific record then you need to specify what record to pull or what record meets the condition you are looking for. You can do that using a WHERE clause.
Might be helpful if you read up on SQL a bit. Here is a nice introduction to SQL:
http://www.sqlcourse.com/
This is more a SQL/MySQL problem so I will move this to the MySQL forum..
missing-score
04-14-2003, 12:27 AM
You shouldnt cross post!
Anyway, change the SQL query to:
SELECT * FROM `demoTable` WHERE id='WhateverId';
You can then use the info to display the row.
As id is auto increment, you will only ever get one row by calling a specific Id, so you dont need to use the while loop.
Hope this helps you.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.