无法从mysql数据库加载数据

时间:2014-11-15 16:01:00

标签: php mysql database sqlite pdo

这是我的问题。我正在设置一个日历,我无法从sqlite数据库切换到我的mysql数据库。

问题是DayPilot在从mysql传递时没有加载事件。任何帮助都是值得赞赏的。谢谢。

<?php

//$db_exists = file_exists("daypilot1.sqlite");
//$db = new PDO('sqlite:daypilot.sqlite'); This works

// i tried these two ways. both of them did not work.

$db = new PDO("mysql:host=localhost;dbname=his", "root", "123"); 


/* $servername = "localhost";
$username = "root";
$password = "123";


try {
    $db = new PDO("mysql:host=$servername;dbname=his", $username, $password);
    echo "Connected successfully";
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
  */

if (!$db) {
    //create the database
    $db->exec("CREATE TABLE IF NOT EXISTS events (
                        id INTEGER PRIMARY KEY, 
                        name TEXT, 
                        start DATETIME, 
                        end DATETIME,
                        resource VARCHAR(30))");

    $messages = array(
                    array('name' => 'Event 1',
                        'start' => '2013-05-09T00:00:00',
                        'end' => '2013-05-09T10:00:00',
                        'resource' => 'B')
                );

    $insert = "INSERT INTO events (name, start, end, resource) VALUES (:name, :start, :end, :resource)";
    $stmt = $db->prepare($insert);

    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':start', $start);
    $stmt->bindParam(':end', $end);
    $stmt->bindParam(':resource', $resource);

    foreach ($messages as $m) {
      $name = $m['name'];
      $start = $m['start'];
      $end = $m['end'];
      $resource = $m['resource'];
      $stmt->execute();
    }

}

?>

1 个答案:

答案 0 :(得分:0)

试试这个

<?php

//$db_exists = file_exists("daypilot1.sqlite");
//$db = new PDO('sqlite:daypilot.sqlite'); This works

// i tried these two ways. both of them did not work.

$db = new PDO("mysql:host=localhost;dbname=his", "root", "123"); 


/* $servername = "localhost";
$username = "root";
$password = "123";


try {
    $db = new PDO("mysql:host=$servername;dbname=his", $username, $password);
    echo "Connected successfully";
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
  */

if (!$db) {
    //create the database
    $db->exec("CREATE TABLE IF NOT EXISTS events (
                        id INTEGER PRIMARY KEY, 
                        name TEXT, 
                        start DATETIME, 
                        end DATETIME,
                        resource VARCHAR(30))");

    $messages = array(
                    array('name' => 'Event 1',
                        'start' => '2013-05-09T00:00:00',
                        'end' => '2013-05-09T10:00:00',
                        'resource' => 'B')
                );

    $insert = "INSERT INTO events (name, start, end, resource) VALUES (:name, :start, :end, :resource)";
    $stmt = $db->prepare($insert);


    foreach ($messages as $m) {
      $name = $m['name'];
      $start = $m['start'];
      $end = $m['end'];
      $resource = $m['resource'];
    }

    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':start', $start);
    $stmt->bindParam(':end', $end);
    $stmt->bindParam(':resource', $resource);
    $stmt->execute();

}

?>

我没有测试过,但希望它可以解决你的问题。