Plz Help i Dont know what is wrong in this function ....
$gsql = "SELECT * FROM posts WHERE group='$group_name' ORDER BY postdate DESC LIMIT 0,20";
$gquery = mysqli_query($db_conx, $gsql);
$gstatusnumrows = mysqli_num_rows($gquery);
while ($grow = mysqli_fetch_array($gquery, MYSQLI_ASSOC)) {
and it keeps saying this error :-
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in D:\group.php on line 3
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in D:\group.php on line 5
That means your query failed.
[mysqli_query] returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
So use mysqli_error to find out what you did wrong. In this case, though, it's because you have a column named "group". GROUP is a reserved word in MySQL. To be on the safe side, ALL database, table and column names SHOULD be enclosed in backticks ` to prevent any possible ambiguity.
Related
This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 1 year ago.
I'm building a search within my site.
I have a problem with the DB. It's giving me this:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP-5.3.5.0\www\searchscript\search.php on line 86
I'll show you the code section where it gives me such error
line 82: $query = "SELECT * FROM dreams WHERE titolo,titch LIKE \"%$trimmed%\" ORDER BY id_dreams DESC ";
line 85: $numresults=mysql_query($query);
line 86: $numrows=mysql_num_rows($numresults); //error
Now I tried to see what is the problem behind the query and it's telling me this:
SELECT * FROM dreams WHERE titolo, titch LIKE "%tags%" ORDER BY id_dreams DESC
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'titch LIKE "%tags%" ORDER BY id_dreams DESC' at line 1
The code behind this is:
$query = "SELECT * FROM dreams WHERE titolo, titch LIKE \"%$trimmed%\" ORDER BY id_dreams DESC ";
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
The mysql_query is returning a boolean value meaning the sql query is probably failing and you're getting a false returned rather than a mysql resource.
Have you checked your query?
You forgot to check whether $num_results is a MySQL result resource. In this case your query errored, so it's FALSE instead.
Re-read the documentation for mysql_query and ensure you program for all possible cases.
I've searched the site for an answer but couldn't find any answer for my specific problem that worked.
I have two files, the first one is my index.php of-course, the second is my functions.php file, which obviously contains the functions, I made this function:
function sql_get($lang, $tabler, $rower){
if ($lang == "heb") {
$result = mysql_query("SELECT * FROM %s WHERE id = 0",
mysql_real_escape_string($tabler));
$row = mysql_fetch_array($result);
return $row['$rower'];
}
else if ($lang == "rus") {
$result = mysql_query("SELECT * FROM %s WHERE id = 0",
mysql_real_escape_string($tabler));
$row = mysql_fetch_array($result);
return $row['$rower'];
}
this code supposed to get an information about the language (from a get, it gets it, it's all fine with that), the sql table and the specific row from this table where the id is 0.
and return the information from the row inserted.
My warnings and errors when the language is "heb":
Warning: mysql_query() expects parameter 2 to be resource, string given in /home/elenbyin/public_html/elenby.co.il/vadim/functions.php on line 16
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/elenbyin/public_html/elenby.co.il/vadim/functions.php on line 17
and when the language is "rus":
Warning: mysql_query() expects parameter 2 to be resource, string given in /home/elenbyin/public_html/elenby.co.il/vadim/functions.php on line 23
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/elenbyin/public_html/elenby.co.il/vadim/functions.php on line 24
the function call in the index.php file looks like that:
I will be thankful for the people who will try to help and especially for those who will help me.
Thanks A lot!
Firstly, you haven't connected to the database inside your function. If you've opened your database somewhere then you can get away with this with mysql, but you shouldn't.
Secondly, you're passing your variable as a second parameter to mysql_query(), but mysql_query() expects the query to be complete as the first parameter, and the second parameter should be the connection resource tht connects to the database, which you haven't got.
From the structure of the query it looks like you intend to use sprintf() to create it.
This should create the query for you, assuming you have opend a databse connection:
$query = sprintf("SELECT * FROM %s WHERE id = 0",
mysql_real_escape_string($tabler));
$result = mysql_query($query) or die(mysql_error());
Note: mysql is deprecated. You shouldn't use it. Use mysqli or PDO instead. You'll need to be more rigorous about passing in your database connecions if you use mysqli.
As Amal suggested - better not use mysql_* functions cause not only they're deprecated but also vulnerable to sql-injection.
That said, in order to fix your code you should do:
$table = mysql_real_escape_string($tabler);
$sql = "SELECT * FROM $table WHERE id = 0";
$link=mysql_connect('host','user','pass');
$result = mysql_query($sql, $link);
...
as the second (and optional) parameter of mysql_query should be a resource - not a string!
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);
}
This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 2 years ago.
I'm building a search within my site.
I have a problem with the DB. It's giving me this:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP-5.3.5.0\www\searchscript\search.php on line 86
I'll show you the code section where it gives me such error
line 82: $query = "SELECT * FROM dreams WHERE titolo,titch LIKE \"%$trimmed%\" ORDER BY id_dreams DESC ";
line 85: $numresults=mysql_query($query);
line 86: $numrows=mysql_num_rows($numresults); //error
Now I tried to see what is the problem behind the query and it's telling me this:
SELECT * FROM dreams WHERE titolo, titch LIKE "%tags%" ORDER BY id_dreams DESC
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'titch LIKE "%tags%" ORDER BY id_dreams DESC' at line 1
The code behind this is:
$query = "SELECT * FROM dreams WHERE titolo, titch LIKE \"%$trimmed%\" ORDER BY id_dreams DESC ";
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
The mysql_query is returning a boolean value meaning the sql query is probably failing and you're getting a false returned rather than a mysql resource.
Have you checked your query?
You forgot to check whether $num_results is a MySQL result resource. In this case your query errored, so it's FALSE instead.
Re-read the documentation for mysql_query and ensure you program for all possible cases.
This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 8 months ago.
I get following Error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in......
Here is my Query:
$query = "SELECT ListNumber FROM residential";
$result1 = mysql_query($query);
if (mysql_num_rows($result1) >10){
$difference = mysql_num_rows($result1) - 10;
$myQuery = "SELECT * FROM `residential` ORDER BY `id` LIMIT 10,". $difference;
$result2 = mysql_query($myQuery);
while ($line = mysql_fetch_array($result2, MYSQL_BOTH))
Your query ($myQuery) is failing and therefore not producing a query resource, but instead producing FALSE.
To reveal what your dynamically generated query looks like and reveal the errors, try this:
$result2 = mysql_query($myQuery) or die($myQuery."<br/><br/>".mysql_error());
The error message will guide you to the solution, which from your comment below is related to using ORDER BY on a field that doesn't exist in the table you're SELECTing from.
The code you have posted doesn't include a call to mysql_fetch_array(). However, what is most likely going wrong is that you are issuing a query that returns an error message, in which case the return value from the query function is false, and attempting to call mysql_fetch_array() on it doesn't work (because boolean false is not a mysql result object).
mysql_fetch_array() expects parameter 1 to be resource boolean given in php error on server if you get this error : please select all privileges on your server. u will get the answer..
This error comes when there is error in your query syntax check field names table name, mean check your query syntax.