Как решить проблему с ошибкой call to a member function query() on null?

0

Урок 63. работа с xml.

Сразу скажу, что скачивал файлы с кодом из урока и сверял со своим - все сходится. Однако ошибка присутствует при попытке спарсить данные из XML файла movies.xml

В браузере отображается Fatal error: Uncaught Error: Call to a member function query() on null in C:\OSPanel\domains\localhost\kinomonster\index.php:14 Stack trace: #0 C:\OSPanel\domains\localhost\kinomonster\index.php(44): insert(Object(SimpleXMLElement), Object(SimpleXMLElement), Object(SimpleXMLElement), NULL, Object(SimpleXMLElement), 1) #1 {main} thrown in C:\OSPanel\domains\localhost\kinomonster\index.php on line 14

Код файла:

<?php

	function insert($name, $desc, $year, $rating, $poster, $category_id) {
		$mysqli = new mysqli('localhost', 'root', '', 'kinomonster');
		
		if(mysqli_connect_errno()) {
		print_f("Соединение не установлено", mysqli_connect_error());
		exit();
		}
		$mysqli->set_charset('utf8');
		$query = "INSERT INTO movie VALUES(null, '$name', '$desc', '$year', '$rating', '$poster', Now(), '$category_id')";
		$result = false;
		
		if($mysql->query($query) or die($mysqli->error)) {
			$result = true;
		}
		return $result;
	}
	
	
	
	$xml = simplexml_load_file("xml/movies.xml") or die("Error: Can not create object");
	
	$title = null;
	$title_origin = null;
	$post = null;
	$rating = null;
	$year = null;
	
	foreach ($xml as $movie_key => $movie) {
		$title = $movie->title_russian;
		$title_origin = $movie->title_original;
		$year = $movie->year;
		
		foreach ($movie->poster->big->attributes() as $poster_key => $poster) {
			$post = $poster;
		}
		
		if($movie->imdb) {
			$rating = $movie->imdb->attributes()['rating'];
		} else {
			$rating = null;
		}
		insert($title, $title_origin, $year, $rating, $post, 1);
	}
	
	echo "<pre>";
	print_r($xml);
	echo "<pre>";

?>
php
xml
mysql

2 ответов

0

Хи, у меня тоже так было :)

1

Нашел ошибку. Не пропечатал букву i в переменной $mysqli на строке 14.

Sign up or Log in to write an answer