阅读sql脚本

时间:2017-01-12 20:55:34

标签: php mysql

我的php脚本存在问题。脚本从mysqli数据库读取数据,然后将其保存到数组中供以后使用。该脚本确实运行良好,直到我想插入一些更多的sql语句来保存我的mysli表,我从中获取数据,最新。

我使用前三行连接到数据库并运行sql脚本。没有它们一切都很好。

<?php

$con=mysqli_connect('localhost', 'root', '','mks-solution-dev');
$sqlSource = file_get_contents('Einrichtung.sql');
mysqli_multi_query($con,$sqlSource);

$categories = Category::getTopCategories();
$categories = array_values($categories);
echo json_encode($categories);

    // Here, the entries in $data are indexed by the values they also have in                  'name'
// If you want them numerically indexed, all you have to do is:
//$data = array_values($data);
//echo json_encode(array('data' => $data));
//echo json_encode($data);      

class Category
{
    /**
     * The information stored in the database for each category
     */
    public $id;
    public $parent;
    public $name;
    public $vlink;      

    // The child categories
    public $children;

    public function __construct()
    {
        // Get the child categories when we get this category
        $this->getChildCategories();
    }

    /**
     * Get the child categories
     * @return array
     */
    public function getChildCategories()
    {
        if ($this->children) {
            return $this->children;
        }
        return $this->children = self::getCategories("parent = {$this->id}");
    }
    ////////////////////////////////////////////////////////////////////////////

    /**
     * The top-level categories (i.e. no parent)
     * @return array
     */
    public static function getTopCategories()
    {
        return self::getCategories('parent = 0');
    }

    /**
     * Get categories from the database.
     * @param string $where Conditions for the returned rows to meet
     * @return array
     */
    public static function getCategories($where = '')
    {
        if ($where) $where = " WHERE $where";
        $conn=mysqli_connect('localhost', 'root', '','mks-solution-dev');
        mysqli_set_charset($conn,"UTF8"); 


        $sql = "SELECT * FROM mdl_praktikum$where";         
        print $sql;         

        $result =mysqli_query($conn,$sql);
        print $result;

        $categories = array();
        while ($category = mysqli_fetch_object($result, 'Category'))
           {
        $vlink = array('vlink' => $category->vlink);
        unset($category->vlink);
        $category->data = $vlink;
        $categories[] = $category;
       }

        mysqli_free_result($sqlSource);
        mysqli_free_result($result);
        return $categories;
    }
}
?>

在这里,您可以看到使用集成的sql脚本的结果:

SELECT * FROM mdl_praktikum WHERE parent = 0[]

其中一些没有:

SELECT * FROM mdl_praktikum WHERE parent = 0 

SELECT * FROM mdl_praktikum WHERE parent = 1 

SELECT * FROM mdl_praktikum WHERE parent = 2 

SELECT * FROM mdl_praktikum WHERE parent = 3 

SELECT * FROM mdl_praktikum WHERE parent = 6 
.
.
.
SELECT * FROM mdl_praktikum WHERE parent = 386 

SELECT * FROM mdl_praktikum WHERE parent = 387 

SELECT * FROM mdl_praktikum WHERE parent = 388 

[{"id":"1","parent":"0","name":"root","children":[{"id":"2","parent":"1","name":"Verschiedenes","children":[],"ref":"1","layer":"1","data":{"vlink":null}},{"id":"3","parent":"1","name":"EBSN","children":[{"id":"6","parent":"3","name":"EBSN 1415","children":[{"id":"23","parent":"6","name":"General .......

我觉得在我的方法getCategories中的某些地方完全错了,但我不明白为什么

0 个答案:

没有答案