-2

I can't seem to correct my php error having looked at their answers and corrections. This is the error I get when the page is refreshed on the internet:

mysqli_query() expects parameter 1 to be mysqli, null given…

<?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>
Ash
  • 21
  • 1
  • 7
  • 2
    Wait. you should debug your `$db_server` variable by `var_dump()` or something similar. Basics of debugging. Also, this line: `$db_server = mysqli_query($db_server, $query1)` doesn't make sense. You seem to try to overwrite `$db_server` with query result. – Raptor Dec 24 '13 at 03:11
  • `$db_server = mysqli_query($db_server, $query1) or die`? That won't wash. – Funk Forty Niner Dec 24 '13 at 03:15
  • I dunno, try this instead `$result = mysqli_query($db_server, $query1); if(!result){ die("Insert failed: " . mysqli_error($db_server)); }` – Funk Forty Niner Dec 24 '13 at 03:23
  • Where are you initializing `$db_server` with `mysqli_connect()`? – Alejandro Iván Dec 24 '13 at 03:24
  • Thank you all for the quick responses. I have another PHP page that is linked to this page called db_connect.php (which contains the code that links the page to the database). I have edited the code with your suggestions and now the page is loading. I still have the same error Warning: mysqli_query() expects parameter 1 to be mysqli, null given in .../home.php on line 41. any suggestions? – Ash Dec 24 '13 at 04:02
  • It's most likely a DB connection error. Double check how and where it is defined. – Funk Forty Niner Dec 24 '13 at 04:06
  • Also make sure your connection is not using `mysql_*` instead of `mysqli_*` that I've seen happen before. You should show us your full code, including your DB connection, replacing actual credentials with `xxx,xxx,xxx` also showing how you're using the `getPosts()` function. – Funk Forty Niner Dec 24 '13 at 04:09
  • I have now put all of the database connection code into the page just to make sure that there wasn't a problem with the db_connect.php page and checked that it is not wrong. – Ash Dec 24 '13 at 04:15
  • $db_server = mysqli_connect($db_hostname, $db_username, $db_password); $db_status = "connected"; $db_select = mysqli_select_db($db_server, $db_database); is the last 3 lines of the code that connects it to the database. could the problem be because of this? – Ash Dec 24 '13 at 04:16
  • You may need to pass your connection inside your function `function getPosts(mysqli $db_server)` [**This question**](http://stackoverflow.com/q/18862743/1415724) resembles your code a lot, btw. – Funk Forty Niner Dec 24 '13 at 04:17
  • Plus I tend to think that your DB connection may be out of scope. – Funk Forty Niner Dec 24 '13 at 04:22
  • You're doing `if (!isset($_SESSION['logged']))` what happens if the sessions IS set? What you have now tells me that, if the session is NOT set, then proceed with the rest of the code, is that the desired result? – Funk Forty Niner Dec 24 '13 at 04:25
  • Try this `function getPosts() { global $db_server $query1 = "SELECT * FROM comments";` followed by the rest of your code in your `getPosts()` function. That's the only thing I can think of. Other than that, I'm baffled. – Funk Forty Niner Dec 24 '13 at 04:36
  • thanks a lot for your help. the last line I added made a syntax error so sadly didn't work I don't think. but thank you so much for helping me. It is probably be a really small mistake I have made so will keep trying.. – Ash Dec 24 '13 at 04:50

3 Answers3

0

Seems like you are overwriting your $db_server on the line that reads:

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

So you should change that to be another variable like $result1:

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

That said, are you actually connecting via $db_server anywhere else in your code? Look out for errors similar to what I just pointed out.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
0

your $db_server is null which should a valid mysqli instance. this indicates either database connection failed or you didn't initialize $db_server or you overwritten it and looks like last option is true.

 $db_server = mysqli_query($db_server, $query1)

this overwrites $db_server.

nur
  • 161
  • 2
  • 11
0

Insted of $db_server use $db_select. This may work as your $db_server variable does not have a connection to database. Its just a connection to server.

Mehul Vasa
  • 51
  • 9