mysqli_error() expects parameter 1 to be mysqli, n

2019-01-20 16:53发布

问题:

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!";

}

回答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.



标签: php mysql mysqli