By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,043 Members | 1,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,043 IT Pros & Developers. It's quick & easy.

Looping from a fetcharray()

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.