Doctrine Entity Manager gets Read Only error from database - php

public function save($itemId, $invoiceName)
{
$query = "
INSERT INTO invoice (item_id, invoice_name)
VALUES (" . $itemId . ",'" . $invoiceName . ")
";
$connection = $this->entityManager->getConnection('master');
return $connection->executeQuery($query);
}
Error while trying to insert into table:
An exception occurred while executing ' INSERT INTO invoice (item_id, invoice_name) VALUES (10600029,'FV/1823/06/2018/SOL') ': SQLSTATE[HY000]: General error: 1290 The MySQL server is running with the --read-only option so it cannot execute this statement
Checked connection to DB its fine.
Tried to save something with same user with a DBeaver DB client and it went through all fine - saved without any problem.

you need to close the connection used before to change connection and insert I think, try something like this:
$connection = $this->entityManager->getConnection()->close();
$query = "
INSERT INTO invoice (item_id, invoice_name)
VALUES (" . $itemId . ",'" . $invoiceName . ")
";
$connection = $this->entityManager->getConnection('master');
return $connection->executeQuery($query);

Even tho I specified 'master' the request was catched by slave all the time.
$entityManager->getConnection()->prepare('INSERT ... ')->execute();
Appeared to fix the problem.

Related

I would like to avoid importing duplicates records but ON DUPLICATE KEY UPDATE is not working

I have a table in dbf file.
I am writing PHP script to import data from dbf file to the mysql using PHP and ODBC connection.
So I have file called ffc. It has four columns file, format,commons,dbf. The dbf file does not have primary key.
I created a table in mysql with 4 fields. I also added an 'id' field as primary key.
I am writing PHP to import records from dbf file to the mysql. But if records exist update (if record has been updated in dbf file) and move to next records and add new record if record does not exist.
I am using ON DUPLICATE KEY UPDATE file but it is not working.
WHen I run the script first time; all records get imported. But when I run the script again they get imported again.
Here is code.
<?php require_once('../database.php'); ?>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
error_reporting(E_ERROR | E_PARSE);
//ODBC connection
$connectionstring = odbc_connect("foxpro", "", "");
//Select statement to pull data
$q1 = "SELECT * FROM ffc.dbf";
$rw = odbc_exec($connectionstring, $q1);
//Loop through to insert data into mysql
while ($rs = odbc_fetch_array($rw)) {
//print_r($rs);
$file = $rs['file'];
$format = $rs['format'];
$commons = $rs['commons'];
$dbf = $rs['dbf'];
//Insert/Update query
$test_query = "INSERT INTO test (file, format, commons, dbf) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE file = ?, format = ?, commons = ?, dbf = ?;";
if(!($stmt_test_query = $mysqli->prepare($test_query))) {
echo "Prepare (1) failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if(!($stmt_test_query->bind_param('iiiiiiii', $file, $format, $commons, $dbf,$file, $format, $commons, $dbf))) {
echo "Bind params (1) failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if(!($stmt_test_query->execute())) {
echo "Execute (1) failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
}
?>

PHP/MYSQL: Update query after delete query removes relevant rows

Hello Stack Overflow Community,
I have a table which stores information about an event running sheet. When a user updates this timesheet, I first insert the new data, in order, to the end of the table. To differentiate this data from the data last entered/updated, I have a boolean column called 'transit', and all such new data is marked as TRUE, whilst all previous data is marked as FALSE.
The idea behind this is that in case an error occurs while updating the table with the new data, the previous data is not lost, which would be the case if I deleted all data from the table first, and then entered the new rows.
Creating these rows is working fine, however when I conduct the following code...
$query = "DELETE FROM " . $table_name . " WHERE transit = 0";
if (!$result = mysqli_query($conn, $query)) {
echo("Query Error: " . mysqli_error($conn));
exit();
}
$query = "UPDATE " . $table_name . " SET transit = 0";
if (!$result = mysqli_query($conn, $query)) {
echo("Query Error: " . mysqli_error($conn));
exit();
}
...all the new data which was inserted with transit = 1 is deleted. After I did some research on Stack Overflow, it was apparent I should be using mysqli_multi_query(); - When I tried that with this modified code...
$query = "DELETE FROM " . $table_name . " WHERE transit = 0; UPDATE " . $table_name . " SET transit = 0";
if (!$result = mysqli_multi_query($conn, $query)) {
echo("Query Error: " . mysqli_error($conn));
exit();
}
.... I was getting the "Commands out of sync; you can't run this command now" error.
Closing and reopening the connection between the sequential queries did not make a difference, nor did adding:
mysqli_store_result($conn);
mysqli_free_result($result);
while (mysqli_next_result());
help either, as someone suggested on another post. Is this some sort of buffer issue with MySql? Any help would be appreciated!
Thank you very much for taking the time to read!
UPDATE
So the logic seems to in fact work, ie enter new data and mark it with transit = 1, delete all data marked with transit = 0, then set all remaining data to transit = 0, when using the mysqli_multi_query() function, as well as the mysqli_store_result($conn); mysqli_free_result($result); while (mysqli_next_result());, as I described above. However, as described, this throws the Commands out of sync error. I thought that meant the queries had failed, but apparently, they have succeeded.
In that case, what is causing this error to throw? Thanks!

I cannot insert a data with Postgres and PHP

I'm developing a software with PHP and Postgres. So, I have a problem with insert code:
function create(){
$query = "INSERT INTO " . $this->table_name . " VALUES (" . $this->pess_nome . ")";
$result = pg_query($this->conn, $query);
if($result){
return true;
}else{
return false;
}
}
The error message :
Warning: pg_query(): Query failed: ERROR: column "test" does not exist LINE 1: INSERT INTO pessoa VALUES (test) ^ in C:\xampp\htdocs\Projeto_GPass\objects\pessoa.php on line 26
String values in SQL queries need to be enclosed in apostrophes or quotes. Modify the second line of your code to read:
$query = "INSERT INTO " . $this->table_name . " VALUES ('" . $this->pess_nome . "')";
EDIT: When you use a string value in a query and do not enclose it in quotes ("test") or apostrophes ('test'), PostgreSQL takes it as a name of the column, hence the error message.

MySQLI Insert not working not inserting blocked IP

I have this code to insert new attempts to login so when people fail 7 times they get blocked. The problem is when trying to insert while IP is not listed on DB it just does not insert and I cannot understand why. Here is the code:
session_start();
$blocked = mysqli_query($db, "
SELECT * FROM `blocked` WHERE `ip` = '" . $_SERVER["REMOTE_ADDR"] . "' LIMIT 1");
if (mysqli_num_rows($blocked)==0){
$insert = mysqli_query($db, "
INSERT INTO `blocked` VALUES ('', '" . $_SERVER["REMOTE_ADDR"] . "', '1')");
}
I get error
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in
if $db was defined somewhere else, it might not be available.
There are ways to get variables to move around in different scopes, otherwise just connect again at the top of this script
$db = connect(...)

PHP MySQL - Move record to another table, Select, Insert, Delete

I have two tables with the same structure. Iam attempting to move an entire record from one table to another and then delete from the source table. If i execute the two statements seperately it works, i.e. select and Insert 2. Delete.
How could I combine these two sql statments so that both statements are within one, and can be esxecuted one after another. Here is mysql statement so far, Its not executing
$q=" INSERT INTO archived (SELECT * FROM table1 WHERE id='$id') ;
DELETE FROM table1 WHERE id='$id'; ";
You can execute multiple SQL statements using one SQL statement string by seperating them by semi colon and passing the final statement to mysqli_multi_query() function.
$con = new mysqli('localhost', 'user', 'pass', 'demo');
// username is user , password is pass ,connecting to the demo database on the localhost host:
if($con->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "INSERT INTO archived SELECT * FROM table1 WHERE id='$id';";
$sql .= "DELETE FROM table1 WHERE id='$id'; ";
if (!$mysqli->multi_query($sql)) {
echo "Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
else{
echo "Executed successfully";
}
You can do
function mysql_multiquery($queries,$conn)
{
$queries = explode(";", $queries);
foreach ($queries as $query)
$query = mysql_query(trim($query),$conn);
}
Call function
mysql_multiquery($sql,$conn);
If you are looking to send only one command from php, write a stored procedure with the two queries. Then your php command executes the procedure.

Categories