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 6 years ago.
this is my php codes getting Post Successfully response when true but when it failed for some reason.
i got this error.
mysqli_error() expects parameter 1 to be mysqli, null given
$result = mysqli_query($connection, $query) ;
if($result){$response["msg"] = "Post Successfully";
echo json_encode($response);} else {$response["msg"] = "failed ".mysqli_error($con);
echo json_encode($response);
}
mysqli_close($connection);
}
$con is null
mysqli_error($con); should be mysqli_error($connection); as you have declared connection as $connection.
According to this line:
mysqli_query($connection, $query)
You are using variable $connection for link identifier, and in mysqli_error($con) you are using $con variable which is undefined or NULL, this should be:
mysqli_error($connection)
Related
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();
}
This question already has an answer here:
mysqli_real_escape_string() expects exactly 2 parameters, 1 given Fatal Error [duplicate]
(1 answer)
Closed 6 years ago.
Here is my code:
include "db_conx.php";
$sql = mysqli_query("INSERT INTO table(column) VALUES('" . mysqli_real_escape_string($var) . "')");
if ($sql) {echo "connection successful";
} else {
echo "failure";
}
It returns these errors:
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given
Warning: mysqli_query() expects at least 2 parameters, 1 given
I tried using PDO but that didn't work either...
First Parameter is mysql connection link identifier, and second is string For more details, you can visit this link : http://in2.php.net/manual/en/mysqli.real-escape-string.php.
include "db_conx.php";
$sql = mysqli_query(pass_your_connection_identifier_here ,"INSERT INTO table(column) VALUES('" . mysqli_real_escape_string(pass_your_connection_identifier_here, $var) . "')");
if ($sql) {echo "connection successful";
} else {
echo "failure";
}
You are missing connection identifier in both mysqli_real_escape_string() and mysqli_query()
change your code to mysqli_query($connection,$sql) and mysqli_real_escape_string($connection,$string)
You are missing connection variable at both the lines that's why you are facing issues :
Try to replace your code with this :
//$con // it is your connection variable
include "db_conx.php";
$sql = mysqli_query($con,"INSERT INTO table(column) VALUES('" . mysqli_real_escape_string($con,$var) . "')");
if ($sql) {echo "connection successful";
} else {
echo "failure";
}
This question already has answers here:
Fatal error: Call to undefined method mysqli_stmt::get_result() [duplicate]
(2 answers)
Closed 7 years ago.
I have mysqli script, Unfortunately it doesn't run on my Server with PHP 5.3.29.
As far as I can debug the server doesn't return any value for the function get_result() and the script breaks after it. Is there any know issue?
$query = "SELECT * FROM test WHERE column = ? LIMIT 1";
$stmt = $mysqli->stmt_init();
if(!$stmt->prepare($query)) echo "Failed to prepare statement\n";
else echo "prepare statement okay\n";
if(!$stmt->bind_param('s', 'test')) echo "Failed to bind parameter\n";
else echo "bind parameter okay\n";
if(!$stmt->execute()) echo "Failed to execute\n";
else echo "execute okay\n";
$result = $stmt->get_result();
if(!$result) echo "Failed to get result \n";
else echo "result okay\n";
The output would be just:
prepare statement okay
bind parameter okay
execute okay
Any ideas?
You are getting the following error:
PHP Fatal error: Call to undefined method mysqli_stmt::get_result()
The error message does not leave any room for doubt: You're calling a method that does not exists.
Looking up the method in question in the PHP manual shows this detail:
Available only with mysqlnd.
So it's not only the PHP version you need to fulfil but also a specific mysql driver.
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 8 years ago.
I have a connection to my database:
$con=mysqli_connect("localhost","xxxx","xxxxx","xxxxxx");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
which returns no error.
I then try to perform a mysqli_query:
$result = mysqli_query($con,"INSERT INTO entries (name, genre, info , date , website , twebsite, video , tvideo, image, extra_genre)
VALUES ('".$name."', '".$type."', '".$info."', '".$date."', '".$url."', '".$href."', '".$_POST["video"]."', '".$videotype."', 'video images/".$photo."', '".$type2."')");
with the alert whether this insertion to the database has worked or not:
if($result)
{
echo "Success:";
}
else
{
echo "error inserting data into database: ";
}
When I perform my code I get the output error inserting data into database:
But I have no ides why $result is not successful?
How can I debug it?
You are looking for mysqli_error() function.
Problem here is that date is a reserved word, so you have to escape that: `date`
try this:
echo $result;
and then run the sql output.. that should give you an error message.
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 3 years ago.
I have written a mysql_query and it is not working.
I want to know the problem via mysqli_error() however it gives the following error mysqli_error() expects parameter 1 to be mysqli
my mysql code is as follows:
$connect = mysqli_connect("localhost","root", "", "tomuman");
$query = mysqli_query($connect, "SELECT id, to FROM messages WHERE read='0'");
and mysqli_error as follows:
echo mysqli_error($query);
What could cause this problem?
You should not use $query as a parameter for mysqli_error()
use
echo mysqli_error($connect);
Just after trying to connect you can also check for specific connection errors with:
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
exit();
}
Add following code in your query to get the mysql error message.
or die (mysqli_error($connect))
as like below
$query = mysqli_query($connect, "SELECT id, to FROM messages WHERE read='0'") or die (mysqli_error($connect));
if ($query) {
echo "success";
}
else {
echo("Error description: " . mysqli_error($connect));
}