This question already has answers here:
php pdo get only one value from mysql; value that equals to variable
(3 answers)
Closed 2 years ago.
I am trying to convert my mysqli to PDO using PostgreSQL and I am wondering what is the equivalent way of writing the following code in PDO
$result = $mysqli->prepare("SELECT FOUND_ROWS()");
$result->execute();
$result->bind_result($total_rows);
$result->fetch();
What I tried is the following:
$stmt = $this->pdo->prepare('SELECT FOUND_ROWS()');
$stmt->execute();
but I am not sure how to convert the rest of the my_sqli logic to my new PostgreSQL PDO, in particular the $result->bind_result($total_rows); and fetch().
PDO doesn't have anything equivalent to bind_result(). PDOStatement::fetch() returns the row as an array or object, and you extract the result columns from that. So instead of
$result->bind_result($total_rows);
$result->fetch();
echo $total_rows;
you use
$row = $stmt->fetch(PDO::FETCH_NUM);
echo $row[0];
It's generally easier if you assign aliases to the columns being selected, then you can fetch an associative array.
$stmt = $this->pdo->prepare('SELECT FOUND_ROWS() AS total_rows');
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $row['total_rows'];
Related
This question already has answers here:
Single result from database using mysqli
(6 answers)
Closed 5 months ago.
I am trying to get a value from column "odznak" in "users" tab for user "user01" and store it in variable $odznak (for searching in another tab.
$stmt = $conn->prepare("SELECT odznak FROM users WHERE username = 'user01'");
$stmt->execute();
$result = $stmt;
$odznak;
You need to fetch the data (say into an associative array)
On the other hand, as a good practice, please use parameterized prepared statement in your select query
So, change to:
$stmt = $conn->prepare("SELECT odznak FROM users WHERE username = ?");
$stmt->bind_param("s", 'user01');
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$odznak=$row["odznak"];
Now, $odznak is the retrieved data
This question already has answers here:
get array of rows with mysqli result
(2 answers)
Closed 1 year ago.
The query is simple -> "SELECT * FROM xxx";
When the result comes, it brings only the last result of the table instead all.
If I'm not mistaken, most remote databases come with a limit on the amount of data that is fetched, in order to avoid overflowing results.
But none of that is for sure, just my speculation.
Anyway, how to solve this?
$stmt = mysqli_stmt_init($conn);
$sql = "SELECT * FROM favorite";
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result);
var_dump($row);
Try
$row = mysqli_fetch_all($result);
mysqli_fetch_assoc();
only returns 1 row which explains why you only see the last row of the expected result set.
mysqli_fetch_all
This question already has answers here:
Can PHP PDO Statements accept the table or column name as parameter?
(8 answers)
Closed 5 years ago.
I have this code to get a COUNT DISTINCT data:
$param = 'email';
$stmt = $conn->stmt_init();
$stmt = $conn->prepare("SELECT COUNT(DISTINCT(?)) FROM contatos");
$stmt->bind_param('s',$param);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($count);
while ($stmt->fetch()) {
echo $count;
}
But echo $count always returns 1, but i have dozens of records...
What is wrong?
Thanks
Binding is not allowed for column names (or table names). Your query is not executing correctly. You need to directly pass the name of the field.
$stmt = $conn->prepare("SELECT COUNT(DISTINCT(email)) FROM contatos");
This question already has answers here:
Row count with PDO
(21 answers)
Closed 2 years ago.
Having issues returning number of results in PHP SQLSRV PDO connection, when I try $stmt->rowCount(); get -1 result, really don't get it.
...
...
...
if(empty($region)){
$query2 = "SELECT [QuotID], [QuotNumber], CreationDate, QuotDate
FROM [dbo].[vQuotaion]
GROUP BY [QuotID]
,[QuotNumber]
,[CreationDate]
,[QuotDate]
HAVING CreationDate >='".$fdate."' AND CreationDate <='".$edate."' AND ProType = 'OPSFi' ORDER BY CreationDate DESC";
$stmt2 = $conn->query( $query2 );
} else {
...
...
...
}
...
...
...
<?php
if(empty($stmt2)){
echo '';
}else{
while ($result = $stmt2->fetch(PDO::FETCH_ASSOC)){
bla bla bla;
}
}
?>
If you want to count the rows without a separate query you can do this with PDO:
$rows = $stmt2->fetchAll();
$num_rows = count($rows);
There is no way to directly count rows when using a SELECT statement with PDO for all database drivers. You could create a function using the code above if you need to retrieve counts regularly.
Warning!
Little Bobby says your script is at risk for SQL Injection Attacks.. Even escaping the string is not safe! Learn about prepared statements for PDO.
You can get the row count of a select query with the PDO versions of the sqlsrv drivers however, like the standard version of the drivers (non-PDO), you have to specify a scrollable cursor. Like so:
$query = "SELECT * FROM myTable";
$stmt = $conn->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute();
$rows = $stmt->rowCount();
The default cursor used is PDO::CURSOR_FWDONLY which, when rowCount() is used, returns -1.
Microsoft documentation.
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 9 years ago.
Trying to get the data from a table in my database and store it into a textfield named "about"
however i keep getting an error:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, object given in
<?php
require("common.php");
$query = $db->prepare("SELECT * FROM about");
$result = $query or die(mysql_error()); // run the query
$row = mysql_fetch_assoc($result); // fetch a result row
echo $row['about'];
?>
thats because you are not executing your query.after Prepare use Execute().your code will look something like this
<?php
require("common.php");
$query = $db->prepare("SELECT * FROM about");
$query->execute();
$result = $query->fetch(PDO::FETCH_ASSOC);
print_r($result);//to check the elements of the array
echo $row['content'];
?>
Remember
PDO::prepare() - Prepares a statement for execution and returns a statement object
PDOStatement::execute() - Executes a prepared statement
You are missing mysql_query();. This to execute your sql query.
$query = "SELECT * FROM about";
$result = mysql_query($query) or die(mysql_error());