0

I need put a conditional to my PHP code, for load a JSON file, I need to load first query 1, if pos is equal to 1, but if pos is different to 1, to load query 2

How I can do?

My code

        $pos = $_GET["pos"];

        if ($pos = "1"){
        // query 1
            $query = mysqli_query($con, "SELECT * FROM escolar WHERE r_ruta = '$r_ruta' and jornada = '$jornada' and pos = '$pos'");
        }else {
        // query 2
            $query = mysqli_query($con, "SELECT * FROM escolar WHERE r_ruta = '$r_ruta' and jornada = '$jornada' and pos < '$pos' order by pos desc limit 30");
        }
Camilo
  • 1
  • 4
  • 1
    You're using assignment operator in your if statement rather than comparison. Change `$pos = "1"` - to - `$pos == "1"`. – Karlo Kokkak Apr 07 '18 at 01:41
  • 1
    Please be aware of [SQL injection](https://www.w3schools.com/sql/sql_injection.asp) try to use [Prepared Statements](https://www.w3schools.com/php/php_mysql_prepared_statements.asp). Either way you are using `=` operator that results the statement always go to true because you are assigning variable `$pos` to 1 instead of `==` operator – jerome Apr 07 '18 at 01:44

1 Answers1

-1

You're right there. You have to use ==, because if not, you're just setting $pos to 1 with =.

   $pos = $_GET["pos"];

    if ($pos == "1"){
    // query 1
        $query = mysqli_query($con, "SELECT * FROM escolar WHERE r_ruta = '$r_ruta' and jornada = '$jornada' and pos = '$pos'");
    }else {
    // query 2
        $query = mysqli_query($con, "SELECT * FROM escolar WHERE r_ruta = '$r_ruta' and jornada = '$jornada' and pos < '$pos' order by pos desc limit 30");
    }
Adam J
  • 506
  • 3
  • 17