Couldn't Fetch Mysqli - php

I have a problem fixing this code it says that
Warning: mysqli_query(): Couldn't fetch mysqli in
D:\Websites\movie-site\watch.php on line 56.php Warning:
mysqli_fetch_array() expects parameter 1 to be mysqli_result, null
given in D:\Websites\movie-site\watch.php on line 59

I got this issue when i close the mysqli connection and after closing the mysqli i am performing some more queries that time i got this error. so please check that your connection to your mysqli server is working.

From the sounds of the error the query you provided in mysqli_query() may have a syntax error in it which would explain why mysqli_query() wont work.
mysqli_fetch_array() will not work unless you have a returned result from mysqli_query(), good programming practice is before using mysqli_fetch_array() to check what has been returned by mysqli_query(), since it's saying that it's getting NULL in the argument an example check would be
if(empty(mysqli_query($conn,$query)))
{
echo "can not execute query"; // can use anything as a friendly error
}

From the errors you are getting, I can say that you're not passing any parameters to the mysqli_fetch_array() function, while it expects one argument.
You should run the mysqli_query() function with your SQL query as an argument and the returned result is stored to a result variable.
For example:
$result = mysqli_query($connection,"SELECT * FROM Peson");
Then you should use the $result variable with msqli_fetch_query() to fetch the result to a row array.
For example:
while($row = mysqli_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br>";
}

Related

How to return results from a database in a PHP foreach loop

So I have this code:
foreach ($_SESSION['basket'] as $item) {
$ref = $_SESSION['basket'][$counter];
$result = pg_query ($conn, 'SELECT * from music WHERE ref='.$ref.' ORDER BY artist');
}
This will output the first row fine, however it outputs this Warning: pg_fetch_row() expects parameter 1 to be resource, boolean given if I try to retrieve more than one row. I don't understand how I'm giving a boolean to parameter 1, this is the code on line 46 where it is getting the error:
($row = pg_fetch_row($result))
Thanks in advance
You can use $row =pg_fetch_array($result) and then $row['field_name'] to take values out in the foreach loop.
The error may be because your connection variable $conn does not connect to your database.
Try all possibilities. Thank you.
If your query fails, pg_query returns FALSE. Also, you should use pg_query_params instead to prevent SQL injection.
Have a look at the examples in the PHP doc: http://php.net/manual/en/function.pg-query.php.
REMEMBER TO:
A: Escape input, or even better use prepared statements
B: Check the return value before iterating over the results. If an error occured it doesn't make sense to try and iterate.
In general the PHP docs are a great place, when strugling with the details of the PHP-api's, which are sometimes... Less intuitive than they could be. :-)

php and mysql error for registration form

when i press register on my php form im getting this error:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean
given in /home/changj/public_html/register.php on line 26 No database
selected
Below is the register.php script line 26 but unsure how to fix the error
if(mysql_num_rows($sql)> 0 ) {
any ideas thanks.
I guess $sql is a string. Try this,
$result = mysql_query($sql);
if(mysql_num_rows($result)> 0 ) { .... }
but mysql_query() will be deprecated as of PHP 5.5.0. An alternative is to use, mysqli_query() or PDO::query()
You don't have a database selected: "No database selected"
Make sure that you have a mysql_connect() and a mysql_select_db() anywhere before your statement.
As answered by me here
From mysql_query() documentation:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
The query is wrong. Change it.
Did you set the database?
mysql_select_db("databaseName");
Or, if you're using "raw queries", you may be missing this line:
mysql_query("USE databaseName");

mysql_free_result() expects parameter 1 to be resource

im sending data from my android application to mySQL in localhost and I receive warning on (Warning: mysql_free_result() expects parameter 1 to be resource, Boolean given in C:\xampp\htdocs\datatest.php on line 17)
despite the warning i'm still able insert the data into the database.
i'm wondering is it ok to ignore this or how can i solve this problem?
i tried various forums and website by none solve my problems.
<?php
$dbcnx = mysql_connect("localhost", "root", "");
$db = "agentdatabase";
mysql_select_db($db, $dbcnx);
$user_id=$_POST['username'];
$passwd=$_POST['password'];
$query = "INSERT INTO agentable (username,password) VALUES ('".$user_id."','".$passwd."')";
echo $query;
$result = mysql_query($query) or die ("<b>Query failed:</b> " . mysql_error());
if($result){
echo '<br />','pass';
}
else echo mysql_error();
mysql_free_result($result);
mysql_close($dbcnx);
?>
You can't free the result of an INSERT query, since you can't free a boolean.
Side note, the MySQL PHP extension is deprecated. It's better to use MySQLi or PDO.
mysql_free_result() only needs to be called if you are concerned about how much memory is being used for queries that return large result sets.
mysql extension is deprecated, instead use the MySQLi or PDO_MySQL.
mysql_query() only returns a resource for SELECT, SHOW, EXPLAIN, and DESCRIBE queries.
Here's another possible reason that I tested and was able to reproduce this error in two other different ways which have not been mentioned here.
1) Your SQL has the wrong syntax and looks like this (notice the incorrect extra comma after field3:
"select field1, field2, field3, from myTable";
2) Your SQL contains a duplicate field and looks like this:
"select field1, field2, field2 from myTable";
In both cases I got the message and it was displayed in different functions:
"Warning: mysql_result() expects parameter 1 to be resource, boolean given"
"Warning: mysql_free_result() expects parameter 1 to be resource, boolean given"
"Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given"
use this:
if (is_bool($result) === false) {
mysql_free_result($result);
}

I am getting error in mysql_fetch_assoc() [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
I am connected with my database and there seems to be an error appearing on this line of code:
while($br = mysql_fetch_assoc($brand))
and on my query I put this:
$brand = mysql_query("Select * from genratb");
The error says
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\videoshop\index.php on line 166
The first command is actually my line 166.
It looks like your query failed. The mysql_query call probably returned false, instead of the result resource.
$brand = mysql_query("Select * from genratb");
if (!$brand)
{
//error, query failed
}
else
{
while($br = mysql_fetch_assoc($brand))
{
//use row
}
}
If they query fails, then mysql_query() will return false. In that case, you need to look at mysql_error() to find out why the query failed.
The PHP documentation states:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
Most likely your query is failing for some reason, and setting $brand to FALSE. Are you sure your database connection is working, and your table exists?
You can add after your query line:
if (FALSE===$brand) { die(mysql_error()); }
This should tell you what is going wrong.

mysql_num_rows PHP

elseif(isset($_POST['submit']))
{
// Look for their user
$lookuser = mysql_query("SELECT * FROM `users` WHERE username='". mysql_escape_string($_POST['username']) ."'");
// If we find a row
if(mysql_num_rows($lookuser) > 0)
But my else for that, echos: An error has occured. <br> If you are sure you entered your username correctly, please contact an administrator.
I've tried to echo $_POST['username']; all works out fine. I've made sure my user exists, that works out fine.
The PHP error I get:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in password.php on line 23
Php.net says this about the return value of mysql_query:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
If you're getting a boolean, it's because there's a mysql error. Use mysql_error() to print the error, it'll help you diagnose the issue.
You probably have an error in your sql query. To be sure of that, echo after your mysql_query mysql_error().

Categories