homepage Welcome to WebmasterWorld Guest from 54.204.73.126
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Warning: mysqli query() expects parameter 1 to be mysqli, nul
ricksum



 
Msg#: 4602767 posted 11:59 am on Aug 17, 2013 (gmt 0)

I received the following message when executing the included code:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\diff\selecttesttable.php on line 28

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\diff\selecttesttable.php on line 30

and have spent several hours trying to find the problem with no success

The code is:
<?php
// connect to the database
// include('connect-db_belmont.php');
// server info
$server = 'localhost';
$user = 'root';
$pass = '';
$db = 'belmont';

// connect to the database using object oriented style
$mysqli = new mysqli($server, $user, $pass, $db);

// show errors (remove this line if on a live site)
// mysqli_report(MYSQLI_REPORT_ERROR);

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT owner_name FROM assessor");

while($row = mysqli_fetch_array($result))
{
// echo $row['FirstName'] . " " . $row['LastName'];
// echo "<br>";

$owner=$row['owner_name'];

// echo "This is the variable ".$owner."<br />";
// echo $row['owner_name'];
preg_match('/^([^ ]+) (.*?)(?: & (.*))?$/', $row['owner_name'], $matches);
// echo " - Last name: {$matches[1]} - Husband: {$matches[2]}<br />";
$last = $matches[1];
$first = $matches[2];
$_SESSION['last'] = $matches[1];
echo "session variable last equals ".$matches[1];
echo " last variable is ".$last."<br />";

$first = $matches[2];
$_SESSION['first'] = $matches[2];
echo "session variable first equals ".$matches[2];
echo " first variable is ".$first."<br />";

if(isset($matches[3])) {
$mate = $matches[3];
echo " - Mate: {$matches[3]}";
}
echo "<br />";

}

// mysqli_close($con);
?>

Line 28 is: $result = mysqli_query($con,"SELECT owner_name FROM assessor"); I have also tried changing from owner_name to * with same results
Line 30 is: while($row = mysqli_fetch_array($result)) {

Please help

 

Readie

WebmasterWorld Senior Member



 
Msg#: 4602767 posted 12:24 pm on Aug 17, 2013 (gmt 0)

It's because you're mixing the mysqli object and the mysqli procedural functions together. You've also failed to asign anything to the variable $con that you are trying to use in your mysqli_query function.

If you replaced this line:
$mysqli = new mysqli($server, $user, $pass, $db);

With this:
$con = mysqli_connect($server, $user, $pass, $db);

Then it looks like it should work.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved