【问题标题】:PHP error - mysqli_query() expects parameter 1 to be mysqli, null givenPHP 错误 - mysqli_query() 期望参数 1 为 mysqli,给定 null
【发布时间】:2013-12-24 03:05:15
【问题描述】:

查看了他们的答案和更正后,我似乎无法纠正我的 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>

【问题讨论】:

  • 等等。你应该通过var_dump() 或类似的东西来调试你的$db_server 变量。调试基础。此外,这一行:$db_server = mysqli_query($db_server, $query1) 没有意义。您似乎试图用查询结果覆盖$db_server
  • $db_server = mysqli_query($db_server, $query1) or die?那不会洗。
  • 我不知道,试试这个$result = mysqli_query($db_server, $query1); if(!result){ die("Insert failed: " . mysqli_error($db_server)); }
  • 你在哪里用mysqli_connect()初始化$db_server
  • 感谢大家的快速回复。我有另一个链接到此页面的 PHP 页面,名为 db_connect.php(其中包含将页面链接到数据库的代码)。我已经根据您的建议编辑了代码,现在页面正在加载。我仍然有同样的错误警告:mysqli_query() 期望参数 1 为 mysqli,第 41 行的 .../home.php 中给出的 null。有什么建议吗?

标签: php html database mysqli


【解决方案1】:

似乎您正在覆盖您的$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 连接代码中的其他任何地方吗?留意与我刚才指出的错误类似的错误。

【讨论】:

    【解决方案2】:

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

     $db_server = mysqli_query($db_server, $query1)
    

    这会覆盖 $db_server。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-26
        • 1970-01-01
        • 2014-02-08
        • 1970-01-01
        • 1970-01-01
        • 2016-02-29
        • 1970-01-01
        相关资源
        最近更新 更多