PHP:Mysqli准备了带有"select *"的语句 [英] PHP: Mysqli prepared statement with "select *"

查看:98
本文介绍了PHP:Mysqli准备了带有"select *"的语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前从数据库中获取的方式:

This is how I presently fetch from the DB:

if ($stmt = $mysqli->prepare ( "SELECT fname,lname from $table_name 
where cno >=? LIMIT 50" ))
    {
        $stmt->bind_param ( "i", $cno); 
        $stmt->execute ();
        $stmt->bind_result ($fname,$lname); 
        $arrayUsers = array();

        while ($stmt->fetch())
        {
            if (isset ($fname))
            {
                $arrayUsers[] = array(
                        "fname" => $fname,
                        "lname" => $lname);

}
}
$stmt->close ();
        }
        $mysqli->close ();

,效果很好. 但是,如果我将选择更改为SELECT * from ...,则绑定失败. 这是否意味着如果我有大量字段,我仍然必须指定每个字段,或者是否可以使用select *?

and it works great. But if I change my select to SELECT * from... my bindings fail. Does that mean if I have a large number of fields, I will still have to specify each and every field or is there a way to use select *?

----已更新---

---- updated ---

if (empty($arrayUsers))
    {
        return 0;
    }
    else
    {
        return $array;
    }

推荐答案

如果需要对所有列进行选择:

If you need to perform a selection of all of the columns:

SELECT * FROM `table`

您将使用PHP的 get_result() 而不是 bind_result() .

You would use PHP's get_result() rather than bind_result().

bind_result()更好,get_result()将允许您使用表中更通用的数据返回.

bind_result() is better when you're specifying each column that you're retrieving where get_result() will allow you to work with a more generic return of data from your tables.

这篇关于PHP:Mysqli准备了带有"select *"的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