PDO connect to SQLite server on different server - php

I have a very simple SQLite database that I need to read/write to from a different server.
Say the database is stored here : http://www.abc.com/data/data.sqlite
And I'm using PHP to access it from http://www.xyz.com
So my first attempt was the following:
$dbpath = "http://www.abc.com/data/data.sqlite";
$dbconn = "sqlite:$dbpath";
$db = new PDO($dbconn)
No good, I get the following:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [14] unable to open database file'.........PDO->__construct('sqlite:http://w...') #1 {main} thrown
If try and copy the database onto the same server I'm accessing from:
$dbpath = "http://www.xyz.com/data/data.sqlite";
$dbconn = "sqlite:$dbpath";
$db = new PDO($dbconn)
I get the same message.
It's only when I give it a relative path on the same server:
$dbpath = "../data/data.sqlite";
That it actually works.
I know the database URLs and database itself are correct.
So is there a limitation to accessing cross-servers? Anyone know out to solve this issue?
Thanks a lot.

There are no such thing like 'SQLite server'. It exists only in a form of a file.
But there are no files in the HTTP protocol, but URIs only.
So, this is essential incompatibility.
To be able to do remote calls you have 3 choices
Only as a joke: download the file locally before each SELECT query and upload it back after updates
Establish some proxy script, to recieve a query and to return json.
Get yourself a real database server.
Or just change the project architecture to eliminate the need of remote access.

Related

Issue connecting to PHP with Ajax chat

