I can´t seem to find the error in this code.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Owe Blomqvist
Forum Commoner
Posts: 33
Joined: Wed Oct 16, 2002 2:27 pm

I can´t seem to find the error in this code.

Post by Owe Blomqvist »

Hi.
I´m new to this board and php, but i allways read it for answers when i get stuck in php.
Anyhow, I have this problem wich i´ve been trying to solve for the last week without any succes.
I made this script that compares 2 dates to each other to output the sum of those two into a column in a mysql database.

$row[6] = registrered date.
$row[7] = registrered date + 10 days.
$row[9] = remaining days

When i run the code below i get this output:

Name: name1
Before Update: 0
Patch Status: UPDATE `table` SET status=10
After Update: 5

Name: name2
Before Update: 0
Patch Status: UPDATE `table` SET status=10
After Update: 5

Name: name3
Before Update: 0
Patch Status: UPDATE `table` SET status=5
After Update: 5

Everything seems fine except putting the correct values in $row[9].
I hope you php gurus can help me with this one.
I can´t seem to find an answer anywhere in my php books nor php forums.
Thank you for your time & sorry about the length of this thread.
/Owe Blomqvist

Here´s the code.

Code: Select all

<?
		$link = mysql_connect ("host", "user", "pass");
		$database =	mysql_select_db ("database");
		$sql = "SELECT * FROM table";
		$result = mysql_query($sql);
			
			for($i=0; $i <=$result; $i++)
			{
			$row = mysql_fetch_row($result);
			$difference = ($rowї'7'] - $rowї'6']);
			$sum = ($difference - ($difference % 86400)) / 86400;
			$patch_status = "UPDATE `table` SET status=$sum";
			$do_patch = (mysql_query($patch_status));

					if(!$do_patch)
						{
						echo "Hmmm. No workie";
						return false;
						}
mysql_query($do_patch);
			
 			echo 	"Name: $rowї1]<br>
					 Before Update: $rowї9]<br>
					 Patch Status: $patch_status<br>
					 After Update: $rowї9]<br><br>";
			}
?>
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

row[9] is status?

Post by phpScott »

Try putting your $sum is single quotes
'$sum' and see what happens.

phpScott
Owe Blomqvist
Forum Commoner
Posts: 33
Joined: Wed Oct 16, 2002 2:27 pm

Post by Owe Blomqvist »

Thanks for the reply, but that didn´t help.
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

$difference = ($row['7'] - $row['6']);
try putting the numbers without (')s since they are numbers not strings
Owe Blomqvist
Forum Commoner
Posts: 33
Joined: Wed Oct 16, 2002 2:27 pm

Post by Owe Blomqvist »

Thanks, but that didn´t do it either.
It seems the code adds the first value (5) to all rows.
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

mysql_query($do_patch);

==

mysql_query(mysql_query($patch_status));

since

$do_patch = (mysql_query($patch_status));


now i might be going crazy, and i am definately quite a newbie when it comes to this sort of thing, but im assuming that would give you the funny answer you get


BESIDES WHICH

you are saying that this:

Code: Select all

<?php
          echo    "Name: $rowї1]<br> 
                Before Update: $rowї9]<br> 
                Patch Status: $patch_status<br> 
                After Update: $rowї9]<br><br>";
?>
gives 2 different values for $row[9]
Owe Blomqvist
Forum Commoner
Posts: 33
Joined: Wed Oct 16, 2002 2:27 pm

Post by Owe Blomqvist »

you are saying that this:

Code: Select all

<?php
          echo    "Name: $rowї1]<br> 
                Before Update: $rowї9]<br> 
                Patch Status: $patch_status<br> 
                After Update: $rowї9]<br><br>";
?>
gives 2 different values for $row[9]
No, i´m saying, the code writes the first "$patch_status" value (5) into all rows.

You´re right about executing the mysql_query twice.
I fixed it but it still won´t work. :cry:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You need to make a change to your update statement - as you've found out just doing:

Code: Select all

UPDATE table SET column1='value'
sets every single column1 field to be value. You need to add a where statement to limit it to just the record that you're working on. If you had an ID field in the database this is what I would use (any unique value will do):

Code: Select all

UPDATE table SET column1='value' WHERE ID='current_record_id'
Have a look at http://www.mysql.com/doc/en/UPDATE.html for more info.

Mac
Owe Blomqvist
Forum Commoner
Posts: 33
Joined: Wed Oct 16, 2002 2:27 pm

Post by Owe Blomqvist »

Thank You so very much.
it worked right away (I had the unique 'ID' column).
A Huge hug to you guys for taking your time to look into my problem.
Thanks again.
/Best Regards,
Owe Blomqvist.
Post Reply