SQL: is there a delay on Insert? - php

When using SQL through php mysqli prepared statements, is there any delay on an Insert statement?
For example, in this script:
$sql = "INSERT INTO Records (Name, Data) VALUES (?, ?)";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('ss', $recordName, $data);
$stmt->execute();
echo "INSERT_SUCCESS";
Is the execute() function blocked until the Insert is complete?
If I replace the echo line with a Select query, will it find the newly inserted data or will the data still not be there, as the Insert statement was still not executed when the Select statement was queried.

PHP is a synchronous language. There is no delay. Except for very few cases, the next line won't execute until the previous is finished

Related

PHP mysqli multiple queries bind_param crash [duplicate]

I would like to know if i can prepare one mysqli statement that executes multiple queries:
mysqli->prepare(query1 ...1,2,3 param...; query2...4,5 param...);
or
mysqli->prepare(insert into ...1,2,3 param...; insert into...4,5 param...);
and after all
mysqli->bind_param("sssss", 1, 2, 3, 4, 5);
In that way it make error: Call to a member function bind_param() on a non-object in...
$stmt = $sql->getQueryPrepare("INSERT INTO user (id_user, username, pw, email) VALUES (?,?,?,?); INSERT INTO process (id_user, idp) VALUES (?,?);");
$stmt->bind_param("ssssss",$id, $username, $pw, $email, $id, $idp);
$stmt->execute();
$stmt->close();
A prepared statement can only execute one MySQL query. You can prepare as many statements as you want in different variables:
$stmtUser = $sql->prepare("INSERT INTO user (id_user, username, pw, email) VALUES (?,?,?,?)");
$stmtProc = $sql->prepare("INSERT INTO process (id_user, idp) VALUES (?,?);");
And then execute them later. If you want to ensure that neither one is ever run unless both are able to run, then you need to look into transactions, like Thomas said.
Also, a general tip: "call to member function on a non-object" is the standard error you get when prepare() fails and so $stmt isn't actually a prepared statement object. It usually means you need to look for an error in your prepare() statement rather than anything later.
No, a single call to the mysqli prepare() function cannot prepare multiple queries at once. You can, however, prepare more than one query for execution by using different variables. The documentation for this function is available here.
It also looks like you are trying to setup a transaction, which is a different question than you asked. If that's what you really want to know, then you'll need to provide more information about your database setup and probably more specifics about the use case you are trying to solve.

When do you have to close a php mysqli stmt before preparing a new one?

If I do not close the stmt at the end of this code snippet:
$stmt = $mysqli->prepare("SELECT typeId FROM types WHERE name = 'author'");
$stmt->execute();
$stmt->bind_result($typeId);
$stmt->fetch();
(with this:)
$stmt->close();
Then the following prepared statement fails with error code 0 and no error:
$stmt = $mysqli->prepare("INSERT INTO typeTracker (reserved_id, typeId) VALUES (NULL, ?)");
$stmt->bind_param("i", $typeId);
Why? Is it because I called the bind_result and fetch functions in the first block of code? I don't typically have to close statements before preparing new ones.
Thanks.
If $stmt gets replaced or falls out of scope it gets destroyed. You can close it as a formality, but normally it gets taken care of anyway.
Calling close() frees up any associated resources immediately. If you don't do that you'll need to wait for the garbage collector to take care of it.

PHP: Statement fails to prepare but SQL is correct [duplicate]

I would like to know if i can prepare one mysqli statement that executes multiple queries:
mysqli->prepare(query1 ...1,2,3 param...; query2...4,5 param...);
or
mysqli->prepare(insert into ...1,2,3 param...; insert into...4,5 param...);
and after all
mysqli->bind_param("sssss", 1, 2, 3, 4, 5);
In that way it make error: Call to a member function bind_param() on a non-object in...
$stmt = $sql->getQueryPrepare("INSERT INTO user (id_user, username, pw, email) VALUES (?,?,?,?); INSERT INTO process (id_user, idp) VALUES (?,?);");
$stmt->bind_param("ssssss",$id, $username, $pw, $email, $id, $idp);
$stmt->execute();
$stmt->close();
A prepared statement can only execute one MySQL query. You can prepare as many statements as you want in different variables:
$stmtUser = $sql->prepare("INSERT INTO user (id_user, username, pw, email) VALUES (?,?,?,?)");
$stmtProc = $sql->prepare("INSERT INTO process (id_user, idp) VALUES (?,?);");
And then execute them later. If you want to ensure that neither one is ever run unless both are able to run, then you need to look into transactions, like Thomas said.
Also, a general tip: "call to member function on a non-object" is the standard error you get when prepare() fails and so $stmt isn't actually a prepared statement object. It usually means you need to look for an error in your prepare() statement rather than anything later.
No, a single call to the mysqli prepare() function cannot prepare multiple queries at once. You can, however, prepare more than one query for execution by using different variables. The documentation for this function is available here.
It also looks like you are trying to setup a transaction, which is a different question than you asked. If that's what you really want to know, then you'll need to provide more information about your database setup and probably more specifics about the use case you are trying to solve.

odbc_exec vs odbc_excute

from php manual:
odbc_exec — Prepare and execute an SQL statement
odbc_execute — Execute a prepared statement
which is prepared by odbc_prepare
so what is the different? why not to use odbc_exec directly?
If you want to execute the same statement multiple times with different parameters, then you prepare it once, and execute the prepared statement multiple times. Some RDBMS' will compile the statement when you prepare it, and this saves time when you execute it. This is useful when you have a loop executing the same query inside the loop with different parameters.
For example:
$stm = odbc_prepare($conn, 'INSERT INTO users (id, name, email) VALUES (?, ?, ?)');
foreach($users as $user) {
$success = odbc_execute($stm, array($user['id'], $user['name'], $user['email']));
}

mysqli: can it prepare multiple queries in one statement?

I would like to know if i can prepare one mysqli statement that executes multiple queries:
mysqli->prepare(query1 ...1,2,3 param...; query2...4,5 param...);
or
mysqli->prepare(insert into ...1,2,3 param...; insert into...4,5 param...);
and after all
mysqli->bind_param("sssss", 1, 2, 3, 4, 5);
In that way it make error: Call to a member function bind_param() on a non-object in...
$stmt = $sql->getQueryPrepare("INSERT INTO user (id_user, username, pw, email) VALUES (?,?,?,?); INSERT INTO process (id_user, idp) VALUES (?,?);");
$stmt->bind_param("ssssss",$id, $username, $pw, $email, $id, $idp);
$stmt->execute();
$stmt->close();
A prepared statement can only execute one MySQL query. You can prepare as many statements as you want in different variables:
$stmtUser = $sql->prepare("INSERT INTO user (id_user, username, pw, email) VALUES (?,?,?,?)");
$stmtProc = $sql->prepare("INSERT INTO process (id_user, idp) VALUES (?,?);");
And then execute them later. If you want to ensure that neither one is ever run unless both are able to run, then you need to look into transactions, like Thomas said.
Also, a general tip: "call to member function on a non-object" is the standard error you get when prepare() fails and so $stmt isn't actually a prepared statement object. It usually means you need to look for an error in your prepare() statement rather than anything later.
No, a single call to the mysqli prepare() function cannot prepare multiple queries at once. You can, however, prepare more than one query for execution by using different variables. The documentation for this function is available here.
It also looks like you are trying to setup a transaction, which is a different question than you asked. If that's what you really want to know, then you'll need to provide more information about your database setup and probably more specifics about the use case you are trying to solve.

Categories