You are not logged in.

#1 2018-06-04 10:33:07

Smit
Member
From: Pune
Registered: 2018-01-02
Posts: 51

Can't work with MariaDB and PHP [SOLVED]

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_sqlite

still 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

#2 2018-06-04 10:46:43

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,435

Re: Can't work with MariaDB and PHP [SOLVED]

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

#3 2018-06-04 10:49:25

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,543
Website

Re: Can't work with MariaDB and PHP [SOLVED]

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

#4 2018-06-04 11:29:24

Smit
Member
From: Pune
Registered: 2018-01-02
Posts: 51

Re: Can't work with MariaDB and PHP [SOLVED]

@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.service

Also is this code running in your arch linux machine ?

Last edited by Smit (2018-06-04 11:31:55)


live and let live

Offline

#5 2018-06-04 11:46:04

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,543
Website

Re: Can't work with MariaDB and PHP [SOLVED]

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

#6 2018-06-04 12:04:12

Smit
Member
From: Pune
Registered: 2018-01-02
Posts: 51

Re: Can't work with MariaDB and PHP [SOLVED]

@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
Bye

Last edited by Smit (2018-06-04 12:04:49)


live and let live

Offline

#7 2018-06-04 12:08:14

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,543
Website

Re: Can't work with MariaDB and PHP [SOLVED]

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

#8 2018-06-04 13:14:40

mpan
Member
Registered: 2012-08-01
Posts: 1,675
Website

Re: Can't work with MariaDB and PHP [SOLVED]

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

#9 2018-06-04 14:03:29

Smit
Member
From: Pune
Registered: 2018-01-02
Posts: 51

Re: Can't work with MariaDB and PHP [SOLVED]

mpan wrote:

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(); ?>

FluxBB bbcode test


live and let live

Offline

#10 2018-06-04 14:27:37

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,543
Website

Re: Can't work with MariaDB and PHP [SOLVED]

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

#11 2018-06-04 14:33:12

Smit
Member
From: Pune
Registered: 2018-01-02
Posts: 51

Re: Can't work with MariaDB and PHP [SOLVED]

@Trilby it points to /etc/php/php.ini


live and let live

Offline

#12 2018-06-04 16:20:16

mrunion
Member
From: Jonesborough, TN
Registered: 2007-01-26
Posts: 1,938
Website

Re: Can't work with MariaDB and PHP [SOLVED]

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-networking

line 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

#13 2018-06-04 16:33:11

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,543
Website

Re: Can't work with MariaDB and PHP [SOLVED]

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

#14 2018-06-04 16:53:18

loqs
Member
Registered: 2014-03-06
Posts: 19,113

Re: Can't work with MariaDB and PHP [SOLVED]

The documentation seems to imply skip-networking disables all tcp/ip connections https://mariadb.com/kb/en/library/serve … networking

Offline

#15 2018-06-04 16:57:57

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,543
Website

Re: Can't work with MariaDB and PHP [SOLVED]

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

#16 2018-06-04 17:05:06

Smit
Member
From: Pune
Registered: 2018-01-02
Posts: 51

Re: Can't work with MariaDB and PHP [SOLVED]

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: On

I commented those and change everything works now .

Thank you all for help


live and let live

Offline

#17 2018-06-04 17:08:43

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,543
Website

Re: Can't work with MariaDB and PHP [SOLVED]

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

Board footer

Powered by FluxBB