|
|
 |
mysql_fetch_object (PHP 3, PHP 4, PHP 5) mysql_fetch_object -- Fetch a result row as an object Descriptionobject mysql_fetch_object ( resource result )
Returns an object with properties that correspond to the fetched row.
Return Values
Returns an object with properties that correspond to the
fetched row, or FALSE if there are no more rows.
mysql_fetch_row() fetches one row of data from
the result associated with the specified result identifier. The
row is returned as an array. Each result column is stored in an
array offset, starting at offset 0.
Examples
Example 1. mysql_fetch_object() example |
<?php
mysql_connect("hostname", "user", "password");
mysql_select_db("mydb");
$result = mysql_query("select * from mytable");
while ($row = mysql_fetch_object($result)) {
echo $row->user_id;
echo $row->fullname;
}
mysql_free_result($result);
?>
|
|
Example 2. mysql_fetch_object() example |
<?php
$row = mysql_fetch_object($result);
echo $row->field;
?>
|
|
NotesNote:
mysql_fetch_object() is similar to
mysql_fetch_array(), with one difference - an
object is returned, instead of an array. Indirectly, that means
that you can only access the data by the field names, and not by
their offsets (numbers are illegal property names).
Note: Field names returned by this function
are case-sensitive.
Note: This function sets NULL fields to
PHP NULL value.
User Contributed Notes
mysql_fetch_object
oops_oopsother at yahoo dot fr
19-May-2005 05:37
<?php
mysql_connect('localhost','root','') or die('Impossible de se conneter au serveur de base de sonnées');
mysql_select_db('publication') or die('Impossible de se connecter à la base de données publication');
$login = $_POST['userlogin'];
$pass = $_POST['userpass'];
$requete = mysql_query("select pass from auteur where auteur.login=$login");
while($ligne = mysql_fetch_object($requete))
{
if( $ligne->pass == md5($pass))
{
header('location: Redaction.php');
}
}
?>
This script doesn't work and the error message is
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in c:\program files\easyphp1-7\www\publication\connexion.php on line 17
q
09-Jul-2004 09:31
Some clarifications about previous notes concerning duplicate field names in a result set.
Consider the following relations:
TABLE_A(id, name)
TABLE_B(id, name, id_A)
Where TABLE_B.id_A references TABLE_A.id.
Now, if we join these tables like this: "SELECT * FROM TABLE_A, TABLE_B WHERE TABLE_A.id = TABLE_B.id_A", the result set looks like this: (id, name, id, name, id_A).
The behaviour of mysql_fetch_object on a result like this isn't documented here, but it seems obvious that some data will be lost because of the duplicate field names.
This can be avoided, as Eskil Kvalnes stated, by aliasing the field names. However, it is not necessary to alias all fields on a large table, as the following syntax is legal in MySQL: "SELECT *, TABLE_A.name AS name_a, TABLE_B.name AS name_b FROM TABLE_A, TABLE_B ...". This will produce a result set formatted like this: (id, name, id, name, id_A, name_a, name_b), and your data is saved. Hooray!
-q
rcoles at hotmail dot com
16-Oct-2003 04:11
In reviewing Eskil Kvalnes's comments (04-Mar-2003 11:59
When using table joins in a query you obviously need to name all the fields to make it work right with mysql_fetch_object()) I was left asking and, as a newbie, the reason why I'm here. I have a 28 field table. Ran SELECT * with a LEFT JOIN, etc and it appears to have worked on my test server without issue.
On further reading, MYSQL.COM has the following:
* It is not allowed to use a column alias in a WHERE clause, because the column value may not yet be determined when the WHERE clause is executed. See section A.5.4 Problems with alias.
* The FROM table_references clause indicates the tables from which to retrieve rows. If you name more than one table, you are performing a join. For information on join syntax, see section 6.4.1.1 JOIN Syntax. For each table specified, you may optionally specify an alias.
Aware of the fact there's a difference between tables and fields there appears to be confusion here somewhere.
zhundiak at comcast dot net
15-May-2003 08:44
Here is a wrapper that will allow specifying a class name.
function &db_fetch_object($set,$className)
{
/* Start by getting the usual array */
$row = mysql_fetch_assoc($set);
if ($row === null) return null;
/* Create the object */
$obj =& new $className();
/* Explode the array and set the objects's instance data */
foreach($row as $key => $value)
{
$obj->{$key} = $value;
}
return $obj;
}
class CPerson
{
function getFullName()
{
return $this->fname . ' ' . $this->lname;
}
}
$set = mysql_query('SELECT fname,lname FROM person');
while($person =& db_fetch_object($set,'CPerson'))
{
echo $person->getFullName();
}
Eskil Kvalnes
04-Mar-2003 12:59
When using table joins in a query you obviously need to name all the fields to make it work right with mysql_fetch_object().
kalleanka
30-Jan-2003 05:50
an addition to the previous...
for example getting members from a database:
function getAllMembers () {
$query = "SELECT * FROM people ORDER BY lname";
$result = mysql_query($query);
while($member = mysql_fetch_object($result)){
$members[] = $member;
}
return $members;
}
<br><br>
DON'T FORGET TO DECLARE THE ARRAY. If you try to cycle through members after the function has been called and you don't declare the array first you will get a horribly (HORRIBLY!) ugly error in your page. Also, if you try to add the object into the members array inside the while condition instead of in the while loop, you will generate one extra empty space in the array due to the last iteration/check.
spamme at aol dot com
16-Jan-2003 02:28
This is probably a little more elegant:
$sql = "SELECT * FROM table ";
$result = mysql_query($sql);
$data = array();
while ($row = mysql_fetch_object($result))
$data[] = $row;
allen at brooker dot gb dot net
19-Nov-2002 06:14
I found the above code to be buggy, not adding all the records to the array. This is the code I used instead:
$command = "SELECT * FROM table ";
$result = mysql_query($command, $link_id);
$num = mysql_num_rows($result);
$clickthru = array();
for ($i = 0; $i <= $num; $i++) {
$clickthru[$i] = array();
$clickthru[$i] = mysql_fetch_array($result);
}
Allen
Federico at Pomi dot net
15-Sep-2002 09:41
Be carefull:
the object returned will be a new/fresh object.
You can't use this function to replace some attributes of an existing object keeping the old ones.
Example:
class person
{
var $name;
var $surname;
var $doh;
function print()
{
print($name." ".$surname);
}
function get_from_db()
{
$res=query("select name, surname from ppl where... limit 1");
$this=mysql-fetch-object($res);
}
}
This won't work! When the method get_from_db() is executed, your old object will be destroyed... you won't find anything in the attribute $doh, and if you'll try to call the method print(), it will say it doesn't exist.
amackenz at cs dot uml dot edu
15-Jan-2001 01:03
When selecting with a count/sum, the field must be named.
select count(*) from users;
becomes
select count(*) as total from users;
This way the result can be referenced as:
$row->total;
| |