I am trying to get my fist sqlite programs with php working on localhost but I can't get it to work. On my machine I have installed Sqlite3 and all works fine with C/C++.
If I move created database to localhost, give read/write permissions to db file and try to access them through php I get following error message:
file is encrypted or is not a database
Here is example code I use:
<?php
$dbhandle = sqlite_open("m_test1.db", 0666, $error);
if (!$dbhandle) die ($error);
$stm = "CREATE TABLE Friends(Id integer PRIMARY KEY," .
"Name text UNIQUE NOT NULL, Sex text CHECK(Sex IN ('M', 'F')))";
$ok = sqlite_exec($dbhandle, $stm, $error);
if (!$ok)
die("Cannot execute query. $error");
echo "Database Friends created successfully";
sqlite_close($dbhandle);
?>
If I run this code through browser when database don't exists then I get:
unable to open database: /var/www/m_test1.db
Info:
sqlite_libversion: 2.8.17
phpversion: 5.3.2-1ubuntu4.14
linux Ubuntu 10.04
By looking to phpinfo it seems that SQLite, SQLite3 and PDO_Sqlite is enabled.
Any help to get this working will be appreciated.
EDIT:
Solution is: 'chmod ugo+rwx /var/www' :)
After that sqlite_open and PDO both can create database.
PHP5 doesn't play nice with sqlite_open(). You'll need to use a PDO instead, like shown here: https://stackoverflow.com/a/4751965/369032
(code copied from above answer)
try
{
/*** connect to SQLite database ***/
$dbh = new PDO("sqlite:VPN0.sqlite");
echo "Handle has been created ...... <br><br>";
}
catch(PDOException $e)
{
echo $e->getMessage();
echo "<br><br>Database -- NOT -- loaded successfully .. ";
die( "<br><br>Query Closed !!! $error");
}
echo "Database loaded successfully ....";
Related
I am having connection issues when trying to connect from a PHP script to a SQL server database.
I have used the PDO method - My script can be seen below, connecting using Windows authentication, the object referenced in my script has been created in the database, roles and permissions have been defined.
I am testing locally using WAMP, calling a simple file called insert.php from the webroot.
My SQL server is installed on the same machine and my SQL version is 2012.
The script when executed should simply echo the results of the PATIENT table to the screen for now.
In both of my Apache and PHP versions of the php.ini file i have added the necessary extension to use the sqlsrv drivers and installed all the of the necessary drivers.
Yet still I get the error:
could not find driver1
The script:
<?php
try
{
$conn = new PDO("sqlsrv:Server=localhost ;Database=MindTrackDb", "", "");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(Exception $e)
{
die( print_r( $e->getMessage() ));
}
$tsql = "SELECT * FROM PATIENT";
$getResults = $conn->prepare( $tsql);
$getResults->execute();
$results - $getResults->fetchAll(PDO::FETCH_BOTH);
foreach($results as $row) {
echo $row['PATIENT_ID'].''.$row['FORENAMES'].''.$row['SURNAME'].''.$row['EMAIL'];
echo '<br>';
}
?>
My question is 2 part really:
A) Is this the best method to communicate to a SQL server database with PHP or is there a better method?
B) What could the driver error possibly be - I have followed various suggestions from here and nothing has worked.
I have searched on many different sites and cannot find an answer that specifically answers this question:
I have WAMP Server installed, which installed phpmyadmin as well as MySQL. I can easily connect to the database through php/pdo by using localhost as my hostname.
But my problem is I am trying to connect to a database that I have exported from phpmyadmin as "mydatabase.sql". So now lets say I placed this file on my pc at "C:\Users\Username\Desktop\MyFolder\mydatabase.sql". How would I connect to this database (if this is possible)?
The reason is that I cannot install WAMP Server on all the pcs that would use this program. so would like to be able to connect to the database without having to install any type of servers, etc...
My php at the moment is:
<?php
$dsn='mysql:C:\Users\Username\Desktop\MyFolder\mydatabase.sql';
$username='username';
$password='pass';
try
{
$dbh = new PDO("$dsn",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected to Database<br/>';
$sql = "SELECT * FROM users";
foreach ($dbh->query($sql) as $row)
{
echo $row["ID"] ." - ". $row["Name"] ."<br/>";
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Also, if this is possible, would username and password be necessary to connect to an "off server database"?
If I try:
$dsn='sqlite:C:\Users\Optique\Desktop\optique.sql';
I get:
"Connected to Database
SQLSTATE[HY000]: General error: 26 file is encrypted or is not a database"
But I think that is because SQLite uses a different database format than MySQL (stand to be corrected). So what I need is something similar to the SQLite driver for MySQL.
Thanks in advance!
You cannot find an answer simply because it doesn't exist: it's impossible to connect to an SQL dump through PDO, and it makes no sense anyway: a dump is not a database but simply a collection of INSERT queries. Unlike sqlite, for mysql you need a server to connect with.
To be able to connect, you should import that dump into mysql database first and then connect to that database with PDO.
I have UwAmp installed and running. I have set up a mysqlite db on the localhost and I'm trying to connect to it using the following PHP code:
<?php
try
{
/*** connect to SQLite database ***/
$dbh = new PDO("sqlite:graspe.sqlite");
echo "Handle has been created ...... <br><br>";
}
catch(PDOException $e)
{
echo $e->getMessage();
echo "<br><br>Database -- NOT -- loaded successfully .. ";
die( "<br><br>Query Closed !!! $error");
}
echo "Database loaded successfully ....";
?>
The db is called graspe and when I run this script it says that it has connected successfully. If I change the name of the database to something else it still returns a successfully connected message. What am I doing wrong? Thanks in advance.
By default when you construct a new connection to sqlite database - that database will be created if it doesn't exists.
If you want to test your code to make sure it throws an exception when the database cannot be created you can try to write to filename that you don't have permissions to (new PDO("sqlite:/");)
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Connect from PHP to an Oracle DB using an Oracle Wallet
We are planning to implement Oracle Wallet. It works from sqlplus as shown below.
That shows that wallet functionality is working.
export ORACLE_HOME=/afs/engg/g/lcls/package/oracle/product/11.1.0.6/client
export PATH=$ORACLE_HOME/bin:${PATH}
export TNS_ADMIN=/afs/engg/g/lcls/tools/oracle/wallets/engg_reader
$ sqlplus /#enggdev
SQL> show user
USER is "ENGG_READER"
I am so far unsuccessful to make it work from php. We have php installed with
OCI8 extension. Please guide me especially about oci_connect command and it's
syntax.
This is my php file -
<?php
// Create connection to Oracle
PutEnv("ORACLE_HOME=/afs/engg/g/lcls/package/oracle/product/11.1.0.6/client");
PutEnv("TNS_ADMIN=/afs/engg/g/lcls/tools/oracle/wallets/engg_reader");
$conn = oci_connect("/", "", "$TNS_ADMIN", null, OCI_CRED_EXT);
if (!$conn) {
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else {
print "Connected to Oracle!\n";
}
// Close the Oracle connection
oci_close($conn);
?>
When I execute the command $ /mccelog/package/php/php-5.4.7/bin/php connect4.php
Warning: oci_connect(): OCIEnvNlsCreate() failed. There is something wrong
with your system - please check that ORACLE_HOME and LD_LIBRARY_PATH are set and
point to the right directories in /afs/engg/u/cd/divekar/technical/connect4.php on
line 7
Note that I have properly set ORACLE_HOME and LD_LIBRARY_PATH.
Line 7 is oci_connect string which is causing that error. How to connect to
Oracle database using oci8/oci_connect ?
Thanking you in advance.
Regards.
-Shashi Divekar
THANK YOU. Your answer is useful. I tried to assign points but it's not allowing me to assign points. I did some experiements and now this works for me .
I set some environmen variables and then run the php script.
export ORACLE_HOME=/nfs/lcls/package/oracle/product/11.1.0.6/client
export TNS_ADMIN=/nfs/lcls/tools/oracle/wallets/elog_reader
I run following test php script from command line and it's working now. At lease now I have OCI8 and Oracle wallet working together.
php connect.php
<?php
// Create connection to Oracle
$conn = oci_connect("/", "", "MCCODEV", null, OCI_CRED_EXT);
if (!$conn) {
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else {
print "Connected to Oracle!\n";
}
// Close the Oracle connection
oci_close($conn);
?>
Thanks again for your help and for the very good information provided. I will also update my stackoverflow.com post.
Regards.
-Shashi Divekar
Sorry about this question but i never worked with SQLite, and i gone through the SQLite site , downloaded "Precompiled Binaries For Windows" files tried to make them usable for my purpose but i couldnot, i saw with working tutorials with databases,tables.
My tasks is to get data from SQLite and put them into magento mysql DB. So for that i have got the SQLite DB dump file as .db3 file, txt file which has size of db dump file(20110125_SIZE).
So how can i import it into the SQLite. Please if anyone of you worked with SQLite3 help me to understand the .db3 file and how can i see the records from them.
I have got sqlite3 db dump as dbname.db3,
So then i tried to connect this sqllite from php. Here is my sample code which i got it from forum.
$db = openDatabase();
unset($db);
function openDatabase() {
$dbFile = realpath('dbname.db3');
echo $dbFile . "\n";
$needDbCreate = !file_exists($dbFile);
$db = new SQLiteDatabase($dbFile) or die((file_exists($dbFile)) ? "Unable to open" : "Unable to create");
if($needDbCreate) {
}
return $db;
}
But i am getting fatal exception.
Fatal error: Uncaught exception
'SQLiteException' with message
'SQLiteDatabase::_construct() [sqlitedatabase.--construct]:
file is encrypted or is not a
database' in
C:\wamp\www\SQLITE\index.php:23 Stack
trace: #0
C:\wamp\www\SQLITE\index.php(23):
SQLiteDatabase->_construct('C:\wamp\www\SQL...')
1 C:\wamp\www\SQLITE\index.php(15): openDatabase() #2 {main} thrown in
C:\wamp\www\SQLITE\index.php on line
23
But when i tried the same sqldump with the PDO , i got connected.
try {
$dbh = new PDO("sqlite:C:\wamp\www\SQLITE\dbname.db3");
echo 'Db Connected <br/>';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
here i do not know the list of tables available in this dump so how can i query them to list and then getting records from them.
Please help me to solve this issue ti get connected and browse the tables from php.
Sorry for posting here in answer
section, i wanted to highlight the
code.
Thanks
On a system where SQLite is installed, you will usually also have the sqlite3 command line program. It can be used both from the command line or in interactive mode to dump data or load it into a (binary) database file.
sqlite3 ./database.file
This will give you the interactive prompt, where you can issue SQL commands or special commands such as .help or .dump.
There are also more graphical tools, but they are probably overkill for what you want to do.
Edit: seeing your reply (currently in the answer section), it seems that your .db3 file simply is not in the binary SQLite3 format, but instead perhaps a dump. That would be a problem. If it is a dump, you'd have to load it into a proper database file first.
cat yourdump.sql|sqlite3 ./realdb.db3
call three times
$rr=exec("sqlite3 database.db \".separator '|'\" ");
$rr=exec("sqlite3 database.db \".import myfile.txt tablename\" ");
$rr= exec ("sqlite3 database.db \"pragma encoding = 'utf-8'\" ");
or
single line
$rr=exec("sqlite3 database.db \".separator '|'\" && sqlite3 database.db \"pragma encoding = 'utf-8'\" && sqlite3 database.db \".import myfile.txt tablename\" ");