0

I have looked here (and many many many other places) before i asked my question.

I have tried the following formats '".$user_id." ''$user_id' and even ? to use as my variables holders, but im still getting the same error. I also tried to change from $_GET to $_POST.

Here's my code.

<?php 

    $connection = mysqli_connect("db_name", "user", "pass", "db") or die(mysqli_error($con);

    $user_id = $_GET["user_id"];
    $event_id = $_GET["event_id"];

    $query = "SELECT * FROM  user where user_id BETWEEN('".$user_id."'+1) and ('".$user_id."'+4);";
    $query .= "SELECT * FROM user_in_event WHERE user_id = '".$user_id."' AND event_id = '".$event_id."' ";

    if (mysqli_multi_query($connection, $query) or die(mysqli_error($connection).$query)   ){
        do {
    /* store first result set */
            if ($result = mysqli_store_result($connection) or die(mysqli_error($con))) {
                while ($row = mysqli_fetch_assoc($result) or die(mysqli_error($con))) 
                    $array[] = $row;

                mysqli_free_result($result) or die(mysqli_error($con));
                }   

            } while (mysqli_next_result($connection) or die(mysqli_error($con)));
    }
    header('Content-Type:Application/json');
    echo json_encode($array);

 ?>

and this is the error

mysqli_error() expects parameter 1 to be mysqli null given

and i have checked on the DB itself and the querys works fine

*edit fixed "con" to "connection" but now instead of getting the result required im getting balnk

Community
  • 1
  • 1
styx
  • 1,852
  • 1
  • 11
  • 22
  • Are you really using "BWTWEEN" or is it a typo in the question? (should be "BETWEEN") – nibnut Dec 29 '16 at 20:45
  • okay thanks, i will update the main question because it's still not working `mysqli_error() expects parameter 1 to be mysqli` – styx Dec 29 '16 at 20:52
  • You should create the minimal example with error. In Your code I see a lot of potential problems. You should use prepared statements too. – Michas Dec 30 '16 at 12:41

3 Answers3

1

mysqli_error() expects the first parameter to be a mysqli resource. In your case you are using an undefined variable $con which holds a null value.

A mysqli resource is usually obtained from connecting to the database by calling mysqli_connect.

Thus, $con should be replaced to $connection in mysqli_error().

user3606329
  • 2,405
  • 1
  • 16
  • 28
  • Then you likely have more mistakes in your code i. e. when you fetch the values from your database or $array is simply empty. – user3606329 Dec 29 '16 at 22:29
  • for some reason beyond my understanding the same file is now working properly – styx Dec 30 '16 at 15:13
0

$query = "SELECT * FROM user where user_id BETWEEN ('".$user_id."'+1) ...
try this:
$query = "SELECT * FROM user where user_id BETWEEN ('".($user_id+1)."') ...

Alex K.
  • 18
  • 2
-1

BETWEEN is looking for simple numbers. Example SELECT * FROM Products WHERE Price BETWEEN 10 AND 20;

Your query has some complicated formatting that is probably causing your null value. I notice you have two ";" where there should be just one to close the query. It sometimes helps to simplify.

Try creating your search values before the query:

$start=$user_id+1; $end=$user_id+4;

Then your query becomes $query = "SELECT * FROM user where user_id BETWEEN $start and $end";

Hope this helps.