i have this function that brings me back a value by query a table in the database!
the code:
function recycle_check($recycle_id){
$query = "SELECT recycle_id FROM notes WHERE user_id = '".$_SESSION['user_id']."' and recycle_id ='$recycle_id'";
if(mysql_num_rows($query)== 0)
return 0;
else
return 1;
}
but its giving me an error saying:
Warning: mysql_num_rows() expects parameter 1 to be resource
i just want the function to either give me a zero or a number 1!
0 for exists and 1 for deosnt exist!
you need to run mysql_query before calling to mysql_num_rows
like :
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
what you need to pass to the function is:
The result resource that is being
evaluated. This result comes from a
call to mysql_query().
Related
So, I have written some query code returns the error:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
I understand that this is caused to a boolean response from the query, and I have checked it out, and the Boolean returned is equal to true. So I don't see why there is no response with a data array instead... Here's my code:
$data = mysqli_multi_query($connection, 'UPDATE teams SET teams.teamViews = teams.TeamViews
+ 1 WHERE (teams.teamID, \''.$userToken.'\') NOT IN (SELECT teams_views.teamId,
teams_views.'.$viewType.' FROM teams_views) AND teams.teamUrl = \''.TEAM_URL.'\';
INSERT INTO teams_views (teamId, '.$viewType.') SELECT t.teamId, \''.$userToken.'\'
FROM teams t WHERE t.teamUrl = \''.TEAM_URL.'\' AND NOT EXISTS (SELECT \''.$userToken.'\'
FROM teams_views WHERE t.teamId = teamId);
SELECT * FROM teams WHERE teams.teamUrl = \''.TEAM_URL.'\';');
$dataRow = mysqli_fetch_array($data, MYSQLI_ASSOC);
There are three queries in the SQL - An update, insert, and selection.
How could I alter my query or PHP to return data, rather than a boolean? Thanks
As suggested in a comment by spencer7593, my question was solved using a combination of mysqli_store_result, mysqli_free_result, and mysqli_next_result. The following is the function used to do this:
function multi_queries($query, $numQueries) {
$connection = new database_connection();
$data = mysqli_multi_query($connection->connection, $query) or die(mysqli_error($connection->connection));
$data = mysqli_store_result($connection->connection);
if (sizeof($data) > 0) {
$this->success = true;
do {
if ($result = mysqli_store_result($connection->connection)) {
while ($row = mysqli_fetch_row($result)) {
$this->data[sizeof($this->data)] = $row;
}
mysqli_free_result($result);
}
} while (mysqli_next_result($connection->connection));
}
$connection->close_connection();
}
This question already has an answer here:
sqlsrv_num_rows() expects parameter 1 to be resource, boolean given
(1 answer)
Closed 10 months ago.
This warning can happen for many different reasons, however, I think that my problem here is that the table APPT is empty/and doesn't contain any row with the t_schedule_id. I read every question that may already have my answer, and all answers stated the same thing "your SQL query is not being successful and a FALSE boolean value is being returneds "..So yes, I know that the query is returning false because there is 0 rows with the t_schedule_id and that is my point, I want my function to return the int 0 when there is 0 row with t_schedule_id
function.php
function get_appt($schedule_id,$conn){
$query = "select t_schedule_id as t_schedule_id from [dbo].[APPT] where t_schedule_id = .$schedule_id.; ";
$stmt = sqlsrv_query($conn,$query);
$row_count = sqlsrv_num_rows($stmt);
if ($row_count === false){
return 0;
}else{
return $row_count;
}
}
application.php
$get_appt = get_appt(1, $conn);
echo $get_appt;
So, my question is how can I stop getting this warning, no matter if the table return 0 If is 0 is 0 if is a number > 0 then return that number.
PS: I know there is no error in my query name such as table name, column name etc ... And, when I echo $get_appt; I get the value 0 but also the warning
Warning: sqlsrv_num_rows() expects parameter 1 to be resource, boolean given in line 220
And this is line 220 $row_count = sqlsrv_num_rows($stmt);
The query is failing to execute due to a syntax error in the query string. Consequently, sqlsrv_num_rows($stmt) throws an error. Remove the dots around $schedule_id so that $query becomes:
$query = "select t_schedule_id as t_schedule_id from [dbo].[APPT] where t_schedule_id = $schedule_id";
Checking for errors with sqlsrv_errors() will help to debug future issues. This returns an array and can be output using print_r(sqlsrv_errors(),true); - see
http://php.net/sqlsrv_errors.
What about:
function get_appt($schedule_id,$conn){
$query = "select t_schedule_id as t_schedule_id from [dbo].[APPT] where t_schedule_id = .$schedule_id.; ";
$stmt = sqlsrv_query($conn,$query);
if ($stmt === false){
return 0;
}else{
return sqlsrv_num_rows($stmt);
}
}
The syntax I think is like this:
$query = "select t_schedule_id as t_schedule_id from [dbo].[APPT] where t_schedule_id =" .$schedule_id."; ";
You missed 2 " charaters
I have this PHP code:
if ($username) {
if ($news == 1)
$int = "9";
if ($news == 0)
$int = "12";
}
else
$int = "9";
$query = mysql_query("SELECT * FROM files WHERE active='1' ORDER BY id DESC LIMIT $int");
require ("scripts/connect.php");
$numrows = mysql_num_rows($query);
But then I get this error (Note the line starting with $numrows is line 69):
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/Site/index.php on line 69
I have tried many ways of changing the number to no avail. Please help! Thanks!
If your scripts/connect.php is responsible for establishing database connection, then I belive mysql_query() should be below it, not above as it is now. I also recommend using mysql_error() and always check return values for errors, as assumptions like "query is always successful" are no-no approach and will hit you badly in less expected moment.
require ("scripts/connect.php");
$query = mysql_query("SELECT * FROM files WHERE active='1' ORDER BY id DESC LIMIT $int");
if( $query ) {
$numrows = mysql_num_rows($query);
...
} else {
die( mysql_error() );
}
Please note mysql extension is deprecated. Switch to mysqli or something more sophisticated like PDO as soon as you can.
So, im trying to do a poll, when i run mysql gives me this error, and i dont know what it means.
Warning: max() [function.max]: Array must contain at least one element in .... in line 18.
Could anyone please guide me of whats that?
This is my code:
$query = mysql_query("SELECT * FROM `poll` ORDER BY `id` ASC LIMIT 1");
$rows = mysql_num_rows($query);
if($rows > 0){
$poll = mysql_fetch_array($query);
$title = $poll['name'];
} else {
$title = 'No Poll Yet';
}
$query = mysql_query("SELECT COUNT(`id`) as hits FROM `responses` GROUP BY `qid`");
$me = array();
while($row = mysql_fetch_array($query)){
$me[] = $row['hits'];
}
$max = max($me); ////// THIS IS LINE 18
$query = mysql_query("SELECT `questions`.`pid` FROM `responses`, `questions` WHERE `responses`.`qid`=`questions`.`id` AND `responses`.`ip`='".$_SERVER['REMOTE_ADDR']."' AND pid='".$poll['id']."'");
line 18 is noted after "//////"
Any help ? :s
simply what the error-message says: $me is an empty array (caused by your sql-query returning nothing - why this happens is hard to say without seeing your tablestructure and contents of the table)
Just change in line number 18
if(!empty($me)) $max = max($me);
Full Code After Changes:
$query = mysql_query("SELECT * FROM `poll` ORDER BY `id` ASC LIMIT 1");
$rows = mysql_num_rows($query);
if($rows > 0){
$poll = mysql_fetch_array($query);
$title = $poll['name'];
} else {
$title = 'No Poll Yet';
}
$query = mysql_query("SELECT COUNT(`id`) as hits FROM `responses` GROUP BY `qid`");
$me = array();
while($row = mysql_fetch_array($query)){
$me[] = $row['hits'];
}
if(!empty($me)) $max = max($me); ////// THIS IS LINE 18
$query = mysql_query("SELECT `questions`.`pid` FROM `responses`, `questions` WHERE `responses`.`qid`=`questions`.`id` AND `responses`.`ip`='".$_SERVER['REMOTE_ADDR']."' AND pid='".$poll['id']."'");
You must be passing an empty array to the max() function. Just check the array is not empty before calling the max() or min() function. You can check by count($arr) > 0, Here are a few key points from the documentation.
max() in php, returns the parameter value considered "highest" according to standard comparisons. If multiple values of different types evaluate as equal (e.g. 0 and 'abc') the first provided to the function will be returned.
If an empty array is passed, then FALSE will be returned and an E_WARNING error will be emitted.
The values of different types will be compared using the standard comparison rules. For instance, a non-numeric string will be compared to an integer as though it were 0, but multiple non-numeric string values will be compared alphanumerically. The actual value returned will be of the original type with no conversion applied. If the first and only parameter is an array, max() returns the highest value in that array. If at least two parameters are provided, max() returns the biggest of these values.
I'm running the following query, expecting it to return an INTEGER, but it returns "Resource id #3"
What am I doing wrong?
$queryPlans = mysql_query("SELECT count(*) FROM infostash.rooms");
echo $queryPlans;
There are actually 15 rows in this table, and I would like to return the number 15.
Any suggestions?
mysql_query will return a php resource(see: http://www.php.net/manual/en/language.types.resource.php).
The returned resource should then be passed to mysql_fetch_assoc or similar.
Since you are only getting the count, you can use the following:
$queryPlans = mysql_query("SELECT count(*) FROM infostash.rooms");
$count = mysql_result($queryPlans,0,0);
echo $count;
You need:
$queryPlans = mysql_query("SELECT count(*) FROM infostash.rooms");
$row = mysql_fetch_array($queryPlans);
echo $row[0];
mysql_query() isn't returning the result. It's returning a resource you can loop across and interrogate for rows (as above).
This is actually expected behavior according to the documentation:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
It's a regular select that returns one row with one column and should be treated as such. You can call mysql_fetch_array on the result:
$row = mysql_fetch_array($resource);
$count = $row[0];
mysql_query() returns a result resource. You need another function the get "valuable information" from that resource. In this case mysql_fetch_array()/mysql_fetch_row()/mysql_fetch_object as cletus pointed out. Or (since it's only a single value) mysql_result().
Any sql query may fail for various reasons. You should always check the return value of mysql_query(). If it's FALSE something went wrong and mysql_error() can tell you more about it.
$mysql = mysql_connect(...) or die(mysql_error());
mysql_selecT_db(.., $mysql) or die(mysql_error($mysql));
$query = "SELECT count(*) FROM infostash.rooms";
$queryPlans = mysql_query($query, $mysql) or die(mysql_error($mysql));
$cRows = mysql_result($queryPlans, 0);
echo $cRows;
If you are planning on using the full query later (e.g. select , rather than count()), you can save yourself a database hit by using mysql_num_rows() on the full query. Example:
$queryPlans = mysql_query("SELECT * FROM infostash.rooms");
$results = mysql_fetch_array($queryPlans);
echo "There were " . mysql_num_rows($queryPlans) . " results";
while($row = mysql_fetch_assoc($queryPlans)){
// Do stuff here
}
$queryPlans = mysql_query("SELECT count(*) FROM infostash.rooms");
mysql_num_rows($queryPlans);
http://us.php.net/manual/en/function.mysql-num-rows.php