-1

Hello I am pretty sure they are in the same scope and am unsure why I am getting this error now as it was working before.

These are the other files in the same folder as the php file :

dbh.php recipeLookUp.php

It is line 9 that I am getting the error this is the recipeLookUp.php

<?php

    function lookup($sql,$column_name){

        $results_search=array();

        include_once 'dbh.php';

        $results=mysqli_query($conn,$sql);
        $resultsCheck = mysqli_num_rows($results);
        if ($resultsCheck > 0) {
            while ($row = mysqli_fetch_assoc($results)){
                $info= $row[$column_name];
                $results_search[]=$info;
            }
            return $results_search;
        }
    }

and here is my other file

<?php

$dbServername = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'Recipe';

$conn = mysqli_connect($dbServername,$dbUsername,$dbPassword,$dbName);

Im sure it probaly something small but I can't find it please help!!!

  • 3
    `$conn` is not visible inside function `lookup`. – Nick Jan 16 '19 at 22:55
  • I don't think that's the issue, but I'm worried about that `include_once`. If lookup is called twice in the same script execution, 'dbh.php' won't get included again (that's the `_once` part), so `$conn` won't get defined a second time. You could try replacing `include_once` with `require` – Chris Forrence Jan 16 '19 at 22:58
  • my prev comment was a mistake, so I removed it, sorry if I confused you with wrong solution $conn should actually be visible inside that function sense the include is happening within the function – Eden Reich Jan 16 '19 at 23:02
  • you should try what Chris Forrence suggested, check if that include of dbh.php is included elsewhere in that file – Eden Reich Jan 16 '19 at 23:10

1 Answers1

-1

The include_once 'dbh.php'; within your function seems fishy. What are you gonna do if you have other functions which need to perform SQL queries as well?

Instead, I would put the include_once 'dbh.php'; outside (above) your function, and perhaps use require_once instead of include once. And then inside your function, put global $conn; above the mysqli_query call, so your $conn variable is defined there when you pass it along to mysqli_query.

However, if you are worried about creating unnecessary SQL connections (e.g. in case your lookup function is never actually called) I think it would be even better to put this in your dbh.php :

<?php

$conn = null;

function Query($sql)
{
    global $conn;   
    if (!$conn) // will only connect if connection does not exist yet
    {
        $dbServername = 'localhost';
        $dbUsername = 'root';
        $dbPassword = 'xxxxxxxxxxx';
        $dbName = 'Recipe';
        $conn = mysqli_connect($dbServername,$dbUsername,$dbPassword,$dbName);
    }
    return mysqli_query($conn,$sql);
}

?>

And now in your main PHP file, or any PHP file where you include require_once 'dbh.php'; you can directly use Query($sql) to perform queries, which will take care of the connection itself (and only do so if necessary).

RocketNuts
  • 9,958
  • 11
  • 47
  • 88
  • Thanks!!!!! global $conn fix my problem. I didn't even try this because I thought my variable was in the scope of the function. – Phil Schuster Jan 17 '19 at 02:05