Updating Sqlite table with php yields error 8 - php

I'm running a LightSail amazon server with Lamp PHP 8.1 instance and I needed a lightweight DB for my server, so I've installed SQLite. I've created a DB and trying to insert into a table from php code
$db = new SQLite3('/home/bitnami/bot.db', SQLITE3_OPEN_READWRITE);
$sql = 'insert into users values("' . $id . '", "' . $platform . '", "' . $name . '", "' . $chatId . '", false)';
logLine($sql);
$db->exec($sql);
$err = $db-> LastErrorCode();
logLine("Error code: " . $err);
For some reason completely unclear to me, I'm getting error 8: attempt to write a readonly database.
I've tried the following with no success:
Give 777 permissions on db file
Change owner of db file to the user which runs apache (daemon)
Change owner to root
Shaman dance
When I run the same code with php cli from console it works! But if it runs under apache it doesn't!

You need to set Pragma journal mode in WAL to use read write functions
$db = new SQLite3('yourfile.db');
$db->query("PRAGMA journal_mode=WAL");

Related

MSSQL database stuck in restoring state with PHP

I am trying to restore a database to a different server through PHP. I managed to execute all needed commands but the database keeps hanging in the 'Restoring...' state on the server.
I have searched and followed the answer on SQL Server: Database stuck in “Restoring” state with PHP (which leads to this article) but this did not work for me; I get an error when trying to change the environment to the newly recovered database.
Code:
sqlsrv_configure( "WarningsReturnAsErrors", 0 );
$connOptions = ["Database"=>"master"];
$sqlConnection = sqlsrv_connect("server_name\\SERVER", $connOptions);
sqlsrv_query($sqlConnection, "USE master");
$sql = "IF EXISTS(SELECT name FROM sys.databases
WHERE name = 'db_name')
DROP DATABASE db_name";
sqlsrv_query($sqlConnection, $sql);
$sql = "RESTORE FILELISTONLY FROM DISK='$path'";
$logicalNamesStatement = sqlsrv_query($sqlConnection, $sql);
$moveArray = [];
while($logicalNames = sqlsrv_fetch_array($logicalNamesStatement, SQLSRV_FETCH_ASSOC)){
if($logicalNames['Type'] === "D"){
$moveArray['MDF'] = $logicalNames['LogicalName'];
}
elseif($logicalNames['Type'] === "L"){
$moveArray['LDF'] = $logicalNames['LogicalName'];
}
}
$localDbPath = "c:\\Program Files\\Microsoft SQL Server\\MSSQL11.SERVER\\MSSQL\\DATA\\";
$sql = "RESTORE DATABASE db_name FROM DISK='$path'
WITH
MOVE '" . $moveArray['MDF'] . "' TO '" . $localDbPath . "db_name.mdf',
MOVE '" . $moveArray['LDF'] . "' TO '" . $localDbPath . "db_name_log.ldf',
REPLACE,
STATS=10";
sqlsrv_query($sqlConnection, $sql);
$sql = "RESTORE DATABASE db_name FROM DISK='$path' WITH REPLACE, RECOVERY";
sqlsrv_query($sqlConnection, $sql);
sqlsrv_query($sqlConnection, "USE db_name");
The error that comes back is:
[Microsoft][SQL Server Native Client 11.0][SQL Server]Database 'db_name' cannot be opened. It is in the middle of a restore.
Do you have any ideas on how I can solve this? The only authority on a workaround for the bug described in my previously linked article seems to be that exact article, which does not work as you can see.
Thanks!
I don't have 50 reputation yet, so feel free to move this to the comments section. I don't see your database names including brackets "[ ]". If you have any database names with spaces or special characters, you'll need the brackets to identify the object properly. And as Mitch already stated, based on what you are doing, there is no reason for two restore operations; just add the RECOVERY option into the first and be done with it.

Run SQL script via PHP

