473,320 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. for($i = 0; $i < $len; ++$i)
  2. {
  3.     $param = $array[$i] . '%';
  4.     $query = "UPDATE `$table` SET `bool` = '$bool' WHERE `user` LIKE '$param' LIMIT 1";
  5.     $result = mysql_query($query);
  6.     if (mysql_affected_rows() === 0)
  7.     {
  8.         mysql_free_result($result);
  9.         $result = mysql_query("INSERT INTO `$table` (id, user, bool) VALUES ('', '" . $array[$i] . rand(10, 50) . "', '$bool')");
  10.     }
  11. }
  12.  
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!
Apr 15 '10 #1
3 3675
Markus
6,050 Expert 4TB
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:

Expand|Select|Wrap|Line Numbers
  1. // Get a MySQL resource
  2. $res = mysql_query(' ... ');
  3.  
  4. // Check that the function returned a resource, and not FALSE.
  5. if ($res != false) {
  6.     // Do whatever with your resource
  7.     while ( ... ) {
  8.     }
  9. }
  10. // We got an error 
  11. else {
  12.     printf('Something went wrong: %s<br />", mysql_error());
  13. }
  14.  
Apr 15 '10 #2
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!!
Apr 16 '10 #3
Here's my PHP code -- just in case

Expand|Select|Wrap|Line Numbers
  1. set_time_limit(0);
  2. ini_set("max_execution_time", 0);
  3. ini_set('memory_limit', '400M');
  4.  
  5. require_once 'my_db_stuff.php';
  6.  
  7. $path = $_SERVER['DOCUMENT_ROOT'] . '/path_to_files/';
  8. $fileArray = array();
  9.  
  10. $fileName = $_POST['name'] . '.txt';
  11. $table = $_POST['table'] . '_temp';
  12. $bool = $_POST['bool'];
  13.  
  14. $array = array();
  15. $fp = fopen($path . $fileName, 'r');
  16. while(!feof($fp))
  17. {
  18.     $line = fgets($fp);
  19.     $array[] = trim($line);
  20. }
  21. fclose($fp);
  22.  
  23. $len = count($array);
  24.  
  25. for($i = 0; $i < $len; ++$i)
  26. {
  27.     $param = $array[$i] . '%';
  28.     $query = "UPDATE `" . $table . "` SET `bool` = " . $bool . " WHERE `user` LIKE '" . $param . "' LIMIT 1";
  29.     $result = mysql_query($query) or die(mysql_error());
  30.     if (mysql_affected_rows() === 0)
  31.     {
  32.         mysql_free_result($result);
  33.         $result = mysql_query("INSERT INTO `$table` (id, user, bool) VALUES ('', '" . $array[$i] . rand(10, 50) . "', $bool)") or die(mysql_error());
  34.     }
  35. }
  36.  
  37. if($result)
  38. {
  39.     echo 1;
  40. }
  41. else
  42. {
  43.     echo 0;
  44. }
  45.  
*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!
Apr 16 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Craig Keightley | last post by:
I need to create a form using multiple quantities that can be updated by one single update. eg: (HTML PAGE) <form method="post" enctype = "multipart/formdata"> <? do { ?> <?php echo...
6
by: PG | last post by:
When deleting a row from the database, that id is now missing. So what I'm trying to do is update in a loop (maybe an sql loop if there is one) of all of the id numbers - sort of like renaming...
11
by: DJJ | last post by:
I am using the MySQL ODBC 3.51 driver to link three relatively small MySQL tables to a Microsoft Access 2003 database. I am finding that the data from the MySQL tables takes a hell of a long time...
5
by: HydroSan | last post by:
Having a bit of a problem getting UPDATE working. The project in question is a simple MySQL VB.NET frontend, allowing Insertion, Selection, and others. Well, I've gotten Drop and Insert working,...
2
by: D. Dante Lorenso | last post by:
I'm trying to build a table that will store a history of records by enumerating the records. I want the newest record to always be number ZERO, so I created a trigger on my table to handle the...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
5
by: Valeria | last post by:
Hi, I've a CSV file with 3 columns ID - Price1 - Price2 How I would like to update the records of a MySQL tables that has the same ID of the CSV files. How Can I do this? Read from CSV and...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in...
1
ssnaik84
by: ssnaik84 | last post by:
Hi Guys, Last year I got a chance to work with R&D team, which was working on DB scripts conversion.. Though there is migration tool available, it converts only tables and constraints.. Rest of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.