This question already has an answer here:
Call to undefined function mysqli_result::num_rows()
(1 answer)
Closed 6 years ago.
Alright so I am getting an error that there is no function called num_rows for some reason, anyone knows why?
$result = $sql->query("SELECT * FROM private_messages WHERE sender='".$this->getUsername()."' LIMIT $page_count,5");
$count = $result->num_rows();
Fatal error: Call to undefined method mysqli_result::num_rows()
I attemped to search for the problem online but couldnt find any successful results regarding this issue.
There's no num_rows method in MySQLi_Result. Check MySQLi_Result up in PHP's documentation: http://php.net/manual/pt_BR/class.mysqli-result.php
Perhaps you mean mysqli_num_rows static method or even the num_rows property (in which case you should use without the parenthesis):
$result = $sql->query("SELECT * FROM private_messages WHERE sender='".$this->getUsername()."' LIMIT $page_count,5");
$count = $result->num_rows;
Because its not a method. Its a variable. From the docs,
Object oriented style
int $mysqli_result->num_rows;
So in your case, you should do
$result = $sql->query("SELECT * FROM private_messages WHERE sender='".$this->getUsername()."' LIMIT $page_count,5");
$count = $result->num_rows;
Related
This question already has answers here:
How to fetch all in assoc array from a prepared statement?
(4 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 2 years ago.
I am trying to fetch TaxRate data from a table and I'm trying to select the first array only. However, I keep getting this error
"Fatal error: Uncaught Error: Call to a member function fetch_array()
on boolean in C:\xampp\htdocs\flowerique\shoppingCart.php:77 Stack
trace: #0 {main} thrown in C:\xampp\htdocs\flowerique\shoppingCart.php
on line 77"
Does anyone know the cause of this error and how do I fix it? Thanks!
This is my code :
//Retrieve Current GST Pricing
$qry = "SELECT * FROM gst GROUP BY EffectiveDate DESC";
$stmt = $conn->prepare($qry);
$stmt->execute();
//$stmt->close();
$result = $conn->query($qry);
$row = $result->fetch_array(); // This is line 77
while($row["EffectiveDate"] < date("Y-m-d"))
{
$row = $result->fetch_array();
}
$currentTaxRate = $row["TaxRate"];
This question already has answers here:
Single result from database using mysqli
(6 answers)
Closed 3 years ago.
I'm trying to echo the result of a mysqli_query however I keep getting the error 'Catchable fatal error: Object of class mysqli_result could not be converted to string' on line 'echo $result;'.
Is there any way I can convert it into a string so that it can be echoed? (P.S sorry if this is easy, I'm new to coding.)
My database is successfully connected and the SQL statements definitely work.
$sql= "SELECT ImageURL FROM `unnormalisedtable` WHERE Yeargroup = 9 ORDER BY RAND() LIMIT 1" ;
$result = mysqli_query($db, $sql);
echo $result;
The expected output is that the result of my SQLi query will be printed on screen, however the error is generated instead. Thanks for any help in advance.
Since you limit the selection to one entry use
$row = mysqli_fetch_array($result);
echo $row['ImageURL'];
If you select more than one entry loop over the result.
while($row = mysqli_fetch_array($result)) {
echo $row['ImageURL'];
}
This question already has answers here:
Call to a member function execute() on boolean in [duplicate]
(5 answers)
Closed 5 years ago.
code:
$results = $mysqli->prepare("SELECT news_title FROM news ORDER BY posted_time DESC");
$results->execute();
$results->bind_result($news_title);
When I execute the code then it will return Fatal error: Call to a member function execute() on boolean in /home/*****/public_html/lnews.php on line 22. So, How can I fix this issue ?please help
Thank You
There is some error from database due to this the prepare method returns FALSE, on success this method returns prepared statement.
You can see the logs to determine the database error. Also, you should change your code in the following way and throw some exception in else.
$results = $mysqli->prepare("SELECT news_title FROM news ORDER BY posted_time DESC");
if($results !== false) {
$results->execute();
$results->bind_result($news_title);
}
In this case if false return (if there is database error) the execute method would not be invoked.
This question already has answers here:
Catchable fatal error: Object of class PDOStatement could not be converted to string in D
(2 answers)
Closed 6 years ago.
I know this question has already been answered but i doesn't realy get the solution of my problem. I actualy try to select the numbers of salaries to a sql base but i have this error : "Catchable fatal error: Object of class PDOStatement could not be converted to string"
Here is my code:
function getNbSalaries(){
global $pdo;
$query = "SELECT count(*) as nb FROM salaries ;";
$prep= $pdo->prepare($query);
$prep->fetch(PDO::FETCH_ASSOC);
return $prep;
}
You have to use either
->prepare()
->execute()
or
->query()
In this case as it is a query without any parameters this may well be the simplest solution
function getNbSalaries(){
global $pdo;
$query = "SELECT count(*) as nb FROM salaries";
$prep= $pdo->query($query);
$row = $prep->fetch(PDO::FETCH_ASSOC);
return $row;
}
It is not a good idea to use global like this, it would be a better idea to pass the connection as a parameter of the function like this
function getNbSalaries($pdo){
$query = "SELECT count(*) as nb FROM salaries";
$prep= $pdo->query($query);
$row = $prep->fetch(PDO::FETCH_ASSOC);
return $row;
}
$row = getNbSalaries($pdo);
echo $row['nb']'
that don't work but i see where is the problem i think:
my function :
function getNbSalaries($pdo){
$query = "SELECT count(*) as nb FROM salaries ;";
$prep= $pdo->prepare($query);
$prep->fetch(PDO::FETCH_ASSOC);
$prep->execute();
return $prep;
}
and there is my code to return the result on my main page:
<p>Nombre de salariƩs : <?= getNbSalaries(); ?> </p>
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 getting this error while trying to run a function. I am sending a value $id to this function and trying to return the associated array. But I get a warning supplied argument is not a valid MySQL-Link resource.
But if i run the same code by eliminating the function part and give a static value to $id it returns the result correctly. This is the function
<?php
mysql_select_db($database_spyware, $spyware);
function get_product($id)
{
$query = sprintf("select product.descr as product_descr, product_id as Product_id,
category.descr as category from sp_url
inner join product
on product.id = sp_url.product_id
inner join category
on product.category_id = category.id
where sp_url.id = $id");
$query_result = mysql_query($query, $spyware) or die(mysql_error());
$row_rs_query = mysql_fetch_assoc($query_result);
$totalRows_rs = mysql_num_rows($query_result);
return($row_rs_query);
}
?>
That happens because your second parameter spyware in function "mysql_query" is declared outside the function and is unreadable on your function scope.
try
global $spyware;
at the beginning of the function.
or leave the second parameter empty.