0
    <?php
// connect to MySQL
 $connect = new PDO ("localhost", "username", "")
 or die ( "Hey loser, check your server connection");

 // make sure we`re using the right database 
 mysql_select_db ( "moviesite");

 // insert data into "movie" table
 $insert= "INSERT INTO movie (movie_id, movie_name, movie_type,
    movie_year, movie leadactor, movie_director)".
"VALUES (1, 'Bruce Almighty', 5 , 2003, 1, 2), " .
"(2, 'Office Space', 5, 1999, 5, 6),".
"(3, 'Grand Canyon', 2, 1991, 4, 3)";
$results = mysql_query($insert)
or die (mysql_error());

// insert data into "movietype" table
$type = "INSERT INTO movietype (movietype_id, movietype_label) ".
"VALUES (1, 'Sci Fi'), " .
"(2, 'Drama'), " . 
"(3, 'Adventure'),".
"(4, 'War'),".
"(5,'Comedy'),".
"(6, 'Horror'),".
"(7,'Action'),".
"(8,'Kids')";
$results = mysql_query($type)
or die (mysql_error());
// insert data into "people" table
$people = "INSERT INTO people (people_id, people_fullname, " .
    "people_isactor, people_isdirector) " . 
"VALUES (1, 'Jim Carrey', 1, 0), " . 
"(2, 'Tom Shadyac', 0, 1), " .
"(3, 'Lawrence Kasdan', 0, 1) , " .
"(4, 'Kevin Kline', 1, 0), " .
"(5, 'Ron Livingston', 1, 0), " .
"(6, 'Mike Judge', 0, 1)"; 
$results = mysql_query($people)
or die (mysql_error());
echo "Data inserted succsesfully!";
?> 

Hey guys. I have a problem. When i run this code, i get this error:

( ! ) Fatal error: Uncaught exception 'PDOException'

with message

'invalid data source name' in C:\wamp\www\moviedata.php on line 3 ( ! ) PDOException: invalid data source name in C:\wamp\www\moviedata.php on line 3

If someone can help me fix this , i will be thankful .

Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
Jough Drak
  • 17
  • 3
  • 1
    Possible duplicate of [Uncaught exception 'PDOException' message 'invalid data source name'](http://stackoverflow.com/questions/19740829/uncaught-exception-pdoexception-message-invalid-data-source-name) – bhooks Dec 30 '15 at 21:33
  • 1
    The exception tells you what the problem is: 'invalid data source name'. The first argument to the PDO constructor must be a valid DSN, which 'localhost' is not. Read [here](http://php.net/manual/en/pdo.construct.php) about DSNs. – Don't Panic Dec 30 '15 at 21:35
  • 1
    Maybe a dup, but this question does not seem to be overriding PDO, maybe he is facing a different problem. – Jay Elston Dec 30 '15 at 21:36
  • 2
    Also, if you are trying to convert this from mysql to pdo, you need to convert it all. You cannot have part of your code use pdo and part use mysql. – Don't Panic Dec 30 '15 at 21:37

2 Answers2

2

You are calling

mysql_select_db ( "moviesite");

But that is not part of PDO, it is part of MySQL.

You should be doing:

$DB_HOST = "";
$DB_NAME = "";
$DB_PASS = "";
$DB_PORT = 3306;
$DB_USER = "";

$dsn = "mysql:host=" . $DB_HOST . ";port=" . $DB_PORT . ";dbname=" . $DB_NAME . ";charset=utf8";

$db_connection = new PDO($dsn, $DB_USER, $DB_PASS);
Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
2

Don't mix mysql_* functions and PDO.
Check the syntax of the PDO_MySQL DSN.

<?php
$pdo = new PDO('mysql:host=localhost;dbname=moviesite;charset=utf8', 'username', '', array(
    PDO::ATTR_EMULATE_PREPARES=>false,
    PDO::MYSQL_ATTR_DIRECT_QUERY=>false,
    PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
));

$pdo->exec("
    INSERT INTO movie
        (movie_id, movie_name, movie_type, movie_year, movie leadactor, movie_director)
    VALUES
        (1, 'Bruce Almighty', 5 , 2003, 1, 2),
        (2, 'Office Space', 5, 1999, 5, 6),
        (3, 'Grand Canyon', 2, 1991, 4, 3)
");
// and so on and on
VolkerK
  • 95,432
  • 20
  • 163
  • 226