Skip to content

ext/mysqli: Throw error when mysql_stmt_data_seek cannot be executed#10419

Merged
kamil-tekiela merged 4 commits intophp:masterfrom
kamil-tekiela:mysqli_stmt_data_seek
Apr 21, 2026
Merged

ext/mysqli: Throw error when mysql_stmt_data_seek cannot be executed#10419
kamil-tekiela merged 4 commits intophp:masterfrom
kamil-tekiela:mysqli_stmt_data_seek

Conversation

@kamil-tekiela
Copy link
Copy Markdown
Member

According to the MySQL standard mysql_stmt_data_seek should be a void function, but in mysqlnd it was always returning bool. In this case, it makes sense to check for an error condition and inform the mysqli user about incorrect usage.
The only time this function returns false is if it's used on PS without a buffered result set, hence the text of the error message.

Copy link
Copy Markdown
Member

@cmb69 cmb69 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Since this is a programmer error, it makes sense to throw an Exception, but that might be too strong to introduce in a minor version. What happens currently in this case? Just nothing?

Comment thread ext/mysqli/mysqli_api.c Outdated

mysql_stmt_data_seek(stmt->stmt, offset);
if (mysql_stmt_data_seek(stmt->stmt, offset)) {
zend_throw_error(NULL, "The result set buffer is empty");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the wording; is there even a buffer in this case. Maybe "Result set is not buffered" (or something like that) gives a better clue about what's wrong.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, currently nothing happens. This is because until now we supported libmysql, which as stated above has this function implemented as void. Now, we are no longer limited by libmysql and we can start throwing an error.
I could also change this to a warning, but I don't see this happening very often and I don't it would be too much to have an error here.

In regards to the text, I wanted to find something rather vague. The error could happen because someone forgot to call store_result(), the PS has only been prepared but not yet executed, or the PS didn't produce any result set. Your suggestion fits the first reason, but not so much the rest. I welcome some brainstorming here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a mysqli_sql_exception? And is the result set empty because the offset points to something that doesn't exist? Then maybe a ValueError is better if one can determine ahead of time if the offset is invalid or not?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mysqli_sql_exception should really only be reserved for client and driver errors, i.e. from MySQL or from mysqlnd. This should have been implemented in mysqlnd, but since PDO can still use libmysql, we cannot add it without creating an inconsistent behaviour.

The error was supposed to be similar to https://github.com/php/php-src/blob/master/ext/mysqli/mysqli_api.c#L327 where we can nicely check ahead of time if the function can be used. Here I would need to use this:

if (!stmt->stmt->data || !stmt->stmt->data->result || !stmt->stmt->data->result->stored_data) {

What do you think? Would that be cleaner?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have applied changes and also changed the error message. I kept it as a normal error to keep in line with the error from the other function. I don't think ValueError fits here because it's not about an incorrect argument.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I meant, is it possible for the user to check ahead of time if the offset they provide is valid. But now that I see what the inspiration of this is I think it's fine.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thing I am wondering about: should the function name be included in the error message?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably makes sense to do so :)

@kamil-tekiela
Copy link
Copy Markdown
Member Author

Could I ask for additional opinions on this PR so it can land in 8.3? @bukka @ericmann @adoy

@bukka
Copy link
Copy Markdown
Member

bukka commented Aug 24, 2023

I think it's too late for 8.3 as we are well past feature freeze and this is behaviour changes that should at least be emailed to internals. I think it makes sense to do it but might be better to target 8.4

@ndossche ndossche requested a review from bukka as a code owner March 14, 2026 14:04
@kamil-tekiela kamil-tekiela requested a review from Girgias April 21, 2026 01:17
@kamil-tekiela kamil-tekiela force-pushed the mysqli_stmt_data_seek branch from c70fc7d to 89b3439 Compare April 21, 2026 13:42
@kamil-tekiela kamil-tekiela changed the title Throw error when mysql_stmt_data_seek fails ext/mysqli: Throw error when mysql_stmt_data_seek fails Apr 21, 2026
@kamil-tekiela kamil-tekiela changed the title ext/mysqli: Throw error when mysql_stmt_data_seek fails ext/mysqli: Throw error when mysql_stmt_data_seek cannot be executed Apr 21, 2026
@kamil-tekiela kamil-tekiela merged commit 637c116 into php:master Apr 21, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants