0

I have similar code that works with no errors but when I try and query my database I'm getting the several errors. Can anyone help ?

errors:

Notice: Undefined variable: link in C:\wamp64\www\twitter clone\functions.php on line 22

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp64\www\twitter clone\functions.php on line 22

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\wamp64\www\twitter clone\functions.php on line 24

Code;

<?php

session_start();

$link = mysqli_connect("localhost", "root", "pass", "twitter");
if (mysqli_connect_errno()) {
    print_r(mysqli_connect_error());
    exit();
}

if (isset($_GET['function']) == "logout"){
    session_unset();
}

function displayTweets($type) {
    if ($type == 'public'){
        $whereClause = "";
    }

    $query = "SELECT * FROM tweets ".$whereClause." ORDER BY 'datetime' DESC LIMIT 10";

    $result = mysqli_query($link, $query);

    if (mysqli_num_rows($result) == 0) {
        echo "No Tweets";
    }
}

?>
Community
  • 1
  • 1
oversoon
  • 350
  • 2
  • 7
  • 21
  • All the errors are related to the first [**Variable scope**](http://php.net/manual/en/language.variables.scope.php) problem. – FirstOne Nov 29 '17 at 17:43
  • [Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – FirstOne Nov 29 '17 at 17:47
  • Of course! I need to make $link global – oversoon Nov 29 '17 at 17:50
  • Nooooo! I mean, yeah, that would work, but you're better off passing it as a function argument. – FirstOne Nov 29 '17 at 17:51

2 Answers2

1

The problem is that your $link variable is not in the function's local scope.

A way to fix this is by defining the variable in the function's local scope by adding the following line to the function:

global $link

Please have a look at this page to read more about the variable scope in PHP.


Edit:

An even better way would be to inject your connection by adding it as an argument to your function, which would have your code look something like this:

function displayTweets($type, $connection) {
    if ($type == 'public'){
        $whereClause = "";
    }

    $query = "SELECT * FROM tweets ".$whereClause." ORDER BY 'datetime' DESC LIMIT 10";

    $result = mysqli_query($connection, $query);

    if (mysqli_num_rows($result) == 0) {
        echo "No Tweets";
    }
}

After this, you would call your function using displayTweets('public',$link), with 'public' being the type, and $link being your defined connection


Also, in your current function, $whereClause could be undefined. I'm guessing your aware of this, just wanted to state it in case you get errors on that.

InzeNL
  • 82
  • 1
  • 8
  • Maybe it's better to inject the connection as a function argument. – FirstOne Nov 29 '17 at 17:51
  • It definitely would be. Forgot that as I started typing, edited the answer – InzeNL Nov 29 '17 at 17:56
  • When injecting the connection as in the edited answer, don't forget to add it in the argument list in the function call: `displayTweets(something, $link);` – FirstOne Nov 29 '17 at 17:59
0

You have to use $link variable as global variable ($GLOBALS['link']) in displayTweets function or you have to pass $link as parameter.

$result = mysqli_query($GLOBALS['link'], $query);

or

displayTweets('public',$link);