Using Ajax integrated chat with PHPBB3. It is supposed to use the mysql connections that the site uses. The database tables have been created. When accessing, I continue to get the error
'>[phpBB Debug] PHP Warning: in file [ROOT]/phpbb/di/container_builder.php on line 291: file_put_contents([ROOT]/chat/../cache/container_[ROOT]/chatslashdotdotslash.php): failed to open stream: Invalid argument Error-Report: No connection could be made because the target machine actively refused it. Error-Code: 2002'
Edit: I have not had any trouble connecting to the database with the web application itself. I've read that the hosts file may need to be edited. I made sure that the socket server option for AJAX chat is set to false. I'm not sure what this error is actually trying to say here or what the actual connection error is. Can someone point me in the right direction?
The php class container_builder is where the error falls. Here is the function being called at that point:
protected function dump_container($container_filename)
{
$dumper = new PhpDumper($this->container);
$cached_container_dump = $dumper->dump(array(
'class' => 'phpbb_cache_container',
'base_class' => 'Symfony\\Component\\DependencyInjection\\ContainerBuilder',
));
file_put_contents($container_filename, $cached_container_dump);
I cleared out the particular cache file, which was then recreated, but the error does not go away. I would think there is no problem with connectivity to the database by the application. Cache is stored on the server not the database.

Can't change doctrine connection

I'm trying to build a script that imports items from a production database to a local one, but am having problems switching the connection form production to local.
Doctrine_Manager::getInstance()->setCurrentConnection('prod-slave');
// execute query to get data from production, confirm it worked
Doctrine_Manager::getInstance()->setCurrentConnection('local');
// insert data into local database
When I run this I'm getting this error:
PHP Fatal error: Uncaught exception 'Doctrine_Connection_Mysql_Exception' with message 'SQLSTATE[HY000]:
General error: 1290 The MySQL server is running with the --read-only
option so it cannot execute this statement' in...
If I close the connection to prod-slave before setting it to local I get:
PHP Fatal error: Uncaught exception 'Doctrine_Manager_Exception' with message 'Unknown connection: prod-slave' in...
Notice that in both of these cases, it's still trying to connect to the slave even though I changed the connection to local (In the first case, it must be the slave since it is read-only).
I have also tried manually setting the connections with:
Doctrine_Manager::connection('mysql://path');
Doctrine version: 1.2.3
Symfony version: 1.3.9
Any help would be much appreciated.
Thank you
If you check generated base classes for your models, at the very beginning you will see each of your model is bound to specific connection. So even if you set current connection to another one, the one bound is used.
Solution is to call Doctrine_Manager::getInstance()->bindComponent($component, 'local'); for each of your models.
Instead of using setCurrentConnection try using getConnection
$slave = Doctrine_Manager::getInstance()->getConnection('prod-slave');
$result1 = $slave->execute($some_sql);
$local = Doctrine_Manager::getInstance()->getConnection('local');
$result2 = $local->execute($some_other_sql);
works for me on a symfony 1.4 project.
This should work providing that, in your config/databases.yml, you have the two connections prod-slave and local properly setup. And in your schema files you have bound your tables to the appropriate connections. (Just in case a doc on connections and all that can be found here )

Connect to MS Access with PHP not working

I am trying to connect to a MS Access Database with PHP. It is working perfectly when I am creating a System DSN but how do I make the connection work, when I want to copy and use the PHP files plus Database on a different computer? (Without creating another System DSN on that computer as well)
At the moment I am trying it this way:
$conn = odbc_connect("odbc:DRIVER={Microosoft Access Driver (*.mdb)}; DBQ=$odbc_name; Uid=$Uid; Pwd=$Pwd;");
And I am getting this error:
Warning: odbc_connect() expects at least 3 parameters, 1 given in C:\wamp\www\PartB\db_connection.php on line 14
The file is correctly found with this line of code:
$odbc_name = $_SERVER["DOCUMENT_ROOT"] . "PartB\db.mdb";
So where is my problem? Why is this way not working, but System DSN is? Any ideas?
Ok I found a answer to this myself.
$conn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$odbc_name";
The above code makes the connection work without System DSN.
Now I just need to reconfigure my query statements and all good.

php excel com_exception: unable to lookup 'Workbooks': Access is denied

I am trying to use PHP's COM interface to query a database and create pivot tables from the data in an Excel Workbook.
I have configured the DCOM settings on the local machine to grant the user Local Access, Local Launch, and Local Activation privileges as well as Full Control of Configuration Permissions. I have also tried all three Identity settings.
Here is the code I am using:
<?php
$application = new COM("Excel.Application") or die("Unable to load Excel.");
$workbook = $application->Workbooks;
$workbook = $workbook->Add();
$sheet = $workbook->Worksheets(1);
$sheet->Activate();
$application->Quit();
?>
This is the error I am getting:
PHP Fatal error: Uncaught exception 'com_exception' with message 'Unable to lookup `Workbooks': Access is denied.
This is different than error I was getting before which told me that I didn't have access to the Excel COM object. Setting the permissions fixed that.
Environment is:
Windows Server 2012 R2
Excel 2010 (64-bit)
PHP 5.5.9 (64-bit)
I never was able to solve this, so we had to come up with a different solution and it actually is more stable and performs better. Hope it helps anyone else that may run into the same issue.
Our database is MySQL (but this should be possible with most databases), so we moved our query into its own script and used the mysql client to write the results directly to a file.
We then created an excel template with a data connection to the file and used the connection as the data source for the pivot tables.
Lastly we wrote a short vbscript to open the template, refresh the pivot tables, then and save the file as a new copy.
This had the added benefit of allowing us to make stylistic changes using Excel's interface, which was much faster than doing it programmatically.

PDO Construct error - PHP

I have created a wine shop in PHP that accesses a MySQL database and pulls the wines data onto a webpage. However, after getting it all working nicely I have now come across a PDO Construct error and do not know why.
The error is as follows:
Warning: PDO::__construct() [pdo.--construct]: [2002] A connection
attempt failed because the connected party did not (trying to connect
via HOSTNAME) in C:\webdev\shop\selectfromwines.php on line 5
It seems to relate to the following piece of code:
$database = new PDO('mysql:host=HOSTNAME; dbname=co525', 'co525', 'co525');
Any ideas where I might be going wrong?
P
is HOSTNAME real host name? If not - use correct address. If you have local db, try 127.0.0.1 or localhost
Open your php.ini and set the following:
pdo_mysql.default_socket=”/opt/lampp/var/mysql/mysql.sock”

Categories