You are not logged in.
Pages: 1
I am learning PHP from w3school. I had followed w3school and arch wiki until now and I've setup apache ,php and mariaDB according to arch wiki . everything worked fine until I stared mysql with php.
have un-commented these lines in /etc/php/php.ini
extension=mysqli
extension=pdo_dblib
extension=pdo_mysql
extension=pdo_sqlitestill I can't get fowling code working:-
<?php
$servername = "localhost";
$username = "smit";
$password = "bill";
try {
$conn = new PDO("mysql:host=$servername", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "CREATE DATABASE myDBPDO";
$conn->exec($sql);
echo "Database created successfully<br>";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?> When an this code in firefox I get this error:-
could not find driver Pls I am very new to all these web techs . We have all these subjects at 3rd Year and I am at 1st Year of engineering.The above code runs fine on my sister windows laptop (with her $username and $password ) , So I thought I should post here instead of stackoverflow
Last edited by Smit (2018-06-04 17:05:42)
live and let live
Offline
The mysql extension has been deprecated for a long time and has been removed with php 7, you are advised to use mysqli instead: http://php.net/manual/en/book.mysqli.php
Whoops, more coffee for me, see below...
Last edited by V1del (2018-06-04 10:58:34)
Offline
I'm not sure how it runs on your sisters system as the most conspicuous problem is that you never specify what database to connect to. Is your sister using mariadb? Perhaps it's due to how windows handles (or fails to handle) permissions and on her system it is actually connecting to the mysql root database itself. That's just wild speculation though. The DSN in the PDO constructor should include a dbname, e.g.:
$conn = new PDO("mysql:dbname=$database;host=$servername", ...);EDIT: FWIW, mysql/PDO may be deprectated, but it still works just fine. So while it may be good advice to switch to mysqli for new code, it doesn't explain this error. V1del, does the wiki need updating here:
https://wiki.archlinux.org/index.php/PH … .2FMariaDB
EDIT 2: Nope, V1del, it looks like you are confusing mysql with pdo_mysql, the latter is still very much in use, and I'd personally recommend it: https://wiki.archlinux.org/index.php/PH … .2FMariaDB
EDIT 3: I see one user submitted comment in the docs suggesting that dbname is optional as you can connect without it in order to create a new database. This seems an odd thing to do from php to me though, and I'd still suspect a permissions issue, perhaps httpd is not allowed to connect to the rood db. But in hindsight the error message indicates something more basic: have you restarted apache (and mysqld)?
Last edited by Trilby (2018-06-04 11:04:19)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
@Triby My sister is using XAMP(with MySQL, Not mariaDB) with Windows 10 . Also I copied and pasted this code from w3school.com and just change the $username and $password variables . Also I had
sudo systemctl restart mariadb.service && sudo systemctl restart httpd.serviceAlso is this code running in your arch linux machine ?
Last edited by Smit (2018-06-04 11:31:55)
live and let live
Offline
A slightly modified version works, yes:
<?php
$servername = "localhost";
$username = "myuser";
$password = "nonyabizzness";
try {
$conn = new PDO("mysql:host=$servername", $username, $password);
echo "So far so good";
}
catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}Obviously the username and password are changed above. If I use invalid user name and/or password I get an Access denied error, not the one you are getting. So it would seem the code itself is not the issue, but your servers configuration still needs work.
Can you access mysql from the command line (with that username and password)? Are there any errors from mariadb?
Last edited by Trilby (2018-06-04 11:49:19)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
@Trilby
I think I can access MariaDB shell
% mysql -u smit -p :(
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.1.33-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> exit
ByeLast edited by Smit (2018-06-04 12:04:49)
live and let live
Offline
Yup, looks good.
While the error message wouldn't really make any sense unless it came from the connection command itself failing, it may still be worth simplifying your PHP code to something like my example just to test whether you can connect at all.
You may also want to create a php_info page:
<?php phpinfo(); ?>Then check the output for the line "Loaded Configuration File" and ensure it is in fact reading /etc/php/php.ini and perhaps scroll down to the pdo_mysql section and check the settings (ensure the section is even there too).
Last edited by Trilby (2018-06-04 12:18:23)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Smit, haven’t you changed extension_dir by any chance, or use a non-repo build of PHP? extension_dir should be “/usr/lib/php/modules/” and “/usr/lib/php/modules/pdo_mysql.so” should actually exist. Also enable full error reporting for the development server: it seems pretty strange that PHP hasn’t printed any warnings at all.
Last edited by mpan (2018-06-04 13:15:31)
Offline
Smit, haven’t you changed extension_dir by any chance, or use a non-repo build of PHP? extension_dir should be “/usr/lib/php/modules/” and “/usr/lib/php/modules/pdo_mysql.so” should actually exist. Also enable full error reporting for the development server: it seems pretty strange that PHP hasn’t printed any warnings at all.
I have no idea . Also /usr/lib/php/modules/pdo_mysql.so exist but what should I do with it . I've just following w3school tutorial and arch wiki which mentions nothing
Also @Trilby here is what I get with <?php phpinfo(); ?>

live and let live
Offline
Thanks, what about the "Loaded Configuration File" line, it should be near the top of the page.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
@Trilby it points to /etc/php/php.ini
live and let live
Offline
This may not be the issue, but have you allowed MySQL to listen on a networking port? You haven't disabled networking by having the
skip-networkingline in your my.cnf file have you?
Also, have you verified your user is allowed to access the database via the network? https://wiki.archlinux.org/index.php/My … ote_access
Matt
"It is very difficult to educate the educated."
Offline
mrunion, he's accessing it on localhost. It is recommended to have 'skip-networking' uncommented unless you must access the database remotely.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
The documentation seems to imply skip-networking disables all tcp/ip connections https://mariadb.com/kb/en/library/serve … networking
Offline
And when using pdo_mysql using a hostname of 'localhost' uses a socket connection:
https://secure.php.net/manual/en/ref.pd … ection.php
I'm using this setup in production. It works.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Found It!!!
I had these lines un-commneted in my php.ini file:-
; display_errors
; Default Value: On
; Development Value: On
; Production Value: Off
; display_startup_errors
; Default Value: Off
; Development Value: On
; Production Value: Off
; error_reporting
; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
; Development Value: E_ALL
; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
; html_errors
; Default Value: On
; Development Value: On
; Production value: OnI commented those and change everything works now .
Thank you all for help
live and let live
Offline
That's odd. That would suggest php stopped parsing the ini file due to the syntax errors (of documentation-only lines being uncommented). But it continued to run and didn't give any notice about that failure to read it's ini file? That seems unlikely.
But if it works, it works.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Pages: 1