i want to Display the Result ('Positio: 2' or 'Position: 1') via a Echo
but $statement is a Object of class PDOStatement and not a String, how do i get it to see only the result of $statement? Thank you
<?php
$pdo = new PDO('mysql:host=localhost;dbname=testdb1', 'root', '');
$idV = $_GET['id'];
$statement = $pdo->prepare("SELECT position FROM idtabelle WHERE idnumber = ?");
$statement->execute(array($idV));
echo "Position: //$result_Of_Statement\\ ";
?>
Here's how I would do it:
$statement = $pdo->prepare("SELECT position FROM idtabelle WHERE idnumber = :idV");
$statement->bindParam(':idV', $idV);
$statement->execute();
while ($row = $statement->fetch(PDO::FETCH_ASSOC))
{
echo $row['position'];
}
You could also do away with the while loop if you wanted to.
I would fetch the result and use print_r or var_dump to see the result quickly.
$statement = $pdo->prepare("SELECT position FROM idtabelle WHERE idnumber = ?");
$statement->execute(array($idV));
$result = $statement->fetch(PDO::FETCH_ASSOC); //Or fetchAll()
echo "Position: ". print_r($result);
That's a quick and dirty way to inspect the result. If you want to add some formatting:
$result = $statement->fetch(PDO::FETCH_ASSOC);
for( $result as $key=>$value ){
echo ucfirst($key) .": $value \n";
}
If you want to get multiple rows wrap that in a loop where you fetch a new row each iteration
while( $result = $statement->fetch(PDO::FETCH_ASSOC) ){
echo "===New Row===";
for( $result as $key=>$value ){
echo ucfirst($key) .": $value \n";
}
}
Related
I have got the query below and i am trying to loop through the result but the echo only returns the second letter of the array instead of the whole 2nd array.
$tsql = "SELECT UserId, Email FROM Membership";
$stmt = sqlsrv_query($conn, $tsql);
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC);
while ($row) {
$body .= $row[0];
foreach($row as $email)
{
echo $email[1]. "<BR> ";
}
}
;
your code has too many issues. you should use something like
$tsql = "SELECT UserId, Email FROM Membership";
$stmt = sqlsrv_query($conn, $tsql);
while ($row = sqlsrv_fetch_assoc($stmt, SQLSRV_FETCH_ASSOC) ) {
echo $row [UserId]. "<BR> ";
echo $row [Email ]. "<BR> ";
};
$query = "SELECT * FROM main";
if ($result = $db->query($query)) {
while ($row = $result->fetch_assoc()) {
$name= $row["name"];
$image = $row["image"];
}
}
then somewhere in my code I print it out using like echo $name; but I got only one result, which is the last row. I know I have to do foreach but what variable should I put?
foreach($result as $results) ? like this?
On every iteration, you reassign the values to $name and $image, causing it to show only the last value when it leaves the loop.
You can either echo them right away in the loop, or populate an array or an object with them, so they will be available later.
Example:
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = array('name'=>$row["name"], 'image'=>$row["image"]); // push into the array
}
var_dump($data); // it's all here now
And to echo the data later, one of the ways is foreach:
foreach($data as $row) {
echo "Name: ", $row['name'], "; Image: ", $row['image];
}
$query = "SELECT * FROM main";
$result = $db->query($query);
$row = $result->fetch_assoc();
foreach ($row as $rows) {
echo $rows['name'] ." ". $rows['image'];
}
I have an SQL query which I have recently turned into a prepared statement for security purposes. I have a query which returns many rows, each consisting of many columns. My question is how to echo the results using a while loop. My example I have so far:
$stmt = $conn->prepare("SELECT * FROM Customers
WHERE travel_Date >= ?
AND travel_Date <= ?
".$searchOption."
LIMIT ?
OFFSET ?");
$todayDateFrom = $todayDate." 00:00:00";
$todayDateTo = $todayDate." 23:59:59";
$stmt->bind_param("ssii", $todayDateFrom, $todayDateTo, $limit, $offset);
$stmt->execute();
while ($stmt->fetch()) {
//echo first name
//echo surname
//echo address
//echo number
//echo type
//15 other things i need to print off
}
I'm not sure what the best way to do this is. I have thought about:
$stmt->bind_result($firstName, $surname, $address, //etc);
But I'm wondering if there's another alternative similar to unprepared statements:
while($row = mysqli_fetch_array($query)){
echo $row['firstName'];
echo $row['surname'];
echo $row['address'];
//etc
}
try this:
$result = $stmt->get_result();
while ($row = $result->fetch_array())
{
echo $row['firstName'];
echo $row['surname'];
echo $row['address'];
}
try this -
$result_arr = array();
while($row = $stmt->fetch()){
$record = array();
$record['firstname'] = $row['firstname']; // if your db column contains firstname column other wise you can also use $row[0];
$record['name'] = $row[1]; // end so on .......
$result_arr[] = $record;
}
hope this will work for u...
I need to add an if/then into my PDO::FETCH_OBJ result statement to display a "No Records Found" message if the query is blank.
My working query:
<?php
$command = "SELECT ";
$command .= "id, ";
$command .= "firstName, ";
$command .= "FROM mytable ";
$command .= "ORDER BY sortOrder";
$STH = $DBH->query($command);
$STH->setFetchMode(PDO::FETCH_OBJ);
while($row = $STH->fetch()) { ?>
<tr>
<td><?php echo $row->firstName; ?></td>
</tr>
<?php } ?>
I was able to find similar code that would work for an array, but I can't get it to work with the FETCH_OBJ code above.
This is similar code that illustrates the if/then I would like to implement:
$stmt = $conn->prepare('SELECT * FROM myTable WHERE id = :id');
$stmt->execute(array('id' => $id));
$result = $stmt->fetchAll();
if ( count($result) ) {
foreach($result as $row) {
print_r($row);
}
} else {
echo "No rows returned.";
}
I'm brand new to PDO so I'm sure it's a context issue, I just can't get it to work.
Set a variable to determine if you found any rows during the loop.
$no_rows_found = true;
while ($row = $STH->fetch() {
$no_rows_found = false;
...
}
if ($no_rows_found) {
echo "No rows found";
}
I am not able to figure out why all of my results are repetitions of the first values it returns.
This code returns the same ID and formatted date repeated over and over again; however, I was expecting it to read a value and then transform that value for each entry in the DB. Here is my code:
<?php
include('../includes/conn.inc.php');
$stmt = $mysql->prepare("SELECT id, endDate FROM TABLE ORDER BY id");
$stmt->execute();
$stmt->bind_result($id, $endDate);
while($row = $stmt->fetch()) {
$dataRow[] = array('id'=>$id,'endDate'=> $endDate);
};
foreach($dataRow as $i) {
$newEndDate = date('Y-m-d',strtotime($endDate));
$sql = 'UPDATE TABLE SET startDate = ? WHERE id= ? ';
$stmt = $mysql->stmt_init();
if ($stmt->prepare($sql)) {
$stmt->bind_param('si',$newEndDate, $id);
$OK = $stmt->execute();}
if ($OK) {
echo $id . " " . $newEndDate . "done <br/>";
} else {
echo $stmt->error;
}
$stmt->close();
};
In your foreach you are always using the last values that were set from the last $stmt->fetch()
Try:
foreach($dataRow as $i) {
$newEndDate = date('Y-m-d',strtotime($i['endDate']));
$id = $i['id'];
$sql = 'UPDATE TABLE SET startDate = ? WHERE id= ? ';
$stmt = $mysql->stmt_init();
if ($stmt->prepare($sql)) {
$stmt->bind_param('si',$newEndDate, $id);
$OK = $stmt->execute();
}
if ($OK) {
echo $id . " " . $newEndDate . "done <br/>";
} else {
echo $stmt->error;
}
$stmt->close();
};
use get_result();
$dataRow = array()
$stmt = $mysql->prepare("SELECT id, endDate FROM TABLE ORDER BY id");
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_array()) {
$dataRow[$row['id']] = $row['endDate'];
}
and you don't populate your $endDate in the second loop
foreach($dataRow as $i => $endDate){
$newEndDate = date('Y-m-d',strtotime($endDate));
... // rest of your code