Warning: mysqli_close() expects parameter 1 to be mysqli [duplicate] - php

This question already has answers here:
php warning: mysqli_close() expects parameter 1 to be mysqli
(2 answers)
Closed 1 year ago.
I have this PHP code on a WAMP server with MySQL and this error appears in a dialog box when I visit the site. How can I fix this?
<?php
// Create connection
$con = mysqli_connect("localhost","userdb","userdb","apptestdb");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM Locations";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($result);
mysqli_close($con);
?>

You don't need this line:
mysqli_close($result);
You only need to close the connection.

Related

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /... on line 21 [duplicate]

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 4 years ago.
I've spent the past 2 hours trying to solve this one error. I am a complete rookie so I dont know what's going on. Here's the code, please help:
<?php
header('Access-Control-Allow-Origin: *');
$host="localhost"; // Host name
$username="id11111_ab"; // Mysql username
$password="*****"; // Mysql password
$db_name="id11111_cd"; // Database name
$tbl_name="ef"; // Table name
// Connect to server and select database.
$link = mysqli_connect($host, $username, $password, $db_name);
// Retrieve data from database
$sql = "SELECT * FROM scores ORDER BY score DESC LIMIT 10";
$result = mysqli_query($link,$sql);
// Start looping rows in mysql database.
while($rows=mysqli_fetch_array($result)){
echo $rows['name'] . "|" . $rows['score'] . "|";
// close while loop
}
?>
mysqli_query() returns false if it fails. Subsequently, the mysqli_fetch_array() function is being passed this false boolean value, on which it can't operate. You'd be wise to check the value that mysqli_query() returns isn't false prior to attempting to retrieve the resource. E.g.:
$result = mysqli_query($link,$sql);
if (!$result) {
die('Query failed');
}
It seems like the mysql connection is not established properly.
Check for errors using this:
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}

Directly echo the resulting data show an error msg [duplicate]

This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
When I am echo the $result variable. It display an error msg like this:
Catchable fatal error: Object of class mysqli_result could not be converted to string in D:\Inspiration\server\Table2_yahooData_db.php
on line 13
<?php
// connect to the dataBase
$conn = mysqli_connect('localhost', 'root', '', 'yahooData');
//checking Connection.
if (!$conn) {
die('Error connection failed: ' . mysqli_connect_error($conn));
}
// Creating select query Using sql commands.
$sql = "SELECT * FROM User;" ;
$result = mysqli_query($conn, $sql);
echo $result;
mysqli_close($conn);
?>
$result is an object.
You cannot print it with string's echo function.
You can rather print it with print_r($result);
echo is for scalar variables (which have single value or are single dimensions) like number, string. For multi-dimensional variables e.g. array, objects we need to use print_r() which prints the whole tree.
When using a SELECT query the mysqli_query() function returns a resource, not a string. You need to use mysqli_fetch_assoc() to put the results into an array. Here is an object-oriented example:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT name FROM User";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
echo $row['name'];
}
/* free result set */
$result->free();
}

PHP file on website - Getting fatal error in mysql_connect()

I'm trying to host this php file on my website to connect to a MySQL database.
<?php
// Create connection
$con=mysqli_connect("localhost","username","pass","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM Locations";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($con);
?>
And when I try to validate the code using http://writecodeonline.com/php/ it says
Fatal error: Call to undefined function mysqli_connect() on line 2
Searching around said I needed to change the php.ini file but I don't even have one hosted on my site. Is there something wrong with the code?
When I try to access it at www.mydomain.com/service.php it says file not found error... but it's definitely there. I'm working with this tutorial - http://codewithchris.com/iphone-app-connect-to-mysql-database/
You need to install the php-mysql dependency on your server.
yum install php-mysql -y
or your equivalent on your os.

Warning: mysqli_close() expects parameter 1 to be mysqli, object given in [duplicate]

This question already has answers here:
php warning: mysqli_close() expects parameter 1 to be mysqli
(2 answers)
Closed 2 years ago.
I want to apologize in advance if this has been answered. But I can not seem to find a solution that works for my problem.
I have been staring at my code for about 2 hours now and I can not seem to figure out why I am getting this error. Below is my PHP code. I am using a MYSQL DB that is hosted on a GoDaddy server.
// Create connection
$con=mysqli_connect("","","","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM location";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($result);
mysqli_close($con);
I am getting my error at mysqli_close($result);
you can view via http://www.iqrleads.co/service.php any help will be greatly appreciated my mind is drawing a blank
$result is not a mysqli connection object in your code.
You can remove mysqli_close($result); from your code, as it's not needed.
mysqli_close($con); should suffice for you.
You can also use $con->close(); if you're so inclined.
See the reference for mysqli_close
Change
mysqli_close($result);
mysqli_close($con);
to
$con->close();

update mysql_num_rows to new Mysqli standard: mysqli_stmt_num_rows

I have a PHP page with some Mysqli that I am attempting to convert from MySql. I think I've converted most of it correctly, but I am getting the following error message when I try to execute the code (below):
Connection was OK!
Warning: mysqli_stmt_num_rows() expects parameter 1 to be mysqli_stmt, object given in /quantityremaining5.php on line 25
9999
I'm a bit new to this, so please be gentle - what is that I'm doing wrong? thanks!
<?php
include 'quantitytest_config.php';
// Create connection to MySQL
$link = mysqli_connect($hostname, $username, $password);
// Check connection
if (mysqli_connect_errno($link))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else { echo "Connection was OK!\n";}
//select a database to work with
$selected = mysqli_select_db($link, "grace59_countdown");
// or die("Could not select countdown");
// use if TWO SEPARATE columns for date and time
//execute the SQL query and return records
$result = mysqli_query($link,
"SELECT items
FROM cases
WHERE datetime<=NOW()
Limit 1 ");
if(mysqli_stmt_num_rows($result) == 0){
echo "9999";
} else {
//fetch tha data from the database
while ($row = mysqli_fetch_array($result)) {
echo "Quantity:".$row{'items'}."<br>";
}
}
//close the connection
mysqli_close($link);
?>
Use mysqli_num_rows($result) or $result->num_rows. As the name indicates, mysqli_stmt_num_rows() is intended to be used with a mysqli_stmt object (as returned by mysqli_prepare()).
See the documentation.

Categories