num_rows Always shows Zero value in PHP/MYSQL? - php

I am totally new to this PHP.
I am just practicing Prepared statements.
I know there few questions which is related to mine.
But nothing Helped me.
This is my php code Which returns always num_rows equal to Zero.
But there is a data in my table
if (isset($_POST['submit'])) {
$my_id = 49;
$content = $_POST['cont'];
$content_date = date('d-m-y');
$check = "SELECT * FROM post WHERE user_id = ?";
$stmt = $con->prepare($check);
$stmt->bind_param("i",$my_id);
$stmt->execute();
$stmt->fetch();
$numberofrows = $stmt->num_rows;
$stmt->close();
echo '<h1>'.$numberofrows.'</h1>';
}

Use () look like this... $numberofrows = $stmt->num_rows(); and try.

Guys Thanx For your Ans
Finally My friend helped me to solve this.
We have to store the result before we calling num_rows
$stmt->store_result();
It works fine now

Related

How to SELECT * with multiple WHERE using fetch_assoc in prepared statements in PHP-MYSQL?

It's been two days and I have not found any solution for this on Google, so please help.
I have to SELECT * from one row with multiple WHERE clauses, using a prepared statements, I want to retrieve the results using fetch_assoc so that I don't have to bind variables using bind_result(), using fetch_assoc I can print many columns as $row['column_name']. My code is below and it's not running, giving no error.
$query = 'SELECT * FROM `table_name` WHERE `uid` = ? AND `email` = ?';
if ($stmt_select = $conn->prepare($query)) {
$stmt_select->bind_param('is', $user_id, $user_email);
$user_id = $_SESSION['uid']; //this variable is set and prints perfectly
$user_email = $_SESSION['user_email']; //this variable is set and prints perfectly
$result = $stmt_select->execute();
$stmt_select->store_result();
while ($row = $result->fetch_assoc()) {
print_r($row);
echo $row["supermarket_name"];
echo $row["supermarket-region"];
/*Nothing is being printed*/
}
$stmt_select->free_result();
$stmt_select->close();
}else{
echo 'query returned false';
}
The var_dump($result) returns (boolean)true
Since the above code isn't throwing any error and in last two days I couldn't figure out what's wrong with my code, can somebody let me know what's wrong with my code?
Thanks!

php sqlite3 - get result from prepared statement

All the examples and documentation didnt really help or even offer an example for this so I m gonna ask here:
$db = new SQLite3(database.db);
$stmt = $db->prepare('SELECT COUNT(uid) FROM kunden WHERE date = :date');
$stmt->bindValue(':ldate',$today,SQLITE3_TEXT);
$result = $stmt->execute;
How can I get the result from that prepared statement? I know execute is not supposed to return a result. I tried using query and query_single but that didnt work. var_dump($result->fetchArray()); also didnt work. Help is much appreciated.
SQLite3Stmt::execute() is a function, and needs to be called as such:
$db = new SQLite3('database.db');
$stmt = $db->prepare('SELECT COUNT(uid) FROM kunden WHERE date = :date');
$stmt->bindValue(':ldate',$today,SQLITE3_TEXT);
$result = $stmt->execute();
You can then fetch the result like this:
$array = $result->fetchArray();
echo $array['COUNT(uid)'];

PHP: How to select single value from single row in MySQL without using array for results

I am new to PHP and have a really basic question.
If I know the result of a query is only a single value (cell) from a single row in MySQL how can I simplify the below without having to go through an array of results and without increasing the risk of SQL injection ?
In the example below I would just need to echo a single email as the result of the query.
I found a couple of posts suggesting different approaches with fetch_field for this but I am not sure what is the best way here since some of these seem to be pretty old or deprecated now.
My PHP:
$stmt = $conn->prepare("SELECT email FROM Users WHERE userName = ? LIMIT 1");
$stmt->bind_param('s', $userName);
$stmt->execute();
$result = $stmt->get_result();
$arr = $result->fetch_assoc();
echo $arr["email"];
Many thanks in advance.
You can avoid caring what the column is called by just doing this:
<?php
$stmt = $conn->prepare("SELECT email FROM Users WHERE userName = ? LIMIT 1");
$stmt->bind_param('s', $userName);
$stmt->execute();
$email = $stmt->get_result()->fetch_object()->email;
echo $email;

PHP, MySQL statement results in ZERO rows

hope someone can help me.
i have a very simple prepared SELECT statment in PHP:
$query_select = ("SELECT * FROM companies where user_name = ? ");
$stmt = $mysqli->prepare($query_select);
$stmt->bind_param("s", $user_name);
$stmt->execute();
$count = $stmt->num_rows;
in companies table I have several rows with the $user_name i`m trying to query. But i still get 0 rows as a result.
The strange thing is that the non PREPARED version works:
$query = 'SELECT * FROM companies WHERE user_name="'.$user_name.'"';
$result = $mysqli->query($query);
$count= $result->num_rows;
echo "Aantal: ".$count;
So my question is, does anyone know why the prepared version returns ZERO and the non prepared version returns the correct number of rows?
Add this line to your code between execute and num_rows statement.
$stmt->store_result();
You have to store it before counting it.
For mysqli prepared statements, you must take an additional step: storing the result.
Try this:
$query_select = ("SELECT * FROM companies where user_name = ? ");
$stmt = $mysqli->prepare($query_select);
$stmt->bind_param("s", $user_name);
$stmt->execute();
$stmt->store_result(); // <-- new line
$count = $stmt->num_rows;
May be you need to bind the result:
/* bind result variables */
$stmt->bind_result($district);
Full example here

fetching one column using PDO

I have the following PDO statement:
$stmt = $db->prepare("SELECT MAX(RID) FROM TEMP_ROUTE");
$stmt->execute();
$rid = $stmt->fetch(PDO::FETCH_ASSOC);
$rid = (int) $rid["MAX(RID)"];
Is there any way so that I don't have to do the last statement there. In other words I want it to fetch $rid["MAX(RID)"] when doing the fetch.
Have a look at http://www.php.net/manual/en/pdostatement.bindcolumn.php and try using PDO::FETCH_BOUND instead perhaps? This won't make it shorter though. Or try something like this, from http://www.php.net/manual/en/pdostatement.fetch.php:
$stmt = $db->prepare("SELEXT MAX(RID) AS max FROM TEMP_ROUTE");
$stmt->execute();
$rid = $stmt->fetch(PDO::FETCH_OBJ)->MAX;

Categories