473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to loop through multiple rows returned by a database?

TheServant
1,168 Expert 1GB
I know that there was a post on this a little while back but I can't find it.
[PHP] $raw_data = mysql_query(" SELECT *
FROM mytable
") or die(mysql_error());
$data = mysql_fetch_array($raw_data);
$size_data = count( $data );

if ( $data != NULL ) {
for ( $i=0; $i<=($size_data - 1); $i=$i+3 ) {
echo( "No.: ".$data['id']."<br />"
."Title: ".$data['name']."<br />"
."Date Posted: ".$data['date']."<br />" );
}
}
[/PHP]

This works for one row of data (in MySQL), however when there is two rows of data it just prints the first row data table twice and the actual second row of data is not shown. Can someone tell me if it is my syntax that I have wrong? Also, I haven't used a foreach loop much in php and I can't figure out how to do it when there is multiple values (one key though). I think that should be the way to do this but I have still not managed it.

Thanks in advance.
Jun 10 '08 #1
2 1590
r035198x
13,262 8TB
1.) mysql_fetch_array returns only one row of data. You need to call mysql_fetch_array in a loop to get all the rows
2.)Where is the table that you want to populate? You need to be creating a new table row for every iteration in the loop.

Your loop should generally look something like
Expand|Select|Wrap|Line Numbers
  1. while($row = mysql_fetch_array($raw_data,)) {
  2. //create the rows here
  3. }
  4.  
Jun 10 '08 #2
TheServant
1,168 Expert 1GB
That's exactly what I was looking for, cheers mate, problem solved!
Jun 10 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: JP SIngh | last post by:
Hi All I run a query which gets data from two tables. I store in MainContracts Table ContractId InitiatedBy
4
by: Dave Edwards | last post by:
I understand that I can fill a datagrid with multiple queries, but I cannot figure out how to fill a dataset with the same query but run against multiple SQL servers, the query , table structure...
0
by: Mark Harrison | last post by:
HOWTO: Integrating Posgresql queries into an event loop. Mark Harrison mh@pixar.com May 27, 2004 Problem ------- The commonly used postgresql APIs will block until completed.
9
by: TC | last post by:
I need to design a system which represents multiple "projects" in SQL Server. Each project has the same data model, but is independent of all others. My inclination is to use one database to store...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
7
by: =?Utf-8?B?QVRT?= | last post by:
HOWTO Run multiple SQL statements from ASP/ADO to an Oracle 10g. Please help, I'm trying to write an ASP page to use ADO to run a long query against an Oracle 10g database, to create tables,...
15
Markus
by: Markus | last post by:
What i want to do: Get urls from the database and echo them out into a multiple columned table i.e. 4 pictures per row (recently uploaded table) MY problem is: I have, in my MySQL database,...
1
by: chocciies | last post by:
Hi! I want to delete multiple record via checkboxes. However, with no success. Is there sthg wrong with the value of my checkbox? Your help will be greatly appreciated. Thanks! <form...
482
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if this is more of a coldfusion problem or a javscript problem. So if i asked my question in the wrong section let me know an all move it to the correct place. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.