View Full Version : URGENT: mysql_fetch_array loop problems

04-25-2012, 09:41 AM
Hi, I am a student working on a project that is a beginner at PHP. I am creating a search engine and have some problems. basically, I have a basic user search query and a premium user search query separated by an IF statement. My basic query works (minus the image display) while my premium query doesnt. It is supposed to access the db and run a mathematical calculation to get a weighting for each search result and then display the results in order. My code is getting stuck in the premium loop. Please help as this is due in some hours....Code:



//This is only displayed if they have submitted the form
//if ($searching =="yes")
//echo "<h2>Results</h2><p>";

//If they did not enter a search term we give them an error

$searchbox = $_POST['searchbox'];

if ($searchbox == "")
echo "<p>You forgot to enter a search term";

// Otherwise we connect to our Database

$db_host = "localhost";
$db_username = "root";
$db_pass = "root";
$db_name = "qrate";

@mysql_connect ("$db_host", "$db_username", "$db_pass") or die ("Could not connect to MySQL");
@mysql_select_db ("$db_name") or die ("No Database with that name");

// We preform a bit of filtering

$searchbox = strtoupper($searchbox);
$searchbox = strip_tags($searchbox);
$searchbox = trim ($searchbox);

//Now we search for our search term, in the field the user specified

$data = mysql_query("SELECT * FROM ITEM WHERE item_desc LIKE '%$searchbox%'");

//And we display the results
//For basic users...

$levelcheck = mysql_query("SELECT * FROM USER WHERE username = '".$_SESSION['username']."'");
$levelarray = mysql_fetch_array( $levelcheck );
if ($levelarray['USER_LEVEL'] == 'basic')
while($result = mysql_fetch_array( $data ))
echo $result['ITEM_NAME'];
echo "<br>";
//$img = mysql_query("SELECT $result['ITEM_IMG']);
?><image src="<?php echo $result[ITEM_IMG]; ?>" />
echo "<br>";
echo $result['ITEM_LINK'];
echo "<br>";
echo "<br>";

//if premium user
mysql_query("CREATE TEMPORARY TABLE priority (item VARCHAR(40), itemweight DECIMAL(2,1))");

while($result = mysql_fetch_array( $data ))
$itemweight = 0;
if ($result['ITEM_CAT1'] == 1)
{$itemweight = $itemweight + $result['USER_CAT1'];}
if ($result['ITEM_CAT2'] == 1)
{$itemweight = $itemweight + $result['USER_CAT2'];}
if ($result['ITEM_CAT3'] == 1)
{$itemweight = $itemweight + $result['USER_CAT3'];}
if ($result['ITEM_CAT4'] == 1)
{$itemweight = $itemweight + $result['USER_CAT4'];}
if ($result['ITEM_CAT5'] == 1)
{$itemweight = $itemweight + $result['USER_CAT5'];}
if ($result['ITEM_CAT6'] == 1)
{$itemweight = $itemweight + $result['USER_CAT6'];}
if ($result['ITEM_CAT7'] == 1)
{$itemweight = $itemweight + $result['USER_CAT7'];}
if ($result['ITEM_CAT8'] == 1)
{$itemweight = $itemweight + $result['USER_CAT8'];}

mysql_query("INSERT INTO priority (item, itemweight) VALUES ($result[ITEM_NAME], $itemweight)");

$weightqery = mysql_query("SELECT item, itemweight FROM priority");
$weightarray = msql_fetch_array($weightqery);

return $weightarray;

//$prefmatch = mysql_query("SELECT $result['ITEM_NAME'], $result['ITEM_IMG'], $result['ITEM_LINK'] , $weightarray['itemweight'] FROM $result, $itemweight ORDER BY $weightarray['itemweight'] DESC");

while($sortedresult = mysql_fetch_array( $prefmatch ))
echo $sortedresult['ITEM_NAME'];
echo "<br>";
$img = $sortedresult['ITEM_IMG'];
echo "<image src='img' />";
echo "<br>";
echo $sortedresult['ITEM_LINK'];


//This counts the number or results - and if there wasn't any it gives them a little message explaining that
if ($anymatches == 0)
echo "Sorry, but we can not find an entry to match your query<br><br>";

//And we remind them what they searched for
echo "<b>Searched For:</b> " .$searchbox;

04-25-2012, 11:44 AM
I have fixed the issue with the loop. I had changed the names of the attributes in the db but forgot to change them in the code. I am still having trouble in getting the temp table to work i think.

04-25-2012, 11:48 AM
Always add proper error checks to your queries, like

mysql_query(...) or die(mysql_error());