1

I have a a form that pulls data from a database(mysql to be specific) and echos the data into the value section of <input> tags. It doesn't seem to be working I have coded a view section of my website to do the same thing but from a different table in my database. I use the same code to make making changes easy and if another developer works on my site in the future. Anyway it doesn't seem to be working I'm not sure why though.

The full error I get:

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/caseol5/public_html/jj/admin/news_update.php on line 9 

Here is line 9 that the error is referring to:

$result = mysqli_query($link,$sql); 

I know that both of those function are not null as I did:

echo $link echo $sql 

before that line after I started feting the error and they both are not null.

Here is the full code segment:

$nid = $_GET['nid']; include ("../sql/dbConnect.php"); $sql = "SELECT * FROM jj_news WHERE news_id = $nid"; echo "<p>The SQL Command: $sql </p>"; echo "<p>Link: $link </p>"; $result = mysqli_query($link,$sql); if (!$result) { echo "<h1>You have encountered a problem with the update.</h1>"; die( "<h2>" . mysqli_error($link) . "</h2>") ; } $row = mysqli_fetch_array($result); $ntitle = $row['news_title']; $ntline = $row['news_titleline']; $ndesc = $row['news_desc']; $nother = $row['news_other']; 

I have looked into mysqli_query and I can't find anything I'm missing. I have also tired breaking the code down (and running parts of it and it gives the same error. My guess is it something small that I missed. I've looked at other question on this site that do that are a little similar but none seem to help. I've been looking at this for a while now and need another pair of eyes.

Update

As requested the contents of my dbconnect.php file:

$hostname = "localhost"; $username = "caseol5_jjoes"; $database = "caseol5_jj_site"; $password = "password1"; $link = mysqli_connect($hostname, $username, $password, $database); $link = mysqli_connect($hostname,$username,$password,$database) or die("Error " . mysqli_error($link)); if (!$link) { echo "We have a problem!"; } 
5
  • Can you show your database connection code? Becaouse the error itself says that your database connection object $link is null means it is not created correctly. Commented Mar 29, 2015 at 18:46
  • do not put brackets around your include, it's not needed and is inefficient Commented Mar 29, 2015 at 18:48
  • What's the output of your echo's? Commented Mar 29, 2015 at 18:50
  • I put the contents of my db connect file in the question Commented Mar 29, 2015 at 18:58
  • 1
    Is dbconnect.php actually the file one directory below your executing file, in the sql directory? Can you post the output of var_dump($link) right below the include statement? Can you find (ctrl + f) the text "We have a problem!" anywhere in the output (view source, ctrl + u)? Commented Mar 29, 2015 at 19:02

1 Answer 1

1

As clearly stated in the error message, mysqli_querydocs expects the first parameter to be a mysqli resource. In your case, this parameter is called $link but it holds a null value. A proper mysqli resource is normally obtained from connecting with the database by making use of mysqli_connectdocs

I expect the ../sql/dbConnect.php file holds the logic to connect with the database. Verify whether the $link variable is indeed initialized there. If it's not there, try to find an occurrence of mysqli_connect - maybe the resource is set to a different variable.

Without knowing what exactly is in ../sql/dbConnect.php, your problem right now is that you do not have a valid mysqli resource to use for mysqli_query.

Sign up to request clarification or add additional context in comments.

3 Comments

As per his edit, $link appears to be not null. echo "We have a problem!"; would have been executed otherwise.
@D4V1D According to the documentation, mysqli_connect returns an mysqli object anyway. Connection status is determined with mysqli_connect_error(), not by checking whether the return value is null. I have not verified this behavior.
I'm not sure what happened but it seems to working now I'm really not sure what happened....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.