1

I want to check with PHP whether a query will execute properly without any error.

Say I want to check if inserting a particular record into a table is valid without actually inserting it.

I use the deadly combo PHP + MySQL :)

2
  • 5
    You cannot ask PHP something that only MySQL can tell you. Commented Jan 14, 2011 at 20:20
  • Im just asking what I feel should be :) Sometimes its not what people wanna answer :D BTW yes I will take care of that Commented Jan 14, 2011 at 20:27

3 Answers 3

6

Open new transaction and roll it back after test query execution. Use mysql_error() to check if test query was successful.

Sign up to request clarification or add additional context in comments.

1 Comment

In case of MyISAM I see only one way: use of test (may be temporary) table instead of real one. But that way requires additional SQL-query preprocessing.
3

1: Open up a mysqli connection $conn
$conn->autocommit(false);
$conn->query("INSERT x into y");
echo $conn->error;
$conn->rollback();
$conn->close(); //Without committing changes

2 Comments

(+1 for a detailed example of how to implement Valera Leontyev's idea)
Meh, I started writing this before he posted his - I just took longer since I was putting a code sample together.
1

start your set of queries with the MySql statement "start transaction", then wrap you following statements in try/catch blocks and then either send a "commit" or a "rollback"

see here on transactions in mysql

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.