I'm trying to fetch some data from The Movie Database, using a library to insert results in a database locally using Xampp.
Here's what I have so far, I know it's dirty (globals in functions and such) but I'm running this locally - I just need it to write data to the DB if there is any valid data, or rerun the function if not.
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();
}
My script fails when $mid
= 3, but hasn't written the other two valid entries to my database.
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"
.