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

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

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

    报告相同问题?

    悬赏问题

    • ¥15 smptlib使用465端口发送邮件失败
    • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
    • ¥15 对于squad数据集的基于bert模型的微调
    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存
    • ¥15 CST保存项目时失败
    • ¥15 树莓派5怎么用camera module 3啊
    • ¥20 java在应用程序里获取不到扬声器设备
    • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
    • ¥15 Attention is all you need 的代码运行