Unable to retrieve query [duplicate] - php

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 6 years ago.
I cant retrieve value from database. Below is my query
<?php
$latestcover = queryTable("SELECT id, filename, title, thumbnail FROM borneo_ezone ORDER BY id DESC LIMIT 1");
foreach ($latestcover as $key){
echo '<p><img src="'.$key[3].'" alt="Borneo Ezone '.$key[0].'" /></p>';
echo '<p class="more">'.$key[0].'th issue of Borneo Ezone. Read more »</p>';
}
?>
Receive warning for PHP deprecated. How can I use mysqli to retrieve the query.
this my queryTable function
function queryTable($ask){
$query = mysqli_query($dbhandler,$ask); //query the db
$resArr = array(); //create the result array
while($row = mysqli_fetch_array($query)) { //loop the rows returned from db
$resArr[] = $row; //add row to array
}
return $resArr;
}

$latestcover = "SELECT id, filename, title, thumbnail FROM borneo_ezone ORDER BY id DESC LIMIT 1";
if ($result = $mysqli->query($latestcover)) {
while ($row = $result->fetch_row()) {
echo '<p><img src="'.$row[3].'" alt="Borneo Ezone '.$row[0].'" /></p>';
echo '<p class="more">'.$row[0].'th issue of Borneo Ezone. Read more »</p>';
}
$result->close();
}
try this code

Related

How do I get a value outside the loop using PHP [duplicate]

This question already has answers here:
How to get comma separated values from database [duplicate]
(3 answers)
Converting MySQL results into comma separated values
(4 answers)
Can I concatenate multiple MySQL rows into one field?
(16 answers)
Closed 3 years ago.
I'm creating a small website using PHP, which is generally a site for showcasing the hospital, and I modified the code given in this example:
https://www.w3schools.com/php/php_mysql_select.asp
<?php
$query = "SELECT * FROM emp WHERE type = 'woman' ";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$cat = $row["cat"] . ',';
echo $cat;
////<---- echo on while (Loop)
}
}
The expected output would be as follows:
Output: 35,36
But I changed the code with the link above and it is as follows:
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$cat = $row["cat"] . ',';
}
echo $cat;
///// <---- echo Out of While (Loop)
}
Output: 35
My expecting output would be 35, 36 outside of "while" using "echo".
What code do you recommend to output "35,36" the same code above?
You can try the below code to achive your requirement
$data = array();
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result)) {
$data[] = $row["cat"];
}
}
echo implode(",",$data);

Loop Through Array using PHP and MySQLi [duplicate]

This question already has answers here:
How to loop through a mysql result set
(6 answers)
Closed 3 years ago.
I'm trying to loop through a MySQL table using PHP, but it is only showing one line.
//Retrieve a list of outstanding developments
$sql = "SELECT * FROM tblDevelopment WHERE strStatus=?";
$statement = mysqli_stmt_init($conn);
//Check for any errors in the SQL statement
if (!mysqli_stmt_prepare($statement,$sql)){
//Report any errors with the prepared $statement
header("Location: ../sqlerror.php");
exit();
} else {
//If there are no errors, query the database for the username
mysqli_stmt_bind_param($statement,'s', $status);
mysqli_stmt_execute($statement);
$results = mysqli_stmt_get_result($statement);
if ($row = mysqli_fetch_assoc($results)) {
echo 'header';
while ($row = mysqli_fetch_assoc($results))
{
echo $row['strDetail'] . "</";
}
echo 'footer';
} else {
echo 'No results to display';
}
}
The code works when there are no results, but it only shows one result when there are more than one - any ideas what I'm doing wrong?
You are almost there... you need to keep calling mysqli_fetch_assoc until you reach the end of the result set. Changing your if to a while should be enough to get you rolling.
while (($row = mysqli_fetch_assoc($results)) !== null) {

SQL select as a PHP function [duplicate]

This question already has answers here:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in
(3 answers)
Closed 7 years ago.
I'm selecting data from MySQL. My code looks like this:
$sql = "SELECT oznacenie_odpadu FROM zdroj_dat ORDER by ID ASC";
if ($result = $conn->query($sql)) {
// fetch associative array
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row['oznacenie_odpadu'].'" >'.$row['oznacenie_odpadu'].'</option>';
}
}
This part of code is in my code multiple times. I'm calling it 5times only string "oznacenie_odpadu" is changing. Therefore I made function:
function select($data) {
$sql = "SELECT rozmer FROM zdroj_dat ORDER by id ASC";
if ($result = $conn->query($sql)) {
// fetch associative array
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row[$data].'" >'.$row[$data].'</option>';
}
}
}
Calling it with select("somevalue");
Syntax is ok because I didn't change anything but when I load the page the data from database are not retrieved.
You have a variable scope issue. $conn is not available inside of your function. You need to pass it as a parameter so ic an be used inside of your function.
function select($conn, $data) {
$sql = "SELECT rozmer FROM zdroj_dat ORDER by id ASC";
if ($result = $conn->query($sql)) {
// fetch associative array
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row[$data].'" >'.$row[$data].'</option>';
}
}
}
Call it:
select($conn, "somevalue");

Using PDO to echo display all rows from a table [duplicate]

This question already has answers here:
How can I use PDO to fetch a results array in PHP?
(2 answers)
Closed 2 years ago.
I'm trying to echo out all the rows of a table using PDO but am running into trouble.
With the old way of doing I'd have done it like
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){
$title= $row['title'];
$body= $row['body'];
}
But with PDO I'm trying;
$result = $db->prepare("SELECT title, body FROM post");
$result->execute();
while ($row = $db->fetchAll(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$body = $row['body'];
}
echo $title;
echo $body;
Which keeps giving me Call to undefined method PDO::fetchAll()
Doing the example given in the manual
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
?>
Works, but I don't think I have control over the individual colums like I would with a $row=['blah']; do I? It also prints out like this; rather ugly:
Array ( [0] => Array ( [title] => This is the test title entered in the database[0]
What needs to be done to properly use PDO to do this?
change:
while ($row = $db->fetchAll(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$body = $row['body'];
}
to:
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$body = $row['body'];
}
Which keeps giving me Call to undefined method PDO::fetchAll()
This should have given you the hint, that you are using the wrong object. It's PDOStatement::fetchAll as you can see in your second example, or if you want to use it in a while loop PDOStatement::fetch:
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$body = $row['body'];
}
Additional notes:
$result is a misleading variable name as you might see from the $result->execute() line. You don't execute a result, you execute a statement. This is why in the manual $stmt or $sth (statement handle i guess) are used.
The echo lines should be inside the while loop, otherwise you overwrite again and again, then output only the last row.

Recursive PHP function is not returning a result [duplicate]

This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
Here is my function:
function loop($id) {
unset($result, $sql, $query);
$sql = " SELECT parent_id FROM page_entries WHERE id = '$id' ";
$query = mysql_query($sql) or die(mysql_error());
$result = mysql_fetch_assoc($query) or die(mysql_error());
if ($result['parent_id'] != 0) {
echo $result['parent_id'] . "... looping<br>";
loop($result['parent_id']);
} else {
echo $result['parent_id'] . "... done loop";
return $result['parent_id'];
}
}
echo loop('2');
I'm echoing the parent_id for testing. This is what is output to the browser:
1... looping
0... done loop
Where I'm not sure: the echo loop('2') doesn't echo anything from return $result['id'] if I comment out the echo lines in the function. I've tried testing by changing the return to return 'foo'; and still nothing.
How can I fix it?
At a glance, I think
loop($result['parent_id']);
should be
return loop($result['parent_id']);
otherwise your if branch is returning nothing.

Categories