?
Solved

mysql php data fetch

Posted on 2011-02-15
6
Medium Priority
?
454 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 9

Assisted Solution

by:AriMc
AriMc earned 400 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 400 total points
ID: 34904610
0
 
LVL 13

Assisted Solution

by:dsmile
dsmile earned 400 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 14

Assisted Solution

by:dejaanbu
dejaanbu earned 400 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 111

Accepted Solution

by:
Ray Paseur earned 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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…
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…
Suggested Courses

649 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