duandu6497 2013-06-29 16:15
浏览 35
已采纳

使用php检索项目列表

I am trying to retrieve a list of items from a mySQL db and insert them as a list in a select object on a webpage. The following is the bit of code that isnt working.

In the first line, I am trying to retrieve a JSON object from a public function called getBrands() in a singleton object I have created called DatabaseInterface.

The second line is then attempting to turn that JSON object into a php array.

Finally, I am running a loop which can option each item in between tags for the webpage.

Where am I going wrong?

<?php 

var $brandArrayJSON = DatabaseInterface::getBrands();
$brandArray = JSON_decode($brandArrayJSON);

for ($loop=0; $loop < sizeof($brandArray); $loop++) {
    echo "<option>$brandArray[$loop]</option>";
}

?>

EDIT: In case it helps, here is my DatabaseInterface singleton. I have included this file at the top of my php file

class databaseInterface {

private static $_instance;

// Private constructor prevents instantiation
private function __construct() {
}

public static function getInstance() {
    if (!self::$_instance) {
        self::$_instance = mysqli_connect(self::databaseHost, self::databaseUsername, self::databasePassword, self::databaseName);
        if (mysqli_connect_errno(self::$_instance)) {
            throw new Exception("Failed to connect to MySQL:" . mysqli_connect_error());
        }
    }
    return self::$_instance;
}

public function getBrands() {

    try {
        $con = DatabaseInterface::getInstance();
    } catch (Exception $e) {
        // Handle exception
        echo $e->getMessage();
    }

    $query = "SELECT psBrandName from brands";
    $result = mysqli_query($con, $query) or die ("Couldn't execute query. ".mysqli_error($con));

    $resultArray[] = array();

    while ($row = mysqli_fetch_assoc($result)) {

        extract($row);
        $resultArray[] = $psBrandName;

    }

    return json_Encode($resultArray);

}
  • 写回答

1条回答 默认 最新

  • duanhongyi2964 2013-06-29 17:34
    关注

    There is nothing "wrong" with the code, in that it should work (provided nothing is broken on the query-side). However, there are several things that should be improved.

    First, basically what the getBrands() method is doing is equivalent to this:

    $brandArray = json_encode(array('test','test2','test3'));
    echo $brandArray; // returns ["test","test2","test3"]
    

    Now, when you decode that you get the same thing you originally put in (an array):

    $brandArray = json_decode('["test","test2","test3"]');
    var_dump($brandArray); // Will dump an array
    

    Since this is an array (not a PHP object), you can just use a foreach.

    foreach($brandArray as $option) {
        echo '<option>', $option, '</option>';
    }
    

    If you're worried about it being an object in some instances (maybe you had a non-array JS object which would be mostly the equivalent to a PHP associative array), you could cast the json_decode result into an array.

    $brandArray = (array)$brandArray;
    

    Now, in your getBrands() method, I would highly recommend just using $row['psBrandName'] instead of cluttering things up with extract, unless you have a really good reason to do this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图