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

getOne() does not call free() , results in MySQL throwing "Commands out of sync, you can't run the command now" #46

Open
rozniak opened this issue Oct 24, 2017 · 2 comments

Comments

@rozniak
Copy link

rozniak commented Oct 24, 2017

When making a successful query to getOne() that returns rows, getOne() returns too early and never reaches the free() call. This results in MySQL throwing the error "Commands out of sync, you can't run the command now"

Specifically, the error is on line 212, here:
https://github.com/colshrapnel/safemysql/blob/master/safemysql.class.php#L212

The return statement means that $this->free($res) is never called if the query returned results, it should instead follow the other statements by storing the return value and returning after calling free().

@colshrapnel
Copy link
Owner

Although technically you are right, it shouldn't be an issue, as you shouldn't use getOne() with a query that returns more than 1 row.
Adding LIMIT 1 to your query should solve the problem.

Besides, I cannot reproduce this error. mysqli_query is buffered by default, which means that it's impossible to get "Commands out of sync, you can't run the command now" error. Can you please provide a code that can help me to reproduce this error?

@rozniak
Copy link
Author

rozniak commented Oct 25, 2017

Code along the lines of the following caused it for me:

$objType = $mySql->getOne(
    'CALL GetObjectTypeById(?i);',
    $objId
    );

// Error occurs on this call to getRow
$objDetails = $mySql->getRow(
    'CALL GetObjectDetails(?i);',
    $objId
    );

GetObjectTypeById is just a procedure that does a single SELECT statement and returns a single value, in my case it just returned 5 .

The procedure looks roughly like:

CREATE PROCEDURE GetObjectTypeById (IN queryId BIGINT UNSIGNED)
BEGIN
    SELECT ObjType
      FROM MyObjects
      WHERE Id = queryId;
END

The second call to GetObjectDetails is what then causes that error to come up - I found it was the getOne() call because replacing it using getRow() instead worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants