mysql_fetch_row() expects parameter 1 to be resource, object given.? [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
What is the cause of error in my code and how I can to fix?(i use of codeigniter)
$query_hotel_search = $this->db->query("SELECT * FROM hotel_submits WHERE name LIKE '%$hotel_search%' ORDER BY name asc");
$units = array();
while( $row = mysql_fetch_row( $query_hotel_search ) ) // Line 27
{
$units = unserialize( $row[0] );
}
echo json_encode(var_dump($units));
Error:
A PHP Error was encountered Severity: Warning Message:
mysql_fetch_row() expects parameter 1 to be resource, object given
Line Number: 27
Output: array(0) { } null
UPDATE:
Error:
A PHP Error was encountered Severity: Notice Message:
unserialize() [function.unserialize]: Error at offset 0 of 3 bytes
Line Number: 29
Output: bool(false) null
See my database: http://i.stack.imgur.com/IORSM.jpg
$query_hotel_search = mysql_query("SELECT * FROM hotel_submits WHERE name LIKE '%$hotel_search%' ORDER BY name asc");
if(mysql_num_rows($query_hotel_search)==0){
return '0';
}else{
$units = array();
while( $row = mysql_fetch_row( $query_hotel_search ) )
{
$units = unserialize( $row->units[0] ); // Line 29
}
echo json_encode(var_dump($units));
}

http://de2.php.net/manual/en/function.mysql-fetch-row.php
mysql_fetch_row() takes not the query itself but rather the result from the mysql_query command (which is a resource).
Look at the example at the PHP Documentation (linked at the top) and you will get the functionality of mysql_fetch_row.
Maybe the assignment of $query_hotel_search can clarify the situation.

You are using a codeigniter object as mysql resource. Instead you should use object iterator provided by codeigniter to loop through the results.
foreach($query_hotel_search->result() as $row)
{
// $row as object
}
foreach($query_hotel_search->result_array() as $row)
{
// $row as array
}
For the second part (UPDATE), mysql_fetch_row() return an enumerated array. so you should use:
$units = unserialize($row[12]);

Related

Trying to output data as an array [duplicate]

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 made a table that holds post and likes and I'm trying to output the of everything in the table but I get the
error: mysqli_num_rows() expects parameter 1 to be mysqli_result,
boolean given on line 18.
$sql3 = "SELECT * FROM post;";
$result = mysqli_query($conn,$sql3);
$datas = array();
if (mysqli_num_rows($result)> 0){
while($row = mysqli_fetch_assoc($result)){
$datas[] = $row;
}
}
foreach ($datas as $data){
echo $data;
}
I expect the all the information to be outputted, but the actual output is
mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean
given on line 18.
This means that your query is returning false instead of a mysqli_result object. The problem appears to be from the semicolon at the end of your statement. I believe that mysqli will see that as a multistatement. From https://www.php.net/manual/en/mysqli.quickstart.multiple-statement.php:
Multiple statements or multi queries must be executed with mysqli_multi_query(). The individual statements of the statement string are separated by semicolon. Then, all result sets returned by the executed statements must be fetched.
mysqli has a method to get the last error it encountered:
https://www.php.net/manual/en/mysqli.error.php
Example:
if(!$result){
printf("Error message: %s\n", mysqli_error($conn));
}

PHP warning expects parameter 1 to be resource, object given [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 1 year ago.
I want to retrieve all data from a table, so I use this code
<?php
include("config.php");
$sql = "SELECT * FROM ".$USERS;
$sql_result = mysqli_query($connection, $sql);
if ($sql_result) {
while ($result = mysql_fetch_assoc($sql_result)) {
echo $result;
}
}
else {
die ('Could not execute SQL query '.$sql);
}
?>
but got this warning:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource,
object given in C:\xampp\htdocs\newSDP\phpscript\users.php on line 6
How can I fix it?
Change:
mysql_fetch_assoc to mysqli_fetch_assoc

Can't select a number from MySQL PHP

I am trying to get a value from database and echo it. It seems I mixed something wrong, so at the moment just no value appears.
$postamount=$db->prepare("SELECT post_amount FROM users WHERE id = ? ");
$postamount->bind_param('s',$_SESSION['id']);
$postamount->execute();
$fsvsfnvdjn= mysqli_fetch_assoc($postamount);
$fvklnsfvnfv=implode($fsvsfnvdjn);
echo($fvklnsfvnfv);
Such warning appear in the log:
mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in /Applications/MAMP/htdocs/pageok.php on line 25
implode(): Argument must be an array in /Applications/MAMP/htdocs/pageok.php on line 26
Can you explain please, how to do this right?
Alter your script with the following
$postamount=$db->prepare("SELECT post_amount FROM users WHERE id = ? ");
$postamount->bind_param('s',$_SESSION['id']);
$postamount->execute();
$result = $postamount->get_result();
$arr = array();
while ($row = $result->fetch_assoc()) {
$arr[]=$row["post_amount"];
}
$str=implode($arr);
echo($str);

Create limit posting for users

I want to make users only can create maximum 10 post per day.
Here is my controller:
$id_user=$this->session->userdata('id');
$sql ="SELECT COUNT(id_post) as max_post FROM post
WHERE post.id_user=$id_user
AND DATE(post.time)=DATE(NOW())";
$result = $this->db->query($sql);
if ($result > 10) //line number 17
{
//redirect to home
} elseif($result <= 10 ) //line number 20
{
//then do post
Then is getting error like this:
A PHP Error was encountered
Severity: Notice
Message: Object of class CI_DB_mysql_result could not be converted to int
Line Number: 17
A PHP Error was encountered
Severity: Notice
Message: Object of class CI_DB_mysql_result could not be converted to int
Line Number: 20
Any answer?
Many thanks...
if ($result > 10)
$result Is the resource itself, you have to fetch data from it. $result itself is not what you are looking for. Fetch the required field from result.
For example
$row = $result->row();
if ($row->max_post > 10)
if(mysql_num_rows($result)>10)
{
}
try using this

Is mysql_fetch_array() supported in PHP 5.2.6?

I get this error message when I try to use mysql_fetch_array():
while( $deffefgpSfet->fetch() ) {
$roefe= mysql_fetch_array($ffefe, MYSQL_NUM);
Warning: mysql_fetch_array(): supplied argument is not a valid
MySQL result resource in <b>/opt/lampp/htdocs/index.php on line 157
You don't show the code for the object $defeft but I can assume what you're trying to do...
$blah = mysql_query("SELECT 1 FROM information_schema.tables");
if(!$blah) { // check for mysql errors
echo mysql_error();
exit;
}
while( $defeft = mysql_fetch_array($blah)) {
echo $defeft['row'];
}
Defeft holds the rows data in a array, and while loops through each row.
doesn't that error refer to using mysql_fetch_X on a variable that is not a Resource, e.g. the query failed, ensure your query returns a valid result, rather than a mysql error?
$a = mysql_query($sql) or die();
try this (or similar) :)

Categories