0

我正在尝试从The Movie Database中获取一些数据,使用将结果插入到本地使用 Xampp 的数据库中。

这是我到目前为止所拥有的,我知道它很脏(函数中的全局变量等)但我在本地运行它——如果有任何有效数据,我只需要它来将数据写入数据库,如果没有,则重新运行函数。

require_once '../vendor/autoload.php';

$token  = new \Tmdb\ApiToken('My API Key - removed');
$client = new \Tmdb\Client($token);
$mid = 1;
$conn = new mysqli('localhost', 'root', '', 'test');
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

moviedetails();

function moviedetails() {

    global $token;
    global $client;
    global $mid;
    global $conn;

    $repository = new \Tmdb\Repository\MovieRepository($client);
    $movie = $client->getMoviesApi()->getMovie($mid);

    var_dump($movie);

        $votecount = $movie['vote_count'];
        $voteaverage = $movie['vote_average'];
        $image = $movie['poster_path'];
        $releasedate = $movie['release_date'];
        $id = $movie['id'];
        $title = $movie['title'];
        $description = $movie['overview'];

        $sql = "INSERT INTO movie (votes, vote_average, poster, release_date, dbid, title, description)
        VALUES ('$votecount', '$voteaverage', '$image', '$releasedate', '$id', '$title', '$description')";

        echo $mid;

        $mid++;
        moviedetails();

}   

我的脚本在 = 3 时失败$mid,但尚未将其他两个有效条目写入我的数据库。

4

2 回答 2

1

You are not actaully inserting to the database, you just have a query prepared to do it, there is no execution of that query.

You need $conn->query($sql) after your $sql = "query" .

于 2016-12-02T19:24:58.367 回答
0

全球 $mid 。也必须在顶级脚本中。

于 2016-12-02T19:22:10.913 回答