Solved

mysql php data fetch

Posted on 2011-02-15
6
438 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 109

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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.
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.

776 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