Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you get just one cell to display from a MySQL db with PHP 1

Status
Not open for further replies.

raymundo

Technical User
Aug 14, 2000
14
US
Specifically, I have set up a login with session information set to the user Id. What I want to do is after logging in with the user id and the viewer is on a specific page how do you get just one cell from one row of a MySQL db to display it's contents in the page with the user id as the selector?

I have been beating my head for hours on this and am sure it is simple but can't seem to find the right commands. Any help would be greatly appreciated. [sig][/sig]
 
Hi Raymundo,
Use the mysql_fetch_array() function.

$SQL = "SELECT * FROM $table_name";
$result = mysql_query($SQL,$dbh);
$record = mysql_fetch_array($result);

$field1 = $record[field1_name];


and so on.

hope this is what you wanted.

Cheers [sig]<p>Ken<br><a href=mailto:admin@mysqlwebring.com>admin@mysqlwebring.com</a><br><a href= Webring</a><br>[/sig]
 
This is exaclty what I want to do! But devnull22's response did not work for me. I understand it up to this line:

$result = mysql_query($SQL,$dbh);

what is $dbh? Database header?

For example I want the MOCi integer from the Name = Robert row to print out (Robert is the first row in the database).

Here is what I tried, minus the connect information:
$SQL = &quot;SELECT * FROM mydatabase&quot;;
$result = mysql_query($SQL, MOCi);
$record = mysql_fetch_array($result);
$field1 = $record[Name]
echo $field1;

I get this error:

Parse error: parse error, unexpected T_ECHO

thanks
 
$dbh=mysql_connect(&quot;localhost:3306&quot;,&quot;username&quot;,&quot;password&quot;);
This creates the connection with the database,then you can
store or get database's datas.

Since raymundo saves the User ID with session,we should select the unique row.
$sql=&quot;select * from $table_name where ID='$userID'&quot;;
$res=mysql_query($sql,$dbh);
$record=mysql_fetch_array($res);

P.S.function mysql_fetch_row() can also work.
 
Here's a snippet that shows how I did this the other day...
I wanted to print only the field item_description from the database for a particular item and ignore all the fields before and after it:

<?php

$connection = @mysql_connect(&quot;localhost&quot;,&quot;&quot;,&quot;&quot;);
if ($linkID == FALSE)
{
print &quot;The connection to the server... hey, where did that server go?&quot;;
}
else
{

mysql_select_db(&quot;wellercatalog&quot;, $connection);

$whats = mysql_query(&quot;SELECT * FROM inventory WHERE item_id=1&quot;);

$row = mysql_fetch_object($whats);
print $row->item_description.&quot;<br />&quot;;

mysql_free_result($whats);

}
?>

This works great for me. But since I'm still always learning any criticims (or explanations why one method is better or worse than another) would be apprciated.

George K

I'd like George to reply by:
E-mail[ ] Phone[ ] Swallow- European[ ] African[ ]
 
People keep on missing the fact that PHP already has a very simple method for fetching just one cell. You don't need mysql_fetch_array. See
In fact, you can even nest the call, so it's a one-liner:
Code:
$mycell = mysql_result(mysql_query(&quot;SELECT this FROM that, WHERE something = something&quot;),0);

or for better coding style, debug it:
Code:
$query = &quot;SELECT this FROM that, WHERE something = 'theotherthing'&quot;;
$result = mysql_query($query);
if(!($mycell = mysql_result($result,0)))
{
   echo mysql_error($result);
}
else
{
   echo $mycell;
}
-------------------------------------------

&quot;Now, this might cause some discomfort...&quot;
(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top