How to Execute Two MySQL Queries as One in PHP/MySQL

How to execute two mysql queries as one in PHP/MYSQL?

Update: Apparently possible by passing a flag to mysql_connect(). See Executing multiple SQL queries in one statement with PHP Nevertheless, any current reader should avoid using the mysql_-class of functions and prefer PDO.

You can't do that using the regular mysql-api in PHP. Just execute two queries. The second one will be so fast that it won't matter. This is a typical example of micro optimization. Don't worry about it.

For the record, it can be done using mysqli and the mysqli_multi_query-function.

Running two separate SQL queries on one php page

I think your solution is simple, just change either one of your $sql to another name of variable (ex: $sql1), so you can fetch both of your query with $sql and $sql1, the undefined index happen because you store new query to the same variable, which replace the first query, so with 2 queries with different name it will not cause the undefined index, also, you don't have to separate the query, you can use

$conn = dbConnect('read', 'pdo');
// prepare the SQL query
$sql = "SELECT * FROM wbcsightings ........
....
$sql1 = "'SELECT * FROM wbcnews.......
....

and for fetch you can use

<?php $i=0; foreach ($conn->query($sql) as $row) { if($i==3) break;?>
......
<?php $i=0; foreach ($conn->query($sql1) as $row) { if($i==3) break;?>
......

hope this will help

How to execute multiple MySql query at same time?

The best way is use sql triggers or stored procedure:

https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html

https://dev.mysql.com/doc/connector-net/en/connector-net-tutorials-stored-procedures.html

In other way you can run it in code:

<?php
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if (!$mysqli->query("DROP TABLE IF EXISTS test") || !$mysqli->query("CREATE TABLE test(id INT)")) {
echo "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

$sql = "SELECT COUNT(*) AS _num FROM test; ";
$sql.= "INSERT INTO test(id) VALUES (1); ";
$sql.= "SELECT COUNT(*) AS _num FROM test; ";

if (!$mysqli->multi_query($sql)) {
echo "Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

do {
if ($res = $mysqli->store_result()) {
var_dump($res->fetch_all(MYSQLI_ASSOC));
$res->free();
}
} while ($mysqli->more_results() && $mysqli->next_result());
?>

Execute multiple queries at once in Mysql using PHP

What you are looking for is TRANSACTIONS assuming you are not using MyISAM since it does not supports Transactions.

The concept of transactions is that either all the queries will execute or no query would execute at all.

In simple words all-or-nothing is what Transactions do

This is a basic example using mysqli

mysqli_query("START TRANSACTION");

$query1 = mysqli_query("INSERT INTO TABLE1(id) VALUES(2)");
$query2 = mysqli_query("INSERT INTO TABLE2(id) VALUES(3)");

if ($query1 and $query2) {
mysqli_query("COMMIT"); //Commits the current transaction
} else {
mysqli_query("ROLLBACK");//Even if any one of the query fails, the changes will be undone
}

NOTE: This was just a simple example.It would better if you implement using try and catch blocks handling then exceptions properly.

Take a look at PHP DOCS

Multiple MySQL queries in one PHP file

If I add another case to the switch statement, which executes a MySQL query, I get a fatal error: call to a member function prepare() on null.

That's because you're executing $conn = null; statement in each of your cases and primarily because there's no break; statement in your switch-case. Without any break; statement, all the downstream cases(from the point where there's a match) will get executed. So your code block should be like this:

if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action){
case 'namecheck':
...
break;
case 'adduser':
...
break;
case 'test':
...
break;
}
}

PHP code to execute multiple queries with a single mysql_query() call

From the manual:

mysql_query() sends a unique query (multiple queries are not
supported)
to the currently active database on the server that's
associated with the specified link_identifier.

Move to mysqli, which has support for multiple statements.



Related Topics



Leave a reply



Submit