脚本中没有数据库选择错误

时间:2013-05-03 20:25:35

标签: php sql database mysqli xampp

我开始学习如何使用准备好的查询,因此我正在更改门户网站以使用这些,由于某些原因我无法使其工作我运行了一个更简单的版本并且它工作,然后我把它变成了一个函数,它停止工作,告诉我没有选择数据库......这是代码:

的config.php

//connection to the database
$mysqli = new mysqli($host, $user, $password, $database);
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
        . $mysqli->connect_error);
}

的index.php:

<?php 
        include ("system/mainFunctions.php"); 
        $page = 'home';
        $sub = NULL;

        if (isset($_GET["page"]))
            $page = $_GET["page"];
        if (isset($_GET["sub"]))
            $sub = $_GET["sub"];
?>

MainFunctions.php

function getContent($page , $sub, &$connection){    
if($page == 'home' || ($page == 'home' && $sub ==NULL))
    $qry = "SELECT * FROM public WHERE section = ?";
else
    $qry = "SELECT * FROM public WHERE section = ? AND subsection = ?";
$stmt = $connection->stmt_init();
if ($stmt->prepare($qry)) {
    if($page == 'home' || ($page == 'home' && $sub ==NULL))
        $stmt->bind_param("s" , $page);
    else
        $stmt->bind_param("ss" , $page, $sub);
    $stmt->execute();
    $metaResults = $stmt->result_metadata();
    $fields = $metaResults->fetch_fields();
    $statementParams='';
     //build the bind_results statement dynamically so I can get the results in an array
     foreach($fields as $field){
         if(empty($statementParams)){
             $statementParams.="\$column['".$field->name."']";
         }else{
             $statementParams.=", \$column['".$field->name."']";
         }
    }
    $statment="\$stmt->bind_result($statementParams);";
    eval($statment);
    while($stmt->fetch()){
        if($page == 'home' || ($page == 'home' && $sub ==NULL))
            formatNews($column);
        else
            formatStatic($column);
    }
    $stmt->close();
}
$connection->close();
}

0 个答案:

没有答案