Reply
Getting data from database
Old 10-29-2004, 04:44 PM Getting data from database
Junior Talker

Posts: 3
Hi,

I got this strange situation here...
In the following code, I'm just normally retrieving a data from my database.

Given $first_name should be 'php' according to the database, I expected a result output:
In database
php

But instead I get:
In database
Resource id #2


Anyone know what's wrong with my code? Don't know what Resource id #2 means.

Code:
//connect server and select database 
$link = mysql_connect($db_host,$db_user,$db_pass) or die ('Unable to connect to SQL server.'); 
mysql_select_db($db_name) or die ('Unable to locate' . $db_name . ' database'); 

$str='phpisgood'; 
$first_name = mysql_query("SELECT first_name FROM php_exercise2 WHERE username = '$str'"); 
if (mysql_num_rows($first_name)==0) { 
echo 'News type not in database.' . "\n"; 
} 
else{ 
echo "In database\n"; 
echo $first_name;
baby_g is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 10-29-2004, 08:00 PM
ACJavascript's Avatar
Humble Mod

Posts: 548
Location: CT, USA
Hey,

By using $first_name like that you are just grabbing the true|false result. You need to do this:

$result=mysql_query("SELECT first_name FROM php_exercise2 WHERE username = '$str'");

$row=mysql_fetch_array($result);

echo $row['first_name'];


In order to access data in a mysql database you need to access the array sent back from the query. in PHP you use the "mysql_fetch_array" function.

Hope it helps
__________________
CYTech-services.com - 100 Satisfied Customers - Custom Programming and Web Development
ACJavascript is offline
Reply With Quote
View Public Profile Visit ACJavascript's homepage!
 
Reply     « Reply to Getting data from database
 

Thread Tools

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

vB 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.12107 seconds with 12 queries