Mysql SUM之后的PHP条件

时间:2018-01-26 02:43:31

标签: php

你好,所以我试图得到mysql数据库中一列的总和是它的0 preform查询1 else查询2

问题是,即使来自mysql phpmyadmin的值显示1或2,好像在if条件中存在问题,它总是进入第一个条件而不是其他条件

 <?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 个答案:

答案 0 :(得分:3)

$sum = 0是一项任务,所以$ sum将变为0.修正与==的所有比较,即$sum == 0

正如评论中所建议的那样看看

The 3 different equals