0

I am trying to connect to a database "customers.sql" using PDO. Here is the code:

<?php
  $servername = "localhost";
  $username = "root";
  $password = "";

  try {
    $conn = new PDO("mysql:host=$servername;dbname=customers.sql", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
  }

  catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
  }
?>

And here is my folder structure:

-WebPage
  -index.php
  -customers.sql        << this is the database >>

I am running WAMP Server. The server name is "localhost", the username is "root" and there is no password, i.e. the password is "". The server returns:

Connection failed: SQLSTATE[HY000] [1049] Unknown database 'customers.sql'.

Somebody please point out my mistake. Thanks in advance.

EDIT

Here is the code in "customers.sql":

SET IDENTITY_INSERT Customer ON
INSERT INTO [Customer] ([Id],[FirstName],[LastName],[City],[Country],[Phone])VALUES(1,'Maria','Anders','Berlin','Germany','030-0074321')
INSERT INTO [Customer] ([Id],[FirstName],[LastName],[City],[Country],[Phone])VALUES(2,'Ana','Trujillo','México D.F.','Mexico','(5) 555-4729')
INSERT INTO [Customer] ([Id],[FirstName],[LastName],[City],[Country],[Phone])VALUES(3,'Antonio','Moreno','México D.F.','Mexico','(5) 555-3932')
INSERT INTO [Customer] ([Id],[FirstName],[LastName],[City],[Country],[Phone])VALUES(4,'Thomas','Hardy','London','UK','(171) 555-7788')
INSERT INTO [Customer] ([Id],[FirstName],[LastName],[City],[Country],[Phone])VALUES(5,'Christina','Berglund','Luleå','Sweden','0921-12 34 65')
INSERT INTO [Customer] ([Id],[FirstName],[LastName],[City],[Country],[Phone])VALUES(6,'Hanna','Moos','Mannheim','Germany','0621-08460')
INSERT INTO [Customer] ([Id],[FirstName],[LastName],[City],[Country],[Phone])VALUES(7,'Frédérique','Citeaux','Strasbourg','France','88.60.15.31')
INSERT INTO [Customer] ([Id],[FirstName],[LastName],[City],[Country],[Phone])VALUES(8,'Martín','Sommer','Madrid','Spain','(91) 555 22 82')
INSERT INTO [Customer] ([Id],[FirstName],[LastName],[City],[Country],[Phone])VALUES(9,'Laurence','Lebihan','Marseille','France','91.24.45.40')
INSERT INTO [Customer] ([Id],[FirstName],[LastName],[City],[Country],[Phone])VALUES(10,'Elizabeth','Lincoln','Tsawassen','Canada','(604) 555-4729')
INSERT INTO [Customer] ([Id],[FirstName],[LastName],[City],[Country],[Phone])VALUES(11,'Victoria','Ashworth','London','UK','(171) 555-1212')
INSERT INTO [Customer] ([Id],[FirstName],[LastName],[City],[Country],[Phone])VALUES(12,'Patricio','Simpson','Buenos Aires','Argentina','(1) 135-5555')
...
3
  • 3
    remove .sql from the name Commented Feb 23, 2017 at 19:44
  • Is there a database with the name of customers.sql? Commented Feb 23, 2017 at 19:44
  • It looks like you are trying to use a file as a database, and that will not work. Commented Feb 23, 2017 at 19:45

4 Answers 4

2

There is some confusion.

You say you are using wamp, and your code suggests that you are trying to connect to a mysql database.

A .sql file is not a database. It just contains sql statements. Potentially it can contain all statements needed to create and populate a database. But of course, this is only speculation before we know the content of the file.

If you have wamp installed as you say, you have a running mysql server. You have to execute your .sql file against the server to create the database. The easiest way to do this, is to use phpmyadmin, that also comes with wamp. You should be able to access it at http://localhost/phpmyadmin.

I hope this helps. For sure, you will have to read some articles about mysql server before you will understand all this thoroughly. Have fun learning.

Sign up to request clarification or add additional context in comments.

Comments

1

If your database is named "customers", remove .sql.

Otherwise are you trying to use a file named customers.sql? That can't be done. That statement is to set up a connection to a database.

Comments

1

Is this truly a filesystem file? If so, are you sure the format is not sqlite instead of mysql?

How about opening the database file using

$conn = new PDO('sqlite:customers.sqlite');

Comments

1

customers.sql is not a table, so you don't need the .sql. I usually use this:

$conn = new mysqli($servername, $username, $password);
$conn->query('use customers');

Documentation: http://php.net/manual/en/book.mysqli.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.