Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    URGENT: mysql_fetch_array loop problems

    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:
    Code:
    <?php 
    
    session_start();
    
    //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"; 
    exit; 
    } 
    
    // 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]; ?>" /> 
    	<?php
    	echo "<br>"; 
    	echo $result['ITEM_LINK']; 
    	echo "<br>"; 
    	echo "<br>"; 
    	} 
    }
    
    //if premium user
    else
    {
    	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 
    $anymatches=mysql_num_rows($data); 
    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; 
    //}
    ?>

  • #2
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Progress

    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.

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,851
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Always add proper error checks to your queries, like
    Code:
    mysql_query(...) or die(mysql_error());
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •