Setting sqlite3 database path using PDO - php

Having a hard time to figure out the path to set for my sqlite3 database using PDO in PHP.
My code as follows:
$handle = new PDO('sqlite:/C:/New folder/sqlite/test.db') or die("Could not open
database");
echo $handle;
$query = "SELECT * FROM student";
Debugging result:
It returns a blank web page instead of printing out the $handle. I already set my desired path where my sqlite database file was stored. What did i miss out ?
Kindly advise.

Firstly you need to know what PDO drivers are installed, make a blank file with in it:
<?php phpinfo(); ?>
Then look for the PDO section and you should see somthing along the lines of:
PDO drivers mysql, sqlite, sqlite2
If you only have mysql then you need to install the sqlite pdo driver. Use google for that, dont forget also check your production server too, else your create something you possibly cant use on your hosting.
As the PDO driver is much like the old sqlite_open() it will create the database file for you if its not found.
also as you may be aware spaces in file paths are not so good and can cause problems,
Instead of using:
sqlite:/C:/New folder/sqlite/test.db you should at least rename your New Folder to somthing else: sqlite:/New_folder/sqlite/test.db

Related

PDO mysqlnd; Undefined constant PDO::MYSQL_ATTR_READ_DEFAULT_GROUP

I have a LAMP stack, and I thought it might be nice to use the MySQL configuration file to allow PDO to connect to the MySQL database without the need to place DB credentials within our code structure
So I created a group in /etc/my.cnf.d/gggg.cnf
[clientGggg]
user=uuuu
password=pppp
database=dddd
and can now connect to the DB through the command line with mysql --defaults-group-suffix=gggg
I then tried to create a test DB connection in PHP with
$db = new PDO("mysql", null, null, [PDO::MYSQL_ATTR_READ_DEFAULT_GROUP => 'gggg']);
but I received the error
Undefined constant PDO::MYSQL_ATTR_READ_DEFAULT_GROUP
Checking the PDO docs for this attribute reveals the following:
PDO::MYSQL_ATTR_READ_DEFAULT_GROUP (int)
Read options from the named group from my.cnf or the file specified with MYSQL_READ_DEFAULT_FILE. This option is not available if mysqlnd is used, because mysqlnd does not read the mysql configuration files.
and checking my phpinfo(); reveals I am indeed using mysqlnd
Why is this limitation in place? Is there a workaround? Or am I doomed to write/load my credentials into PHP?

Using ADOdb with ODBC and Oracle

I've been given access to an Oracle Server via ODBC and tested the connection using Oracle SQL Developer. These are the connection constants I've set in PHP:
define('APP_DB_HOST', '192.168.1.1');
define('APP_DB_PORT', '1521');
define('APP_DB_USER', 'MyUser');
define('APP_DB_PASS', 'MyPass');
define('APP_DB_SID', 'MyDatabaseSID');
define('APP_DB_SCHEMA', 'MyDatabaseSchema');
With ADOdb/ODBC, I should be able to use the below, so that I don't need to involve a tnsnames.ora entry:enter link description here
$dsn = '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST='.APP_DB_HOST.')(PORT='.APP_DB_PORT.'))(CONNECT_DATA=(SERVICE_NAME='.APP_DB_SID.')));User Id='.APP_DB_USER.';Password='.APP_DB_PASS.';';
$db->PConnect($dsn, APP_DB_USER, APP_DB_PASS, APP_DB_SCHEMA);
I get the ADOdb Warning:
Warning: odbc_connect(): SQL error: [unixODBC][Driver Manager]Data source name not found, and no default driver specified, SQL state IM002 in SQLConnect in [..]/adodb/drivers/adodb-odbc_oracle.inc.php on line 87
Have others done this before, and if so, how so?
Beyond ADOdb, if anybody has a less complicated method of connecting by ODBC to Oracle with PHP7.4+, please do share.
I would also suggest you to move to the OCI8 native driver if you have a bit of time and not too much refactoring to do.
Your server should already have an Oracle client installed (to make the current TNS and ODBC work), so the only thing to do is to install the OCI8 Oracle DDL PHP extension. Just copy it to php/ext/ and load it in your php.ini
In the meantime, you can test if the following code works:
require_once("include/adodb5/adodb.inc.php"); //depends on your adodb folder
$conn = NewADOConnection("oci8");
$conn->connect(APP_DB_HOST, APP_DB_USER, APP_DB_PASS, APP_DB_SID);
For info, you can check if your system admin has already installed the OCI8 extension by looking for the OCI8 section in the PHPINFO :
If that is an option for you, I would strongly recommend to connect using the native oci8 driver, instead of relying on ODBC. Refer to ADOdb documentation for connection examples.
If you're stuck with ODBC, then I believe you need to adjust your DSN to specify the name of the driver you want to use in the connection string, e.g. Oracle in instantclient_19_6
Driver={OdbcDriverName};Server=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xxx)(PORT=xxx))(CONNECT_DATA=(SERVICE_NAME=xxx)))
In my opinion it's better to specify the user id and password in the function call, i.e. $db->connect($dsn, $username, $password), but if you must have it in the connection string, you may want to try it with uid=xxx;pwd=xxx instead of User Id=xxx;Password=xxx
As an alternative, you could also create a System DSN in your ODBC configuration, and just refer to it by name, instead of hardcoding the TNS connection string directly in your code.
See also Create a DSN for the function odbc_connect for Oracle.

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.

How can I use PDO with MSSQL in PHP from a Windows dev envirenment?

I am developing an application that needs to get data from an outside MSSQL database. I spent a lot of time trying to get various methods of connection to MSSQL with PHP, but there were several routes which were depreciated.
On my production environment running Debian, I was able to make a connection with PDO_DLIB and FreeTDS with something like this:
$this->db = new \PDO('dblib:host='.$thedb_host_prod.';dbname='.$thedb_database_name_prod, $thedb_database_user, $thedb_database_pass);
On Windows, MSSQL is depreciated. I believe I'm using the Microsoft SQL Server Driver and was only able to get it to work with ODBC, which looks something like this:
$dsn = "Driver={SQL Server};Server=".$thedb_host_dev.";Database=".$thedb_database_name_dev;
$this->odbc = odbc_connect($dsn, $thedb_database_user, $thedb_database_pass);
Then, the problem becomes, in each method I need to do something differently for ODBC than I do for DLIB.
public function exampleMethod(){
// logic and create the query in $query
if($this->dev == false){
// PRODUCTION
try {
$stmt = $this->db->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
} else {
// DEVELOPMENT
$query = $query;
$stmt = odbc_exec($this->odbc, ($query));
$result = array();
while($currentRow = odbc_fetch_object( $stmt )){
$jobNumber = $currentRow->Code; // Set object key to jobNumber
array_push($result, $currentRow);
}
}
}
This actually works, but the problem is, with how the query for ODBC needs to be prepared vs how the DBLIB query should be prepared, means that if I don't want to write the query twice in each method, I have to create it before each action. This is really bad because it means I'm not putting my variables into the query with PDO's bindValue.
So, has anyone been able to get PDO work with PHP 5.4 and MSSQL in a Windows environment? Does anyone see a way of securing the query in a way that doesn't make me duplicate the query in each method, once for ODBC and once for DBLIB?
My plan currently is to develop the application out and then remove all of the ODBC stuff which will allow me to put the query in the $stmt properly, avoiding this problem. But until then, it's making development a huge pain.
Oooook, so I finally got it working !
Here are the libraries / drivers for Sql-server connection via C/C++/java/PHP/etc.
Precisely, here are the Windows drivers for PHP.
Check out this page to know which version you need to get
They all exist in have 32 and x64, as well as "Thread safe" (ts) and "non-thread-safe" (nts) versions.
From what I read, nts versions are to be used if you work with IIS.
Usage :
Download and extract the package you need.
in my case, package 3.2, for php 5.6
Take necessary drivers
in my case, php_sqlsrv_56_ts.dll and php_pdo_sqlsrv_56_ts.dll
Put them in your php extensions directory
in my case, [...]\php5630vc11x86x170623162800\ext
If you wonder where this dir is inside your PHP, it should be quite easy to find, since you have plenty of other dlls here.
Likely, php_pdo_mysql.dll, php_pdo_pgsql.dll, etc.
Modify your php.ini
Be careful with Wamp, it seems to have one in the php directory AND another one in the apache dir.
Add lines to load the two extensions.
You can add them at the end of your file, or with the other extensions loading.
In my case, here's what I added :
;SQL-srever extensions
extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_56_ts.dll
This part was for the PHP/PDO side.
For the driver to actualy work, you also need your (Windows) machine to have the ODBC drivers installed.
Feel free to try your connection already, but if it whines that you need the ODBC driver, go get it here :
Microsoft® ODBC Driver 11 for SQL Server®
And last, but not least, be sure to use the right format for your PDO connection.
$conn = new PDO ("sqlsrv:Server=$srv_host;database=$srv_dbname";
For comparison, here's what I wsa using on my linux server :
$conn = new PDO ("dblib:host=$srv_host:$srv_port;dbname=$srv_dbname", "$srv_username", "$srv_password");
I thought this syntax was common to all PDO drivers, but it appears I was wrong.
Here is the PHP PDO driver documentation.
I just actually had to do some work in php connecting to a MSSQL server. I did have to downgrade to php 5.4 due to the fact that the php_pdo_sqlsrv.dll is not updated for 5.5. For the dll files check here. But now down to the code I used to connect once you have the .dll files in the right place.
try {
$db = new PDO("sqlsrv:Server={$host};Database={$database}", $userName, $password);
}catch(PDOException $e){
die("failed to connect");
}
Just a standard PDO connection. Just in order to get it to work you must make sure that the .dll files are in the php directory.
I hope that answers at least part of the question.
I have worked with PHP 5.4 and SQL Server with PDO on Windows.
I strongly recommend using Microsoft's Web Platform Installer to set everything up. You can use it to install PHP, a local version of SQL Server Express to develop on, the official PHP driver for SQL Server, and IIS, all set up to work together.
One note of caution: The last release of the SQL Server PDO driver was in April, 2012. I reported a bug against it last year and was told that it's in "limited support", which apparently translates to "you're on your own". In any case, it worked reasonably well.

mysqli not working, module not found

I am new to PHP and web servers, and i was going to make a website using PHP. I found a free login script that uses mysqli. Every time I go on the page it says I don't have mysqli? I am running Apache with PHP3. The mysqli part of the script is thus:
// if the connection is successfully established
if($conn = new mysqli($this->conn_datas['localhost'], $this->conn_datas['daztestc_testdaz'], $this->conn_datas['dj2403ms81'], $this->conn_datas['daztestc_daztest'])) {
$sql = "SET NAMES 'utf8'";
$conn->query($sql);
$this->conn = $conn; // add the connection in the $conn property
}
php3? mysqli was not introduced until php5. where did you find a server with such an old copy of php? and is any of the code working? as most things have changed since php3.
Probably you don't have MySQLI extension installed. Check this page to know how to enable it. Ah... of course... you can't use it on PHP3... so if you really want, upgrade your server.

Categories