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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么