Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP function to pull record from mySQL
Old 09-20-2005, 01:11 PM PHP function to pull record from mySQL
Joobz's Avatar
Extreme Talker

Posts: 208
Location: DFW Texas
Trades: 0
I need to know the correct syntax for pulling a record from a mySQL DB and display it on a page. I also need to know what code for pulling an entire list of records (just one row in the table like ID or Title, and displaying the list on a page, such as a list of articles' titles or maybe just the first 10 titles, etc....
I'm sure this is a simple process but I'm not good at writing phph code but I can sometimes read and modify some of it
__________________

Please login or register to view this content. Registration is FREE
Joobz is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-20-2005, 01:30 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
PHP has these things built in, but you need to know the right SQL query to send to the database to get back what you want.To get one row out (rows are essentially a 'record', columns or fields are things like ID, title and so on.) you need to have some way of identifying that row, usually with the value of that rows ID column.
PHP Code:
$id =// the row you are looking for
$result mysql_query("SELECT * FROM yourtable WHERE ID = $id");
$row mysql_fetch_assoc($result);
foreach(
$row as $fieldname => $value) {
  echo 
$fieldname " has value ".$value."<br />";

Here, the mysql_query function sends the sql query you give it out to the database. What comes back is $result, something called a 'resource identifier'. All this means in this case is that $result is a piece of a dtabase table (the bit returned by your query). You can't simply echo $result, because it is stored in some special way. There are some more functions to get the values, and here I use mysql_fetch_assoc to get an array from $result. This array will be laid out as follows:

Code:
Array( 
   'ID' => 6,
   'title' => 'Mr'
   'age' => 34
   'number_of_legs' => 2
)
The foreach loop just takes this array and prints it out in a nice way.

To get a whole column back, say to print out a list of first names, you need slightly different sql, and a more clever way of getting the data out of $result.
PHP Code:
$result mysql_query("SELECT first_name FROM yourtable");
echo 
"List of first names:";
while(
$row mysql_fetch_assoc($result)) {
  echo 
$row['first_name'] . "<br />";

If the sql has no WHERE clause, it gives you all the records. In this query we specify we only want the first_name column. A while loop is used to get the results out this time. This time $result has multiple rows, and we need to do each one in sequence. Each time you call mysql_fetch_assoc on the same resource identifier, it moves on to the next row automatically. If there are no more rows left, mysql_fetch_assoc returns false and the while loop ends.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)

Last edited by 0beron; 09-20-2005 at 01:35 PM..
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Reply     « Reply to PHP function to pull record from mySQL
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11736 seconds with 12 queries