RAlexander RAlexander -4 years ago 110
MySQL Question

How to connect to sql database through php/pdo that has been exported from mysql/phpmyadmin as mydatabase.sql?

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:


$dbh = new PDO("$dsn",$username,$password);
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:


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!

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download