I've created a backup.sql script to restore a MySql database. I've checked the script with PHPMyAdmin import and everything works fine (the database has been restored successfully). Now I would like to run it via PHP. I've found this question and I have:
1) created a PHP file into htdocs folder with the following content
$site_path= realpath(dirname(__FILE__)).'/';
$command = 'mysql'
. ' --host=' . 'localhost'
. ' --user=' . 'myuser'
. ' --password=' . 'mypass'
. ' --database=' . 'dbname'
. ' --execute="SOURCE ' . $site_path;
$output = shell_exec($command . 'backup.sql"');
echo "<pre>".$output."</pre>";
2) placed the backup.sql script into htdocs folder
But when I run the script, nothing happens on the database and nothing is displayed regarding shell_exec results. I'm running PHP and MySql under Apache on a windows machine. The command variable has the following value:
mysql --host=localhost --user=myuser --password=mypass--database=dbname --execute="SOURCE C:\Programmi\Apache Software Foundation\Apache2.2\htdocs/
What am I missing?
$mysql_host = "localhost";
$mysql_database = "db";
$mysql_user = "user";
$mysql_password = "password";
# MySQL with PDO_MYSQL
$db = new PDO("mysql:host=$mysql_host;dbname=$mysql_database", $mysql_user, $mysql_password);
$query = file_get_contents("shop.sql");
$stmt = $db->prepare($query);
if ($stmt->execute())
echo "Success";
else
echo "Fail";
If you have the whole code in your sql file 100% correct and nothing to change on it, then try this, use PDO for better security in your code.
You can overcome this by using a MYSQL DBMS tool as Workbench where you can connect to the remote/local server and run the .sql file without the use of PHP.
PHP is meant to be used for applying business queries that reflect certain business functionality and not basically made for server actions or other tool jobs like DBMS tools..
If you really need to do this you can check this tutorial that reads the sql file and explode the queries into an Array using the ; character that shall delimit each query.
It then loops through each query in the array using foreach and executes the query on its own.
One thing I notice is that, the operation may take longer or if another operation is needed afterwards, especially during development to test that the file has been executed, the execute method will no to terminated.
I simple solution is to execute the query, then get the prepared query and run method rowCount,with this then we can safely test whether or not the execution has terminated.
So just use $count = $stmt->rowCount(); and not only $stmt->execute();
try {
$db = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS);
$query = file_get_contents(__DIR__ . "/test.sql");
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->closeCursor();// Safely consuming the SQL operation till end
$count = $stmt->rowCount(); // Check if not pending transaction
if ($count) {
foreach($db->query('SELECT * from person') as $row) {
print_r($row);
}
}
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
exit;
}

mssql_num_rows error, but query works

