-1

I cannot find the solution, spending hours of troubleshooting, though. Frustration growns steadily.

The problem: I try, after transferring a web page to a new provider that requires the oop approach for mysql interfaces, to reset all the sql command lines. This works fine. But there is one part that I cannot get working. The called php-file movie.php looks like:

<?php

    include 'scripts/init.php';

    ...   

    $cameraString = AssignPerson( $movieID, 'camera' );

 ?>

The important bits from init.php are:

<?php
   session_start();

   include 'scripts/connect2SQL.php';   
   include 'scripts/logInOut.php';  
   include 'scripts/classes_functions.php';

?>

The connect2SQL.php reads

<?php

$connection2MySQL = mysqli_connect(  "localhost" , "***" , "***", "***");
mysqli_query( $connection2MySQL , "SET CHARACTER SET 'utf8'" );
mysqli_query($connection2MySQL , "SET SESSION collation_connection ='utf8_unicode_ci'" );

?>

So the variable $connection2MySQL represents the oop connection to the database.

The function AssignPerson( $movieID, 'camera' ); in the movie.php is defined in the classes-file classes_functions.php. Here, the essential snippets are

function AssignPerson( $objectID, $type = 'actor' ) {

   $memoryString = '';          

   // THAT FOLLOWING LINE IS NOT WORKING :::    

   $persons = $connection2MySQL->query( "SELECT * FROM actors2movies" );

   if ( mysqli_num_rows( $persons ) ) { 
       while ($person = mysqli_fetch_assoc( $persons )) {   

          $personID = $person['personeID']; 

          $memoryString .= $person['personName'];       
       }
   }    

   return $memoryString;

}

The error message that comes up in the browser looks like:

Fatal error: Uncaught Error: Call to a member function query() on null in /var/www/web26777278/html/filmstadt-quedlinburg/scripts/classes_functions.php:173 Stack trace: #0 /var/www/web26777278/html/filmstadt-quedlinburg/movie.php(354): AssignPerson('800', 'director') #1 {main} thrown in /var/www/web26777278/html/filmstadt-quedlinburg/scripts/classes_functions.php on line 173

The issue here is, that when I delete the lines of the query (and the following ones), the program works. So the error must be in the line i indicated above. On the other hand, if I copy that line directly into the movie.php, the line works! So it must have to do something with the encapsuling thing of php-files into the others.

I checked if I need to repeat the including line of the SQL connection in the classes.php. I checked also, whether there are forgotten brackets, spelling mistakes in the database tables and that kind of things. Can't help myself.

Do you have any idea where I could have another look for the problem?

Greetings and thank you Frank

  • 1
    It doesn't look like `$connection2MySQL` is in scope in that function. You need to pass it in as a parameter. – iainn Dec 28 '17 at 16:54
  • As a security concern you should probably change the credentials for connecting to that database; assuming the user, pass, and database name in your above code are real. – Robert Dec 28 '17 at 16:56
  • After function AssignPerson( $objectID, $type = 'actor' ) {.... add.... global $connection2MySQL; – halojoy Dec 28 '17 at 17:01
  • 1
    @halojoy No, no, NO. [See this answer](https://stackoverflow.com/a/5166527/2370483) for why `global` is a horrible fix – Machavity Dec 28 '17 at 17:03

1 Answers1

1

$connection2MySQL is declared outside function so it is null inside function.

You have to pass connection object to function to work properly

Deepak Kumar T P
  • 1,076
  • 10
  • 20
  • Yes, thank you! Worked it out with your help and learned something new! Actually I was looking with the wrong focus... And yes, it was answered before in the article you referred to! Thanks again, Frank – Frank Müller Dec 28 '17 at 22:37