This question already has answers here:
mysqli: can it prepare multiple queries in one statement?
(2 answers)
Closed 8 years ago.
I am trying to execute 2 queries at the same time. I know I can do this using mysqli::multi_query.
But is there any way I can perform a multiple queries using prepared statements?
Below is an example of my query Thanks!
$delete_all_options = "DELETE FROM option_categories WHERE item_id = ?; ";
$delete_all_options .= "DELETE FROM option_names WHERE option_category_id = ?";
$delete_stmt = $db->prepare($delete_all_options);
//Execute statement ......
You may want to use transactions. Here's an explanation for using transactions with PDO: http://php.net/manual/en/pdo.transactions.php . Transactions can be used also with the MySQLi extension, by setting MySQLi::autocommit(false) and then committing with MySQLi::commit() (with PHP 5.5+ you can also use MySQLi::begin_transaction() and other methods).
Transactions execute multiple queries "at once" and if one query fails all the transaction is reverted.
Related
This question already has answers here:
How to echo a MySQLi prepared statement?
(6 answers)
Closed 5 years ago.
Is there any function that will return the prepared query string after processing all the parameters. like
$stmt = $conn->prepare("SELECT full_name FROM user_info where user_id = ?");
$stmt->bind_param("s", $user_id);
Can I see the final query string that will execute?
If the driver is capable of using prepared statements, if it doesn't require emulation, then the final query executed is the prepared statement.
If you want to find out what was executed, you need to turn on the general query log on your server. That can be very, very noisy and fill up your disk quickly on a busy server.
This question already has answers here:
How to check if a MySQL query using the legacy API was successful?
(6 answers)
Closed 6 years ago.
so i created php variable contain mysql query inside , but is it will return data to the php variable when the table / column / data is not available ?
example :
$a1='1';
$item1nanowr = mysql_query("SELECT item_name FROM item_info WHERE item_special_number = $a1 ");
now just imagine that item_name column not available , is it will return data ?
and if i put "or die" statement like this :
$item1nanowr = mysql_query("SELECT item_name FROM item_info WHERE item_special_number = $a1 ") or die("Coming Soon");
is it will return word Coming Soon ?
or die() will run only if something goes wrong with the query. Several things could go wrong. Some of them are:
The database table does not exist
One of the specified columns does not exist
There is a syntax error in the query
The database is offline or the connection has been lost
So it will run on critical errors. It won't run if your table or a specific column is empty.
Also it is very important to stop using mysql_ functions!. The mysql extension is deprecated from PHP 5.5 and completely removed from PHP 7.
Use mysqli or PDO instead. You should also use Prepared Statements instead of directly interpolating variables in your query. More on Prepared Statements here
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MySQL INSERT from a SELECT with PDO
I've historically used the mysql_* style of connecting to mysql via php and am finally making the trek over to PDO. In the past I've like to use mysql's INSERT INTO... SELECT... to insert data. The benefit being that I could add columns to a table at a later time without completely hosing all other forms that interact with that table. My question is quite simply. . . is there a PDO equivalent to this?
I'm not having much luck using the old syntax in a prepared statement nor do I see any examples using this format. Any thoughts or suggestions would be much appreciated.
The queries don't change. You can use things like:
$postUser = $_POST["user_name"];
$stmt = $dbh->prepare("SELECT * FROM user WHERE userName = :postUser");
$stmt->bindParam(':postUser', $postUser);
$stmt->execute();
To INSERT:
$sql = "INSERT INTO table (whatever) VALUES (:whatever)";
$q = $conn->prepare($sql);
$q->execute(array(':whatever'=>'whatever'));
And to SELECT:
$sql = "SELECT whatever FROM table WHERE whatever = :whatever";
$q = $conn->prepare($sql);
$q->execute(array(':whatever'=>'whatever'));
$row = $q->fetch();
For more information on prepared statements go here.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get query back from PDO prepared statement
Exists any method to show, the query executed sql query in PDO Statement object?
Ex:
$sql="SELECT * FROM table WHERE id=?";
$res=$con->prepare($sql);
$res->execute(array(1));
I like to view a query similar this: "SELECT * FROM table WHERE id=1"
$res->queryString should contain what you need. You can check out the documentation here.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PDO Prepared Statements
I'm using the mysqli extension in PHP and I'm wondering, is there possibly any way to see a prepared query as it will be executed on the server, e.g. The query is something like this
select * from table1 where id = ? and name = ?
but I want to see the query after the values are filled in, like this:
select * from table1 where id = 20 and name = "John"
Turn on mysql query logging and it will log all queries to a text file for you to review.
Duplicate of PDO Prepared Statements
Short answer: no. A prepared query will never be converted to the query you expect. It's executed directly by the database server. You can use mysql's query log or PDO's undocumented function debugDumpParams, but both are just approximations.
See it where?
If it's your code you have the query and you have the prepared parameters, log them separately or replace in the original query string.
If the binding will fail you will get an error, otherwise you should expect the same values to be "filled" in as you specified them.
Its the way most of the times I am debugging mysql quires:
$q = "select * from table1 where id = ".$id." and name = ".$name;
echo $q;
The output generates all variables assigned to the query.
Hope I understood you exactly, what you wanted.