mysql update not working inside loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fishnfrogs
    New Member
    • Nov 2009
    • 20

    mysql update not working inside loop

    Hi, I can't figure out why this isn't working. I'm trying to loop through an array and do a mysql update. However, it doesn't work.

    Code:
    for($i = 0; $i < $len; ++$i)
    {
    	$param = $array[$i] . '%';
    	$query = "UPDATE `$table` SET `bool` = '$bool' WHERE `user` LIKE '$param' LIMIT 1";
    	$result = mysql_query($query);
    	if (mysql_affected_rows() === 0)
    	{
    		mysql_free_result($result);
    		$result = mysql_query("INSERT INTO `$table` (id, user, bool) VALUES ('', '" . $array[$i] . rand(10, 50) . "', '$bool')");
    	}
    }
    I tried using prepared statements to avoid using a loop but there doesn't seem to be a way to use 'LIKE' and a wildcard together. I also tried using PHP to load my txt file to update it that way, but, once again, my server is timing out. Now I'm forced to, once again, use a counting system and url loaders in Flash. Everything on the Flash side is working perfectly. I post a long string that's colon delimited and explode it in PHP. If I comment out the update statement in my PHP, everything works perfectly. The problem seems to be with my actual update statement, but I can't seem to figure out what's wrong with it. Can anyone offer any insight? Thanks!
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You should be checking that your queries execute successfully. If an error occurs in MySQL, PHP won't tell you.

    The basic structure is this:

    Code:
    // Get a MySQL resource
    $res = mysql_query(' ... ');
    
    // Check that the function returned a resource, and not FALSE.
    if ($res != false) {
        // Do whatever with your resource
        while ( ... ) {
        }
    }
    // We got an error 
    else {
        printf('Something went wrong: %s<br />", mysql_error());
    }

    Comment

    • fishnfrogs
      New Member
      • Nov 2009
      • 20

      #3
      Hi, thanks for your respsonse. I put an 'or die(mysql_error ())' on my update statement but I still don't get any response. I'm trying to update 1000 rows at a time. Is this too big for PHP or something? Also, if I wanted to check if mysql_affected_ row === 0, would I go under my if($res != false) or under the else statement? I feel like this project has been like pulling teeth since day one. Nothing is going smoothly. Anyway, thanks again for your help!!

      Comment

      • fishnfrogs
        New Member
        • Nov 2009
        • 20

        #4
        Here's my PHP code -- just in case

        Code:
        set_time_limit(0);
        ini_set("max_execution_time", 0);
        ini_set('memory_limit', '400M');
        
        require_once 'my_db_stuff.php';
        
        $path = $_SERVER['DOCUMENT_ROOT'] . '/path_to_files/';
        $fileArray = array();
        
        $fileName = $_POST['name'] . '.txt';
        $table = $_POST['table'] . '_temp';
        $bool = $_POST['bool'];
        
        $array = array();
        $fp = fopen($path . $fileName, 'r');
        while(!feof($fp))
        {
        	$line = fgets($fp);
        	$array[] = trim($line);
        }
        fclose($fp);
        
        $len = count($array);
        
        for($i = 0; $i < $len; ++$i)
        {
        	$param = $array[$i] . '%';
        	$query = "UPDATE `" . $table . "` SET `bool` = " . $bool . " WHERE `user` LIKE '" . $param . "' LIMIT 1";
        	$result = mysql_query($query) or die(mysql_error());
        	if (mysql_affected_rows() === 0)
        	{
        		mysql_free_result($result);
        		$result = mysql_query("INSERT INTO `$table` (id, user, bool) VALUES ('', '" . $array[$i] . rand(10, 50) . "', $bool)") or die(mysql_error());
        	}
        }
        
        if($result)
        {
        	echo 1;
        }
        else
        {
        	echo 0;
        }
        *edit: After checking it, turns out I'm getting an Internal Server Error. I looked in the error logs and I'm getting a premature end of script error. And after using the google-net, it turns out that I'm stuck. Can you offer a different approach to update my database? Thanks again!

        Comment

        Working...