Solved

mysql php data fetch

Posted on 2011-02-15
6
439 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
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.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 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