PHP错误 - mysqli_query()期望参数1为mysqli,null给定

时间:2013-12-24 03:05:16

标签: php html database mysqli

我似乎无法纠正我的PHP错误,看了他们的答案和更正。这是我在互联网上刷新页面时出现的错误:

  

mysqli_query()期望参数1为mysqli,null给定...

<?php
session_start();
if (!isset($_SESSION['logged'])){
$_SESSION = array();
header('location: home_start.php'); //your login form
require_once("functions.php");
include_once("home_start.php");
$db_hostname = 'xxxx';
$db_database = 'xxx'; //'Your database name' 
$db_username = 'xxx'; //'your username'; 
$db_password = 'xxx'; //'Your password'; 
$db_status = 'not initialised';
$str_result = ' ';
$str_options = ' ';
$db_server = mysqli_connect($db_hostname, $db_username, $db_password);
$db_status = "connected";
$db_select = mysqli_select_db($db_server, $db_database);

}
//EXISTING DATABASE CONNECTION CODE
//if (!$db_server){
    //die("Unable to connect to MySQL: " . mysqli_connect_error($db_server)); }else{     $db_status = "not connected";
    //NEW SUBMISSION HANDLING CODE HERE
//if(trim($_POST['submit']) == "Submit"){
//}//EXISTING CODE (to create the options list) HERE...
 //} 

//require_once('recaptcha/recaptchalib.php');
//$privatekey = " 6Lem4-gSAAAAADsaa9KXlzSAhLs8Ztp83Lt-x1kn";  
//$resp = recaptcha_check_answer ($privatekey,
//$_SERVER["REMOTE_ADDR"], 
//$_POST["recaptcha_challenge_field"], 
//$_POST["recaptcha_response_field"]);
//$message = "";
//if (!$resp->is_valid) {
    //$message = "The reCAPTCHA wasn't entered correctly. Go back and try it again. (reCAPTCHA said: " . $resp->error . ")";
    //} else {
    // ADD YOUR CODE HERE to handle a successful ReCAPTCHA submission // e.g. Validate the data
    //$unsafe_name = $_POST['fullname'];
    //} 
    //$message .= "Thanks for your input $unsafe_name !";
    //echo $message;

     if (isset($comment) && $comment == '') {
        $bedrooms = $_POST['bedrooms'];
        $bedrooms = clean_string($db_server, $year); 
        $comment = clean_string($db_server, $_POST['comment']);
    }
                else {
                    $query1 = "INSERT INTO comments (comment) VALUES ('$comment')";
                    $result = mysqli_query($db_server, $query1); if(!result){ die("Insert failed: " . mysqli_error($db_server)); 

                    }
                        $message = "Thanks for your comment";
             }
             function getPosts(mysqli $db_select){
             $query1 = "SELECT * FROM comments";
             $result1 = mysqli_query($db_server, $query1);
             while($array = mysqli_fetch_array($result1)){
                    $comments = date('d/m/Y', strtotime($array['commDate'])) . "<p>" . $array['comment'] . "</p><br/>";

             }
             }
 ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="home.css" rel="stylesheet" type="text/css"/>
<title>Home</title>
</head>

<body>
<div id="middle">
 <h2><strong>HELLO!</strong></h2>
 <h2>Welcome to <strong>Cosy Cribs</strong> website!</h2>
 <p>This website combines all the possible lettings available to YOU from the most prefered letting companies in the great city of Leeds!</p>
 <p>It was recognised that when students attempt to let a house for the next year, there were far too many different websites and companies visit; making the whole ordeal of finding a house more stressful then needs be!</p>
 <p>We, at <strong>Cosy Cribs</strong>, decided that your lives needed to be made easier, and so we announce a website that provides you with all of the lettings from these different companies - all on one website - and links to the house you like.</p>
 <h2>ENJOY!</h2>
</div>
<form id="comments" action="home.php" method="post">
<select name="comments">
</select>
<h1>Do you have a comment on preferred company or number of bedrooms?</h1>
Comment: <textarea rows="2" cols="30" name="comment"></textarea>

<input type="submit" id="submit" name="submit" value="Submit" />
</form>

</body>
</html>

3 个答案:

答案 0 :(得分:0)

好像你正在覆盖你的$db_server上写着:

 $db_server = mysqli_query($db_server, $query1) or
   die("Insert failed: " . mysqli_error($db_server));

所以你应该将其改为另一个变量,如$result1

 $result1 = mysqli_query($db_server, $query1) or
   die("Insert failed: " . mysqli_error($db_server));

那就是说,您实际上是通过代码中的其他任何位置通过$db_server进行连接了吗?注意与我刚才指出的类似的错误。

答案 1 :(得分:0)

你的$ db_server是null,它应该是一个有效的mysqli实例。这表示数据库连接失败或者您没有初始化$ db_server或者您覆盖它并且看起来最后一个选项为true。

 $db_server = mysqli_query($db_server, $query1)

这会覆盖$ db_server。

答案 2 :(得分:0)

Ins $db_server使用$db_select。这可能有效,因为$db_server变量没有与数据库的连接。它只是与服务器的连接。