php - MySQL 使用不存在列中的数组

标签 php mysql forms select

我不确定我的标题措辞是否正确。希望我能以简洁的方式解释这一点。我正在尝试从数据库中选择所有内容:

if (! empty ( $fname ) && ! empty ( $lname )) {
        $query5 = "select * from " . $db_prefix . "customer_det where (fname = '" . $fname . "' and lname = '" . $lname . "')";
            $result5 = $mysqli->query ( $query5 ) or die ( mysql_error () );
    } else {
        $query5 = "select * from " . $db_prefix . "customer_det where phone = '" . $phone . "'";

        $result5 = $mysqli->query ( $query5 ) or die ( mysql_error () );

    }

然后我的下一条语句基本上说如果 $result5 === NULL 则插入内容。问题是我是 var_dump($result5) 并且看起来它正在打印一个数组。

object(mysqli_result)#4 (5) { ["current_field"]=> int(0) ["field_count"]=> int(34) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) }

基本上我想说的是如何编写 IF $result5 = 不存在的语句。我在这里寻找的是 count 或 num_rows 函数吗?

最佳答案

mysqli_query()返回 mysqli_result object .

您想要检查其属性之一是否为 null,例如:

if ($result5->num_rows == 0)

注意:我还鼓励您阅读 SQL Injection .

关于php - MySQL 使用不存在列中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13259491/

相关文章:

phpunit xml 文件 (phpunit.xml)

php - WordPress 插入 MySQL 转换

javascript - 如何在mysql Node js中创建表?

MySQL 5.6.26 (Windows 8.1 x64) : Trouble with setting a password for the root MySQL user

forms - 如何在表单中添加 curl `-H` 参数?

带有德语字母的 PHP 邮件主题

将数据返回到数据库 MYSQL 的 PHP 页面

javascript - 使用 jQuery 和 Ajax 发送输入数组

PHP - 通过各种用户操作的表单安全地发送数据

php - 将数据数组传回服务器的最佳方式(在发布时)?