View Single Post
  #1 (permalink)  
Old 06-06-2008, 15:27
Ben Ben is online now
Premium Member
 
Join Date: Jan 2007
Location: setcookie()
Posts: 1,205
Send a message via AIM to Ben Send a message via MSN to Ben Send a message via Yahoo to Ben
Default Using mysql_fetch_object() function in PHP

So what does mysql_fetch_object() do? It fetches a result row (or field) as an object. Now imagine you had a MySQL table named users and a field/row named username. If you wanted to display these usernames in descending order, you'd first of all create a MySQL query:

PHP Code:
<?php
$sql
=mysql_query("SELECT * FROM `users`");
while(
$object=mysql_fetch_object($sql)) {
echo 
$object->username."<br />";
}
?>
Okay, so basically mysql_query() is to create a new MySQL query to interact with the database. If you know a little SQL, SELECT * means select ALL (* = wildcard) FROM `users` table.

The while() loop - well, take a wild guess. "Loops" are continual chains of events that never really stop. Or, until the end of time. But in this case it'll loop all of the values in that specific field until the end of all values in that field .

$object will be the object variable of that MySQL query - so therefore, $object->username would select the username field in that users table. You can't select anything outside of the users table unless you specify that in a seperate MySQL query. You can learn more about SQL Server and find tutorials here.

So there you are . Remember you can get more less detailed information about a function at the PHP website hosting by going to php.net/function_name (replacing function_name with, for example, mysql_fetch_object).

Good luck!

References you may find useful:

PHP: mysql_real_escape_string - Manual
PHP: mysql_fetch_array - Manual
PHP: mysql_fetch_assoc - Manual
__________________
dynBB.com PHP Tutorials & Webmaster Help Forums - need help? find PHP tutorials, help & more @ dynBB.com forums! CSS, PHP, MySQL & more!

Last edited by Ben; 06-06-2008 at 15:29.
Reply With Quote