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));
}
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:
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)
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 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 answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(2 answers)
Closed 6 years ago.
I am at prototype stage so I have error_reporting(-1); at my 1st row. Despite this, I have no php error but php prints 'could not get data'.
As I understood from php.net manual and stackoverflow similar cases, my $sorgula returns FALSE. But why? Can you help, regards
//i am sure that i am connected to db
if ($sorgula = mysqli_query($dbc, "SELECT * FROM tb_yazilar ORDER BY kolon_sn"))
{
while ($satir = mysqli_fetch_array($sorgula, MYSQLI_ASSOC))
{
echo $satir['kolon_yazar'].' - '.$satir['kolon_baslik'].' - '.$satir['kolon_yazi'].' - '.$satir['kolon_etiketler'].' - '.$satir['kolon_ytarihi'].' - - - - ';
}
}
else
{
echo 'could not get data';
}
mysqli_close($dbc);
try to use mysqli_error in your code .
procedural example:
$sorgula = mysqli_query($dbc, "SELECT * FROM tb_yazilar ORDER BY kolon_sn")
or error_log(mysqli_error($dbc));
I used this and it worked: without the if, once it extracts, go back and add the if. :)
require 'db.php';
$query = "SELECT * FROM thoughts";
$result = mysqli_query($conn, $query);
while($row=mysqli_fetch_assoc($result)) {
echo "<td>" . "TEXT: ". $row['text'] . "</td>";
}
mysqli_close($conn);