Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to check result after update()? #4138

Closed
Jimmi08 opened this issue Apr 23, 2020 · 3 comments
Closed

How to check result after update()? #4138

Jimmi08 opened this issue Apr 23, 2020 · 3 comments
Labels
type: question An ask about behavior that is not found documented anywhere

Comments

@Jimmi08
Copy link
Contributor

Jimmi08 commented Apr 23, 2020

There are 3 possible results:

  • nothing was changed
  • successful change
  • error

I noticed that you are testing $sql->getLastErrorNumber() to detect if there is change or not (in news)
but it looks to me that there is an easier way too:

value 0 - no changes
value false - error

Correct?

Tested with this code:

        $result = $sql->update("spfb", implode(",",$updatearray)." WHERE `form_id`=".intval($form_id), true); 
        $m = $result ? array("s" => "success", "m" => SPFB_FORMS_UPDATED) : 
        ( $result === 0 ? array("s" => "info", "m" => LAN_NO_CHANGE) : array("s" => "danger", "m" => SPFB_FORMS_UPDATEERROR) );

and with this too.

$update = array(
        'data' => $insert ,
        'WHERE' =>  "form_id = ".intval($form_id) 
        );                                            
        $result = e107::getDb()->update( "spfb" , $update);

But I wanted to confirm this. So what is correct? test return value after update or getLastErrorNumber() ?

Thanks

@Jimmi08 Jimmi08 added the type: question An ask about behavior that is not found documented anywhere label Apr 23, 2020
@Moc
Copy link
Member

Moc commented Apr 23, 2020

The SQL methods like insert(), update(), delete(), gen() etc, will return false when there's an SQL error.

You can then retrieve the exact error using methods like $sql->getLastErrorNumber() and $sql->getLastErrorText().

Example:

if(!$sql->update($update_query))
{
	$sqlError = $sql->getLastErrorText();
	return $sqlError;
}

Or in your example

You could check
if($result == FALSE) or if(!$result)

I personally think it is best to check for the result of running the query, so checking if $result is false or not. Then if it is false, retrieve the actual SQL error.

@Jimmi08
Copy link
Contributor Author

Jimmi08 commented Apr 23, 2020

@Moc

I spent 2 hours debugging, so I know that this if($result == FALSE) will not work. This was the original check and it still displayed error after just resaving record. I tried to find how Admin UI check this (that blue message that nothing was changed), but after finding that news example, I got where the problem is .

Now you need to use
if($result === FALSE)
because if nothing is changed, than $result is 0 (int(0)) so you need === to be sure it is false.

I don't need to know the exact error.

There is no bug, everything is OK, I just wanted to be sure that I am on right track - to check 0 vs false.

@Moc
Copy link
Member

Moc commented Apr 23, 2020

Ah right, I did this without checking the actual code but using === might be necessary then yes. But you are right in saying that the return of 'false' means that there is an SQL error. I haven't confirmed the result of '0' but I'd assume that's correct.

@Moc Moc closed this as completed Apr 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question An ask about behavior that is not found documented anywhere
Projects
None yet
Development

No branches or pull requests

2 participants