如何检查Mysqli查询是否为空?

时间:2017-03-18 04:52:27

标签: php mysql mysqli

我一直在尝试这一点,就像在MySQL中一样,但它无法正常工作。这是我的代码:

<?php

$sql_st_check = "SELECT station_name FROM station WHERE station_code=".$station_code."";
$sql_st_query = mysqli_query($connect, $sql_st_check);

if ($sql_st_query && mysqli_num_rows($sql_st_query) > 0) {
} else {
    $sql_st_insert = "
        INSERT INTO station (id, station_code, station_name, station_title, url, datetime)
        VALUES ('', '".$station_code."', '".$station_name."', '".$station_title."', '".$station_url."', '".$time."')";

    $sql_st_query = mysqli_query($connect, $sql_st_insert);  
}

?>

2 个答案:

答案 0 :(得分:0)

更正此行:

$sql_st_check="SELECT station_name FROM station WHERE station_code='".$station_code."'";

问题是缺少$ station_code

之前和之后的单引号

答案 1 :(得分:0)

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

$sql_st_check="SELECT station_name FROM station WHERE station_code='".$station_code."'";

$result = $con->query($sql_st_check);

if( $result->num_rows > 0 ){
  //do something 
}
else
{
 //insert
}