php:没有从我的(预准备语句)查询得到结果

时间:2014-05-04 13:40:26

标签: php prepared-statement

有人可以指出错误的位置吗? 我运行下面的代码,而不是填充数组,它给我一个空的结果。

我检查了数据库中的SQL,运行正常。

function fetchStatusDetails($cno, $randomPass)
{
    $mysqli = $this->_setDB (); // This sets up the DB connection.

        if ($stmt = $mysqli->prepare ( "SELECT created_main_menus,created_submenus,picked_a_header_image,uploaded_a_header_image,has_logo FROM " . $this->tableName . " WHERE random_password=? and CNO=? LIMIT 1" ))
        {


            /* bind parameters for markers */
            $stmt->bind_param ( "si", $randomPass,$cno );

            $stmt->execute ();

            $stmt->bind_result ( $main_menus,$sub_menus,$picked_header_img,$upped_header_img,$logo );

            $stmt->fetch ();

            if (! empty ( $main_menus ))
            {
                $array = array();

                $array['main_menus']            = $main_menus;
                $array['sub_menus']             = $sub_menus;
                $array['picked_header_img']     = $picked_header_img;
                $array['upped_header_img']      = $upped_header_img;
                $array['logo']                  = $logo;
                echo "*-*-".$array['upped_header_img']."*-*-";

                return $array;
            }
            else
            {echo "DATABASE ERROR 0090";
                return false;
            }

            /* close statement */
            $stmt->close ();
        }

2 个答案:

答案 0 :(得分:1)

您可以通过以下方式获取关联数组,在建议您关闭return$stmt之前,我建议不要使用$mysqli

function fetchStatusDetails($cno, $randomPass)
{
    $mysqli = $this->_setDB (); // This sets up the DB connection.
    $array = false;

    if ($stmt = $mysqli->prepare ( "SELECT created_main_menus AS main_menus,created_submenus AS sub_menus,picked_a_header_image AS picked_header_img,uploaded_a_header_image AS upped_header_img,has_logo AS logo FROM " . $this->tableName . " WHERE random_password=? and CNO=? LIMIT 1" ))
    {
        $stmt->bind_param("si", $randomPass, $cno);
        $stmt->execute();
        $result = $stmt->get_result();
        $array = $result->fetch_assoc();
    }

    if (empty($array))
    {
        echo "DATABASE ERROR 0090";
    }

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

    return $array;

}

答案 1 :(得分:0)

尝试if(isset($main_menus))而不是if (! empty ( $main_menus ))

一切顺利