|
|
 |
mysql_result (PHP 3, PHP 4, PHP 5) mysql_result -- Get result data Descriptionmixed mysql_result ( resource result, int row [, mixed field] )
Retrieves the contents of one cell from a MySQL result set.
When working on large result sets, you should consider using one
of the functions that fetch an entire row (specified below). As
these functions return the contents of multiple cells in one
function call, they're MUCH quicker than
mysql_result(). Also, note that specifying a
numeric offset for the field argument is much quicker than
specifying a fieldname or tablename.fieldname argument.
Parameters
-
result
The result resource that
is being evaluated. This result comes from a call to
mysql_query(). - row
The row number from the result that's being retrieved. Row numbers
start at 0.
- field
The name or offset of the field being retrieved.
It can be the field's offset, the field's name, or the field's table
dot field name (tablename.fieldname). If the column name has been
aliased ('select foo as bar from...'), use the alias instead of the
column name. If undefined, the first field is retrieved.
Return Values
The contents of one cell from a MySQL result set on success, or
FALSE on failure.
Examples
Example 1. mysql_result() example |
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query('SELECT name FROM work.employee');
if (!$result) {
die('Could not query:' . mysql_error());
}
echo mysql_result($result, 2); mysql_close($link);
?>
|
|
NotesNote:
Calls to mysql_result() should not be mixed
with calls to other functions that deal with the result set.
User Contributed Notes
mysql_result
Frank J.
05-May-2004 10:03
to make the function in the previous note usable for multiple rows, it's better to replace
while($row = mysql_fetch_array($result)) {
by
while($row = mysql_fetch_row($result)) {
tooley311 at yahoo dot com
21-Nov-2003 07:29
For those who use odbc and appreciate using the odbc_result_all utility function, here's the same thing, spelled out, for mysql. No error checking, sorry.
function mysql_result_all($result) {
echo '<table>';
for($i = 0; $i < mysql_num_fields($result); $i++) {
echo '<th>';
echo mysql_field_name($result,$i);
echo '</th>';
}
while($row = mysql_fetch_array($result)) {
echo '<tr>';
for($i = 0; $i < count($row); $i++) {
echo '<td>'.$row[$i].'</td>';
}
echo '</tr>';
}
echo '</table>';
}
till at klimpong dot com
13-Sep-2003 03:54
I frequently use mysql_result() together with mysql_fetch_array(), kinda like that... :-)
<?
$query="SELECT id, name, something_else FROM table";
$rawdb=mysql_query($query);
?>
<table>
<tr><td><?=mysql_result($rawdb, 0, "name")?></td></tr>
<?
mysql_data_seek($rawdb, 0);
while($array=mysql_fetch_array($rawdb, MYSQL_ASSOC)){
extract($array);
?>
<tr><td><?=$id?></td><td><?=$something_else?></td></tr>
<?
}
mysql_free_result($rawdb);
?>
</table>
Took me a while to figure out what to do with mysql_data_seek when you are using mysql_result and mysql_fetch_array on the same query. Hope it helps someone else. :-)
01-Sep-2003 06:16
For another way to display the results of a SELECT query in an HTML table, try:
------------------------------
<html>
<head>
<title>Enter Query</title>
</head>
<body>
<?php
$host = 'localhost';
$name = 'anonymous';
$pass = 'blargh';
function drawtable($qr) {
$rows = mysql_num_rows($qr);
$toreturn = "<table style=\"float: left;\" border=\"2\">\n";
$toreturn .= "<tr>\n";
for ($i=0; $i<mysql_num_fields($qr); $i++) {
$toreturn .= "\t<th>".mysql_field_name($qr,$i)."</th>\n";
}
$toreturn .= "</tr>\n";
for ($i=0; $i<$rows; $i++) {
$row = mysql_fetch_row($qr);
$cols = sizeof($row);
$toreturn .= "<tr>\n";
for ($x=0; $x < $cols; $x++) {
if ($row[$x]==NULL){
$row[$x]=' ';
}
$toreturn .= "\t<td>$row[$x]</td>\n";
}
$toreturn .= "</tr>\n";
}
$toreturn .= '</table>';
return $toreturn;
}
$q = stripslashes($_POST['q']);
if (empty($q)) {
$q = 'SELECT * FROM star_wars;';
}
$db = mysql_connect($db,$name,$pass);
if (!$db) {
echo 'Could not connect to '.$host;
exit;
}
mysql_select_db("test");
$result = mysql_query($q);
echo drawtable($result);
echo '<table>
<tr>
<td><form method="POST" action="temp.php">
<textarea name="q" rows="25" cols="80">'.$q.'</textarea><br />
<input type="submit" value="Submit" /><input type="reset" value="Reset" />
</form></td>
</tr>
</table>
</body>
</html>';
-------------------
This particular example also has an input form on it, just change the user,password, and default table (in the section where it checks if $q is empty) and run it. (It doesn't do any error checking, and only works correctly with SELECT statements...
raz0 at NOSPAM dot worldonline dot dk
23-Aug-2003 12:42
If you want to fetch the result from a mysql query similar to one of these two queries...
$query = mysql_query("SELECT COUNT(*) FROM table");
$query = mysql_query("SELECT LAST_INSERT_ID()");
... you would use mysql_result() like shown below to retrieve the output as an int.
$result = mysql_result($query, 0, 0);
admin at php dot net
13-Jul-2003 05:09
mysql_result() speed is not different mysql_fetch_row() on selecting 1 (one) row
omtelcom at lycos dot es
25-Apr-2003 03:19
|¯OMTELCOM_| from Tunja - Colombia!!
If you like to see your results in a table:
<html>
<title>|¯OMTELCOM_| Show a MySQL Query in a HTML Table |¯OMTELCOM_|</title>
<body>
<?php
$link = mysql_connect("localhost", "nobody");
mysql_select_db("mybd", $link);
$qry = mysql_query("SELECT * FROM agenda", $link);
?><table border="1" width="100%"><tr><?php
if (mysql_num_rows($qry) > 0) {
for ($i = 0; $i<mysql_num_fields($qry); $i++) {
echo "<td align=center><strong>" . mysql_field_name($qry, $i) . "</td>";
}
}
?></tr><?php
if (mysql_num_rows($qry) > 0) {
for ($j = 0; $j<mysql_num_rows($qry); $j++) {
?><tr><?php
for ($k = 0; $k<mysql_num_fields($qry); $k++) {
echo "<td align=center>" . mysql_result($qry,$j, $k) . "</td>";
}
?></tr><?php
}
}
?></table>
</body>
</html>
daendersNOSPAM at yahoo dot com
10-Jul-2002 06:25
If you came to this function like I did, looking for a function that grabs info from the result but does not increment it to the next result row, you are out of luck.
This function appears to move the result to the next row just like mysql_fetch_assoc() and the others.
arx777 at hotmail dot com
30-Jan-2002 08:35
So you would have something like this:
...
$rs = mysql_query("SELECT * from table1",$link);
if (mysql_num_rows($rs) > 0)
for ($i = 0; $i<mysql_num_fields($rs); $i++)
echo "Field [".mysql_field_name($rs, $i)."] - ". mysql_result($rs,0,$i)." // ";
-ACM
szii at sziisoft dot com
24-Sep-2000 01:38
It's pretty much self-explanitory. It returns a single "cell." You specify the row offset, and the column, and you get 1 value back. Usually you'll use something like mysql_fetch_array because you'll be interested in more than 1 cell at a time and it's inefficient to make multiple calls back and forth to the database. Make 1 call (fetch_arry/fetch_object) and then load from that
result data.
-Szii
| |