php dbase error when opening a .dbf file - php

I'm trying to use the dbase library in php5.3 to open a .dbf file. I've got the dbase.so library installed and active on my php5 build and I'm executing the following code:
$db = dbase_open('CMX.dbf', 0);
if( $db ){
echo 'success';
dbase_close($db);
}
Where CMX.dbf is a Visual FoxPro9 data table and is located in the same directory as the executing script with read/write/execute permissions enabled.
The following is an exert from /var/log/apache2/error.log:
PHP Warning: dbase_open(): unable to open database CMX.dbf in /var/www/test.php on
line 28
As this error/warning is not very descriptive, I'm having issues tracking down the root cause. Can anyone help with this?

Not positive about PHP, nor Apache, but typically, when trying to connect to database files (or Foxpro), the typical approach would be to have a CONNECTION to a PATH, then perform a query against the name of the table....

Try this (in foxpro):
use cmx.dbf
copy to cmx_php.dbf type fox2x
I was having a similar problem where some dbs would open and others would not. This allowed me to access the db with php/dbase.so
I found the info here in the comments section.

Try this:
$db_path = "CMX.dbf";
$db = dbase_open($db_path, 0) // 0=ReadOnly, 1=WriteOnly 2=ReadWrite
or die("Error! Could not open dbase database file '$db_path'.");
if( $db ){
echo 'success';
dbase_close($db);
}

Related

PHP: Could not make sqlite3 database

I wanna create a sqlite3 database in PHP but I get an error from PHP
Error
Fatal error: Uncaught Exception: Unable to open database: unable to open database file
Codes
$database = new SQLite3('myDatabase.db');
Note: My PHP version is 7.1
Note: My database name is not duplicate and sqlite3 must make it
Thanks for attention
Your code is correct and should create the database file if it doesn't exist.
Most probably it's a user right issue, check the permissions of the folder where you are trying to create the file in.
Check the directory permissions. The folder containing the SQLite file must be writable by the appropriate user.

php can't open sqlite3 database

I have read a good bit on Stackoverflow, PHP Doc's, etc. So far nothing helps me open my SQLite3 database in PHP 5.6. This is my code snippet:
$db_file = "D:/Websites/Intranet/InetPub/wwwroot/WebDB/HamLog - SQLite/HamLog.SQLite";
// open database file
//$handle = sqlite_open($db) or die("Could not open database");
//$db = new sqlite($db_file) or die("Could not open Dan's HamLog.SQLite");
//$db = new Database($db_file);
$this->db = new PDO("sqlite:.$db_file", NULL, NULL);
//$this->db = new SQLite3($db_file);
You can see I've tried a number of things that all just died at the open attempt line with an error code. The active try at least gives an error saying it is unable to open the database. I can open the database using the sqlite console. Apparently in recent PHP releases, SQLite has been relegated to obscurity, by old references in the PHP documentation. The best I've found is the SQLite library is now housed in PECL. But then I read that its best to install PECL through PEAR. At this point I need some help. How do I open my SQLite database named: HamLog.SQLite in PHP?

Error accessing an SQLite db from a different location using PHP

I'm stuck with this problem. I'm trying to access an sqlite database from a different location. So far I have tried using PDO to access such file. I've tried using PDO since from what I read here on SO that would be easier. I only want to do some select queries on the database.
try{
$DBH = new PDO("sqlite:192.168.48.52/data/log/localdb.db");
echo "connected";
}
catch(PDOException $e) {
echo $e->getMessage();
}
it outputs an error message saying 'SQLSTATE[HY000] [14] unable to open database file'. I could access the location via SSH/putty or manually input the directory through a web browser. But not through PHP coded location. The file resides actually in a gsm gateway interface which has a -rw-r--r-- permission.
If I were to actually download the file and put it in my htdocs folder it does open the file and echos 'connected'
$DBH = new PDO("sqlite:localdb.db"); <--- **like this**
I also tried doing this since it requires some authentication to access the directory.
$DBH = new PDO("sqlite:admin:admin#192.168.48.52/data/log/localdb.db");
to no avail.
Thanks in advance.
i think you miss 1 colon after the IP :
$DBH = new PDO("sqlite:admin:admin#192.168.48.52:/data/log/localdb.db");

Cannot access SQLite3 db using php

