【发布时间】:2016-04-04 21:39:29
【问题描述】:
<?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!";
?>
大家好。我有个问题。当我运行这段代码时,我得到了这个错误:
( ! ) Fatal error: Uncaught exception 'PDOException'
有消息
'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
如果有人能帮我解决这个问题,我将不胜感激。
【问题讨论】:
-
异常告诉你问题是什么:'无效的数据源名称'。 PDO 构造函数的第一个参数必须是有效的 DSN,而 'localhost' 不是。阅读here 了解 DSN。
-
也许是个骗局,但这个问题似乎并没有压倒 PDO,也许他面临着不同的问题。
-
另外,如果你想把它从mysql转换成pdo,你需要把它全部转换。您不能让部分代码使用 pdo 而部分使用 mysql。