Solved

mysql php data fetch

Posted on 2011-02-15
6
433 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

12 Experts available now in Live!

Get 1:1 Help Now