I've spent many-a hour trying to get a local webserver working (I'm new)
I created a sqlite3 database ('database.sql') in the www folder, and tried calling it with numerous different php commands (php 5.5, I checked) such as '$test = new SQLite('database.sql')' or the same with SQLite3, both with no luck. Also tried $test->open('database.sql').
Always with the fatal error "Class 'SQLite' not found". I've spent too many hours on what I'm sure is a very simple problem, I'm sorry to have to ask this!
To open a DB using PHP5 and SQLite we need to use a PDO and not the sqlite_open() function.
This is how to open or create a database: (not sure if it's bug free)
try {
/*** connect to SQLite database ***/
$dbh = new PDO("sqlite:VPN0.sqlite");
echo "Handle has been created ...... <br>";
}
catch(PDOException $e) {
echo $e->getMessage();
echo "<br>Database is loaded UNSUCCESSFULLY .. ";
die("<br>Query is closed $error");
}
echo "Database loaded SUCCESSFULLY ....";
Hope this helps!
There is a php package called sqlite3 which you can use (as well as PDO which is given above). Here is a fraction of code which uses it.
$db = new SQLite3(DATABASE);
if (isset($dbversion)) { //only newer versions of chat will have this
$version = $db->querySingle("SELECT value FROM parameters WHERE name = 'db_version'");
Where the DATABASE variable has been defined with
define('DATA_DIR',$datadir); //Should be outside of web space
define('DATABASE',DATA_DIR.'chat.db');
Are you sure that the package is installed on your server?
If you're still having problems you can do a couple of things. First, use phpinfo(); in a page to determine if the SQLite3 extension is installed.
Second, if you want to use PDO make sure that the following line in your php.ini is un-commented:
extension=php_pdo_sqlite.dll
If you have to un-comment this you will need to restart your server for the changes to take effect.

Connect sqlite and php using netbeans

UPDATED
My latest code as follows:
$file ="sqlite:C:\New folder\\test.db";
echo $file;
$handle = new PDO($file) or die("Could not open database");
Error
sqlite:C:\New folder est.db
Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in C:\inetpub\wwwroot\NetBeans\PHP&SQLITE.php:20 Stack trace: #0 C:\inetpub\wwwroot\NetBeans\PHP&SQLITE.php(20): PDO->__construct('sqlite:C:\New f...') #1 {main} thrown in C:\inetpub\wwwroot\NetBeans\PHP&SQLITE.php on line 20
I'm trying to use PDO driver however the error keep prompting me saying cant find the driver when it's in my extension php folder. I'm at my wits now.
I'm using SQLITE version 3 and PHP version 5.4.3.
I already placed the database file inside my r/w folder but it Fatal error: Call to undefined function sqlite_open(). I already researched on the web for alot of sources but the my directory path still returns some strange error (Sorry if i'm too noob, first time learning).
My code as follows:
$db ="C:\New folder\test.db";
Test to print my directory path of the database.
echo $db;
$handle = sqlite_open($db); //or die("Could not open database");
Debug result
C:\New folder est.db
Fatal error: Call to undefined function sqlite_open() in C:\inetpub\wwwroot\NetBeans\PHP&SQLITE.php on line 18 which is "$handle = sqlite_open($db); //or die("Could not open database");"
Kindly advise where did I do wrong? The path I specified was correct but when I print out, it shows wrong path.
Thanks
one problem you have is that you are escaping (inadvertently) some of your path characters.
As your var_dump shows.
C:\New folder est.db
change your $db variable definition to
$db ="C:\New folder\\test.db";
or just put path in single quotes so PHP won't try and parse the string
$db ='C:\New folder\test.db';
and believe it or not in most cases unix paths will work:
$db ="C:/New folder/test.db";
as for the remainder of your problem, you need to make sure you have sqlite 2 enabled. View your php info and check to see if you a section like:
SQLite
SQLite support enabled
PECL Module version 2.0-dev $Id: sqlite.c 306939 2011-01-01 02:19:59Z felipe $
SQLite Library 2.8.17
SQLite Encoding iso8859
if not, in your php.ini make sure the line
extension=php_sqlite.dll
is uncommented and present (and the .dll exists in your extension folder).
However most people will recommend that you use the PDO driver for sqlite (and for any other database) as it will provide a more consistent paradigm across the different databases (you can use the same code with most databases).
[edit]
I found some comments in the php manual that may shed some light on your issues.
It seems that the directory that contains your sqlite database must be
writeable by the web server. Making just the file writeable won't
work.
and
Don't forget "extension=php_pdo_sqlite.dll" has to be enabled in
php.ini (if you use xampp is will be disabled by default) .

Categories