Solved

mysql php data fetch

Posted on 2011-02-15
6
436 Views
Last Modified: 2014-09-14
Dear Experts,

I think there is a way to fetch the data from the database without using "while"
how should I use it if it is a one row of data.

$result3 = mysql_query($sql3)
or die(mysql_error());  

while($row = mysql_fetch_array( $result3 )) {
      // Print out the contents of each row into a table
      $datacity = $row['rt_value'];
      echo $datacity;
      
       }
0
Comment
Question by:Braveheartli
6 Comments
 
LVL 9

Assisted Solution

by:AriMc
AriMc earned 100 total points
ID: 34904332
You should still check for errors of mysql_fetch_array, so there is not much change but replace while with "if" and you'll only get one row:

$result3 = mysql_query($sql3)
or die(mysql_error());  

if ($row = mysql_fetch_array( $result3 ))
{
      // Print out the contents of each row into a table
      $datacity = $row['rt_value'];
      echo $datacity;  
}
0
 
LVL 11

Assisted Solution

by:SAMIR BHOGAYTA
SAMIR BHOGAYTA earned 100 total points
ID: 34904610
0
 
LVL 13

Assisted Solution

by:dsmile
dsmile earned 100 total points
ID: 34904813
To get single record, you can use mysql_result
$result3 = mysql_query($sql3)
or die(mysql_error());  

$row = mysql_result($result3, 0); 

$datacity = $row['rt_value'];
echo $datacity;

Open in new window

0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 14

Assisted Solution

by:dejaanbu
dejaanbu earned 100 total points
ID: 34905160
U can use mysql_fetch_object function.

$result3 = mysql_query($sql3) or die(mysql_error());  
$obj =mysql_fetch_object($result3);

echo $obj->YOURLCOLUM1;
echo $obj->COLUMN2;

Open in new window


hope this helps
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 100 total points
ID: 34907283
If your query returns more than zero rows of data, you want to use while() for processing the row(s).  If you want to retrieve only one row of data, you can add the LIMIT 1 clause to your query.  There is no need or advantage to write different PHP code for one or more rows.  In fact, there is the disadvantage that you would have more code to maintain, you could not copy code, etc.
0
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 34914916
thank you
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now