0

Hello so i am trying to get the sum of a column in the mysql db is its 0 preform query 1 else query 2

Problem is it always goes to the first condition not the else even when value from mysql phpmyadmin shows 1 or 2 as if there is a problem in the if condition

 <?php
$servername = "localhost:3306";
$username = "****";
$password = "*****";
$dbname = "****";


$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

    $search = $_GET['search'];
    $sqlx = "SELECT SUM(`verified`) AS value_sum FROM Numbers WHERE `number` = ".$search." ";
    $resultx = $conn->query($sqlx);
    $sum = $row['value_sum'];

    if ($sum == 0)
    {
        $sql = "SELECT * FROM `Numbers` WHERE (`number` LIKE '%".$search."%' or `name` LIKE '%".$search."%') and `blocked` = 0 and `verified` = 0 GROUP BY `name` Limit 20";
        $result = $conn->query($sql);
    }
    else
    {

        $sql = "SELECT * FROM `Numbers` WHERE (`number` LIKE '%".$search."%' or `name` LIKE '%".$search."%') and `blocked` = 0 and `verified` = 1 GROUP BY `name` Limit 1";
        $result = $conn->query($sql);
    }

1 Answers1

3

$sum = 0 is an assignment so $sum will become 0. Fix all comparisons with == ie $sum == 0

As suggested in the comments take a look at

The 3 different equals

Bizmate
  • 1,835
  • 1
  • 15
  • 19
  • i changed it yet now it only goes to if not the else i am testing with value 1 and value 0 – Ahmed Saleh Jan 26 '18 at 02:52
  • What is the actual query returning? $sqlx? – Bizmate Jan 26 '18 at 02:53
  • the query from mysql phpmyadmin returns a column with one row value either 0 or more – Ahmed Saleh Jan 26 '18 at 02:55
  • In your script there is no sign of where `$row` is coming from. It is output the first time after `$resultx = $conn->query($sqlx);` out of nowhere,also the `$conn` object . Where are they coming from? Please show the full script and output the actual result from the `$sqlx` query. People cannot guess what is happening on your machine. – Bizmate Jan 26 '18 at 03:00