Hi I am writing to a remote MSSQL 2005 server from a php application, and have a situation where mssql_num_rows errors out with the "mssql_num_rows() expects parameter 1 to be resource, boolean given" message.... but I can't figure out why
$writeitem = "INSERT INTO RebateSubmissionProducts VALUES ('" . $buyproduct . "'," . $quantity . ",CAST('" . $itemUUID . "' as UNIQUEIDENTIFIER),CAST('" . $eligible . "' as UNIQUEIDENTIFIER),CAST('" . $prodID . "' as UNIQUEIDENTIFIER),CAST('" . $UUID . "' as UNIQUEIDENTIFIER),NULL)";
$itemresult = mssql_query($writeitem);
if (!mssql_num_rows($itemresult)){
echo 'Problem writing to RebateSubmissionProducts';
} else {
echo 'Success writing to RebateSubmissionProducts';
}
mssql_free_result($itemresult);
The upshot is that I get the error message, but the insert works fine.
BTW all the input is run through HTMLPurifier so don't slag me too hard about that. The hosting company can't set up PDO_DBLIB so I can't use PDO/bound params.... I also don't have access to the MS server for creating a stored procedure.
Any ideas why php thinks that $itemresult is a boolean? (both mssql_num_rows and mssql_free_result issue the same error message)
As andrewsi pointed out, mssql_num_rows() responds in a different way depending on the query. In my case, I was running an insert, and so when this ran:
$itemresult = mssql_query($writeitem);
$itemresult was boolean(TRUE) beacuse the insert succeeded, so mssql_num_rows (and mssql_free_result() both issued warnings since there wasn't a result set.

Can you use a MySQL Query to Completely Create a Copy of the Database

I have a LIVE version of a MySQL database with 5 tables and a TEST version.
I am continually using phpMyAdmin to make a copy of each table in the LIVE version to the TEST version.
Does anyone have the mysql query statement to make a complete copy of a database? The query string would need to account for structure, data, auto increment values, and any other things associated with the tables that need to be copied.
Thanks.
Ok, after a lot of research, googling, and reading through everyone's comments herein, I produced the following script -- which I now run from the browser address bar. Tested it and it does exactly what I needed it to do. Thanks for everyone's help.
<?php
function duplicateTables($sourceDB=NULL, $targetDB=NULL) {
$link = mysql_connect('{server}', '{username}', '{password}') or die(mysql_error()); // connect to database
$result = mysql_query('SHOW TABLES FROM ' . $sourceDB) or die(mysql_error());
while($row = mysql_fetch_row($result)) {
mysql_query('DROP TABLE IF EXISTS `' . $targetDB . '`.`' . $row[0] . '`') or die(mysql_error());
mysql_query('CREATE TABLE `' . $targetDB . '`.`' . $row[0] . '` LIKE `' . $sourceDB . '`.`' . $row[0] . '`') or die(mysql_error());
mysql_query('INSERT INTO `' . $targetDB . '`.`' . $row[0] . '` SELECT * FROM `' . $sourceDB . '`.`' . $row[0] . '`') or die(mysql_error());
mysql_query('OPTIMIZE TABLE `' . $targetDB . '`.`' . $row[0] . '`') or die(mysql_error());
}
mysql_free_result($result);
mysql_close($link);
} // end duplicateTables()
duplicateTables('liveDB', 'testDB');
?>
Depending on your access to the server. I suggest using straight mysql and mysqldump commands. That's all phpMyAdmin is doing under the hood.
Reference material for Mysqldump.
http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
There is a PHP class for that, I didn't test it yet.
From it's description:
This class can be used to backup a MySQL database.
It queries a database and generates a list of SQL statements that can be used later to restore the database **tables structure** and their contents.
I guess this what you need.
Hi here you can use simple bash script to backup whole database.
######### SNIP BEGIN ##########
## Copy from here #############
#!/bin/bash
# to use the script do following:
# sh backup.sh DBNAME | sh
# where DBNAME is database name from alma016
# ex Backuping mydb data:
# sh backup.sh mydb hostname username pass| sh
echo "#sh backup.sh mydb hostname username pass| sh"
DB=$1
host=$2
user=$3
pass=$4
NOW=$(date +"%m-%d-%Y")
FILE="$DB.backup.$NOW.gz"
# rest of script
#dump command:
cmd="mysqldump -h $host -u$user -p$pass $DB | gzip -9 > $FILE"
echo $cmd
############ END SNIP ###########
EDIT
If you like to clone backuped database just edit the dump and change the database name then:
tar xzf yourdump.tar.gz| mysql -uusername -ppass
cheers Arman.
Well in script form, you could try using
CREATE TABLE ... LIKE syntax, iterating through a list of tables, which you can get from SHOW TABLES.
Only problem is that does not recreate indexes or foreign keys natively. So you would have to list them and create them as well. Then a few INSERT ... SELECT calls to get the data in.
If your schema never changes, only the data. Then create a script that replicates the table structure and then just do the INSERT ... SELECT business in a transaction.
Failing that, mysqldump as the others say is pretty easy to get working from a script. I have a daily firing cron job that dumps all manner of databases from my datacenter servers, connects via FTPS to my location and sends all the dumps across. It can be done, quite effectively. Obviously you have to make sure such facilities are locked down, but again, not overly hard.
As per code request
The code is proprietary, but I'll show you the critical section that you need. This is from in the middle of a foreach loop, hence the continue statements and the $c.. prefixed variables (I use that to indicate current loop (or instance) variables). The echo commands could be whatever you want, this is a cron script, so echoing current status was appropriate. The flush() lines are helpful for when you run the script from the browser, as the output will be sent up to that point, so the browser results fill as it runs, rather than all turning up at the end. The ftp_fput() line is obviously down to my situation of uploading the dump somewhere and uploads directly from the pipe - you could use another process open to pipe the output in to a mysql process to replicate the database. Providing suitable amendments where made.
$cDumpCmd = $mysqlDumpPath . ' -h' . $dbServer . ' -u' . escapeshellarg($cDBUser) . ' -p' . escapeshellarg($cDBPassword) . ' ' . $cDatabase . (!empty($dumpCommandOptions) ? ' ' . $dumpCommandOptions : '');
$cPipeDesc = array(0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'w'));
$cPipes = array();
$cStartTime = microtime(true);
$cDumpProc = proc_open($cDumpCmd, $cPipeDesc, $cPipes, '/tmp', array());
if (!is_resource($cDumpProc)) {
echo "failed.\n";
continue;
} else {
echo "success.\n";
}
echo "DB: " . $cDatabase . " - Uploading Database...";
flush();
$cUploadResult = ftp_fput($ftpConn, $dbFileName, $cPipes[1], FTP_BINARY);
$cStopTime = microtime(true);
if ($cUploadResult) {
echo "success (" . round($cStopTime - $cStartTime, 3) . " seconds).\n";
$databaseCount++;
} else {
echo "failed.\n";
continue;
}
$cErrorOutput = stream_get_contents($cPipes[2]);
foreach ($cPipes as $cFHandle) {
fclose($cFHandle);
}
$cDumpStatus = proc_close($cDumpProc);
if ($cDumpStatus != 0) {
echo "DB: " . $cDatabase . " - Dump process caused an error:\n";
echo $cErrorOutput . "\n";
continue;
}
flush();
If you're using linux or mac, here is a single line to clone a database.
mysqldump -uUSER -pPASSWORD -hsample.host --single-transaction --quick test | mysql -uUSER -pPASSWORD -hqa.sample.host --database=test
The 'advantage' here is that it will lock the database while its making a copy.
That means you end up with a consistent copy.
It also means your production database will be tied up for the duration of the copy which generally isn't a good thing.
Without locks or transactions, if something is writing to the database while you're making a copy, you could end up with orphaned data in your copy.
To get a good copy without impacting production, you should create a slave on another server.
The slave is updated in real time.
You can run the same command on the slave without impacting production.

