dongyan2445 2013-05-03 20:25
浏览 23

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

I'm starting to learn how to use prepared queries, so I'm changing a portal to use these, for some reason I cannot make it work I ran a simpler version of it and it worked, then I turned that into a function and it stop working, telling me no database selected ... here' s the code:

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条回答 默认 最新

    报告相同问题?