dongwh1992 2015-12-30 21:31
浏览 19

MySql连接[关闭]

    <?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 .

  • 写回答

2条回答 默认 最新

  • dtpt75860 2015-12-30 21:41
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集