Basic update database table using php question

I seem to be missing something quite fundamental here and yet my code doesn't seem to be any different to any of the numerous online tutorials that I have looked at.
What I would like is for someone to look at this and say....Oh you have forgotten to...etc;
This is what I have on a separate update page which is intended to perform the update then cycle back to the main admin page:
require_once('../Connections/MyConn.php');
$sql_statement = "UPDATE skyscrapers SET ";
$sql_image = "Ad_image = '" . $_REQUEST['image'] . "', ";
$sql_expire = "Ad_Expires = '" . $_REQUEST['expire'] . "'";
$result = mysql_query($sql_statement . $sql_image . $sql_expire . " WHERE Ad_ID=" . $_REQUEST['ADID']);
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
mysql_close ($MyConn);
header("location:Admin_skyscrapers.php");
However when I run this I get the following error:-
"Error performing query: No database selected"
Well, haven't I selected the database in the connection script which already works everywhere else?
I realise the code isn't very pretty and I am being naughty using the url to pass variables at the moment - I do promise to change this when I get it to work :)
So, any pointers would be helpful, thanks in advance.
Edit to add...
This is the connection script with the sensitive stuff redacted:-
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_MyConn = "*************.co.uk";
$database_MyConn = "db**********";
$username_MyConn = "dbo*********";
$password_MyConn = "*****";
$MyConn = mysql_pconnect($hostname_MyConn, $username_MyConn, $password_MyConn) or trigger_error(mysql_error(),E_USER_ERROR);
This may or may not be declared in your MyConn.php but all you need is a line:
mysql_select_db($db_name);
Where $db_name is the name of your database.
This should come before you attempt to execute the query.
For DB select you have to add mysql_select_db(DatabaseName); or
$dbconn=mysql_select_db($dbname,$MyConn);in MyConn.php
For update in Database you have to use connection variable which is in MyConn.php i.e.$MyConn as follows
$result = mysql_query($sql_statement . $sql_image . $sql_expire . " WHERE Ad_ID=" . $_REQUEST['ADID'],$MyConn);
or
$result = mysql_query($sql_statement . $sql_image . $sql_expire . " WHERE Ad_ID=" . $_REQUEST['ADID'],$dbconn);
respectively
Hope It Helps!!!!!!!

Categories