473,480 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Looping from a fetcharray()

I have a SQL query that returns all company names containing a user
defined keyword. I store all this information using: $listings =
mysql_fetch_array($result);

That query works fine, as I am able to see the first company. What I
need to do, is loop through the array, displaying each company name. I
have the code to write the company name to the screen. I am just
trying to figure out how to do the loop. My beta attempt:

(Not shown is that I have used $NumRows to catch the number of rows
contained in $result)

for (x=0, x<=$NumRows, x++){
echo $listings['company_name'];
}

Any ideas will be greatly appreciated.

Jul 31 '07 #1
3 1464
Rik
On Wed, 01 Aug 2007 00:01:03 +0200, Jerim79 <my***@hotmail.comwrote:
I have a SQL query that returns all company names containing a user
defined keyword. I store all this information using: $listings =
mysql_fetch_array($result);
Nope: unless this is part of a loop, you store 1 row of the result in
$listings.
That query works fine, as I am able to see the first company. What I
need to do, is loop through the array, displaying each company name. I
have the code to write the company name to the screen. I am just
trying to figure out how to do the loop. My beta attempt:

(Not shown is that I have used $NumRows to catch the number of rows
contained in $result)

for (x=0, x<=$NumRows, x++){
echo $listings['company_name'];
}
Try the manual, an excellent example of widely used code:
<http://www.php.net/mysql_fetch_array>

while ($row = mysql_fetch_array($result)) {
print_r($row);
}

The fact is, mysql_query() returns a result-resource, mysql_fetch_array()
returns one row from that result, so you'll have to loop it untill $row
doesn't get an array from mysql_fetch_array() anymore (or earlier if the
code might ask for that).
--
Rik Wasmus
Jul 31 '07 #2
Jerim79 wrote:
I have a SQL query that returns all company names containing a user
defined keyword. I store all this information using: $listings =
mysql_fetch_array($result);

That query works fine, as I am able to see the first company. What I
need to do, is loop through the array, displaying each company name. I
have the code to write the company name to the screen. I am just
trying to figure out how to do the loop. My beta attempt:

(Not shown is that I have used $NumRows to catch the number of rows
contained in $result)

for (x=0, x<=$NumRows, x++){
echo $listings['company_name'];
}

Any ideas will be greatly appreciated.

while ($data = mysql_fetch_array($result.MYSQL_ASSOC))
{
$listings[] = $data['company_name'];
}

will put them all in the numerically indexed array $listings. So you'll
have:

$listings[0]['company_name']... $listings[n]['company_name']

Norm
Jul 31 '07 #3
On Aug 1, 8:01 am, Jerim79 <my...@hotmail.comwrote:
I have a SQL query that returns all company names containing a user
defined keyword. I store all this information using: $listings =
mysql_fetch_array($result);

That query works fine, as I am able to see the first company. What I
need to do, is loop through the array, displaying each company name. I
have the code to write the company name to the screen. I am just
trying to figure out how to do the loop. My beta attempt:

(Not shown is that I have used $NumRows to catch the number of rows
contained in $result)

for (x=0, x<=$NumRows, x++){
echo $listings['company_name'];
}

Any ideas will be greatly appreciated.

You are on the right path, although your syntax is incorrect.

<code>
// Assumes $result is MySQL Result Resource
// Assumes $NumRows has already been populated with number of return
rows
for($x = 0; $x < $NumRows; $x++) {
$listings = mysql_fetch_array($result);
echo $listings['company_name'];
}
</code>
Other method that is shorter:

<code>
// Assumes $result is MySQL Result Resource
while($listing = mysql_fetch_($result)) {
echo $listings['company_name'];
}
</code>

Hendri Kurniawan

Jul 31 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4036
by: kaptain kernel | last post by:
i've got a while loop thats iterating through a text file and pumping the contents into a database. the file is quite large (over 150mb). the looping causes my CPU load to race up to 100 per...
2
3976
by: ensnare | last post by:
Hi all, I'm using a database session handler and am looking to loop through data residing in the sessions table to make a 'Users online' array. I've found that using urldecode on the data...
2
2547
by: Ivo | last post by:
Hi, I have an audio file (.mid or .wav or .mp3) in an object element: <object id="snd" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"...
45
7391
by: Trevor Best | last post by:
I did a test once using a looping variable, first dimmed as Integer, then as Long. I found the Integer was quicker at looping. I knew this to be true back in the 16 bit days where the CPU's (80286)...
5
2406
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at...
1
5376
by: Diva | last post by:
Hi, I have a data grid in my application. It has 20 rows and I have set the page size as 5. I have a Submit button on my form and when I click on Submit, I need to loop through the rows in the...
5
3975
by: johnb41 | last post by:
I need to loop through a bunch of textbox controls on my form. The order of the loop is very important. For example, the top one must be read first, then the one below it, etc. My first...
0
1949
by: anthon | last post by:
Hi all - first post! anywho; I need to create a function for speeding up and down a looping clip. imagine a rotating object, triggered by an action, and slowly decreasing in speed, till it...
20
2796
by: Ifoel | last post by:
Hi all, Sorry im beginer in vb. I want making programm looping character or number. Just say i have numbers from 100 to 10000. just sample: Private Sub Timer1_Timer() if check1.value= 1...
2
2058
by: Davaa | last post by:
Dear all, I am a student making a MS Form application in C++. I would ask a question about "Timer". Sample code which I am developing is below. private: System::Void...
0
6915
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...
0
7054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7097
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6750
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4794
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4493
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3003
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2993
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.