Click to See Complete Forum and Search --> : FOR loop not executed fully


yunfanny
09-28-2006, 11:13 AM
I have the following code, it seems that the FOR loop is not executed fully, as the output just printing the $letter_query, which is the 2nd line of the FOR loop -- print_r($letter_query);. The rest of the codes in the FOR loop is not executed at all. No error message displayed either. Could anyone help to examine the codes? Thanks a lot !

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Blog Warehouse</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Companies in the Blogosphere (Key word matching)</h1>

<?php

$dbcnx = @mysql_connect('localhost', 'root', 'password');
if (!$dbcnx) {
exit('<p>Unable to connect to the database server at this time.</p>');
}

// Select the blog database
if (!@mysql_select_db('ijob')) {
exit('<p>Unable to locate the blog collection database at this time.</p>');
}


$letter_name_query = "select comp_id, comp_name from company_list where comp_ini = '"."a". "'";
$letter_name_result = @mysql_query($letter_name_query);

if(!$letter_name_result)
{
exit ('<p>Error retrieving company names from database!<br />Error: '.mysql_error(). '</p>');
}


$query_array = array();
while ($letter_name_output =mysql_fetch_array($letter_name_result))
{
$letter_name = $letter_name_output['comp_name'];

$letter_name_id = $letter_name_output['comp_id'];

$letter_query = "select id, title, content from blog_entry where title like ";
$letter_query = $letter_query.'"%'.' '.$letter_name.'%"';
$letter_query = $letter_query. " OR content like ". '"%'.' '.$letter_name.'%"';
$query_array[] = $letter_query;
}

$count=count($query_array);

for ($i = 0; $i < $count; $i++)
{

$letter_query = $query_array[$i];
print_r($letter_query);
echo '<p></p>';
$letter_result = @mysql_query($letter_query);

if(!$letter_result)
{ echo '<p> not letter result</p>';
exit ('<p>Error retrieving blog entries from database!<br />'.
'Error: '.mysql_error(). '</p>');
}

while( $letter_output = mysql_fetch_array($letter_result)) {
echo '<p>while loop</p>';
if($letter_output == "")
{echo '<p>empty</p>'; }

if($letter_output != "")
{

echo '<p>result</p>';
$ID = $letter_output['id'];
print_r($ID);
echo '<p></p>';
}
}


}


?>


</body>
</html>

Phill Pafford
09-28-2006, 03:26 PM
whats the $count value?

yunfanny
09-28-2006, 09:52 PM
$count = 553.

So the output is printing of 553 select statements.

Thanks

stephan.gerlach
09-29-2006, 04:37 AM
maybe change this code


if($letter_output == "")
{echo '<p>empty</p>'; }

if($letter_output != "")
{

echo '<p>result</p>';
$ID = $letter_output['id'];
print_r($ID);
echo '<p></p>';
}


to


if($letter_output == "")
{echo '<p>empty</p>'; }

else
{

echo '<p>result</p>';
$ID = $letter_output['id'];
print_r($ID);
echo '<p></p>';
}


and see if that does have any effect besides better coding.

yunfanny
09-29-2006, 05:29 AM
Hi, stephan.gerlach,

Thanks for your advice, but there is no difference in the results after I changed the code.

for $row = mysql_num_rows($letter_result);
print_r($row);
the output is still 0 for every select statement.

Help :(

chazzy
09-29-2006, 06:25 AM
did you actually try running these select statements against the database, outside of PHP, and verify that they give results?

yunfanny
09-29-2006, 09:28 AM
Yes, I use MySQL bin and retrieved rows from the table......