$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'];
}
Related
Usually when you do the basic php excercises you just use the following to print data on a blank page:
<?php
include('../includes/dbh.php');
$titulo = mysqli_real_escape_string($conn,$_GET['titulo']);
$sql = "SELECT * FROM proyectos WHERE proyect_name='$titulo'";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
if($queryResults > 0){
while ($row = mysqli_fetch_assoc($result)){
echo "<div>".$row['proyect_name']. "
</div>";
}
}
?>
My question is, if I try to separate and echo out $row proyect_name in another part of the webpage nothing happens. I used both echo and var_dump and yes, the query works but it just doesn't print anything. I am new to this so yes, it may be obvious to you but not to me. I think it may be the while loop, but I do not know how to "blend it in".
<?php
include('../includes/dbh.php');
$titulo = mysqli_real_escape_string($conn,$_GET['titulo']);
$sql = "SELECT * FROM proyectos WHERE proyect_name='$titulo'";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
$data=array();
if($queryResults > 0){
while ($row = mysqli_fetch_assoc($result)){
echo "<div>".$row['proyect_name']. "
</div>";
$data[] = $row;
}
}
foreach($data as value) {
echo $value;
}
// OR
foreach($data as $key => $value) {
echo $key . " - " . $value;
}
?>
Set variable before loop then add $row data to it then loop it out with a foreach loop Does this work??
Use mysqli_fetch_array() - Example
So in your case it would be
<?php
include('../includes/dbh.php');
$titulo = mysqli_real_escape_string($conn,$_GET['titulo']);
$sql = "SELECT * FROM proyectos WHERE proyect_name='$titulo'";
$result = mysqli_query($conn, $sql);
$queryResults = mysqli_num_rows($result);
if($queryResults > 0){
while ($row = $result->fetch_array()){
$rows[] = $row;
}
foreach ($rows as $row) {
echo "<div>".$row['proyect_name']."</div>";
}
}
?>
I have this code...
$results_query = "SELECT * FROM profile WHERE string LIKE '%" . $search_deliminator . "%'";
$results_result = $database->query($results_query);
$result = mysql_fetch_array($results_result);
What I'm trying to do is pull all the rows in which the string column contains my search deliminator. Further, I would like to get the values from each column of the rows.
How would I pull the multidimensional array I need containing each row, and each value of each column within each row?
EDIT:
I'm looking to do something to this effect...
while ($row = mysql_fetch_assoc($results_result)) {
$result[] = $row;
}
Then echo each column like this...
foreach ($result as $row) {
echo $row["0"];
}
To get a flattened array of the result set, try this:
$result = array();
while ($row = mysql_fetch_assoc($results_result)) {
$result[$row['id']] = $row;
}
echo json_encode($result);
Also, you should look into either MySQLi or PDO to replace the (now deprecated) MySQL extension.
If you use PDO, you can use the fetchAll() method:
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
With mysql or mysqli, you use a loop:
$rows = array();
while ($row = $stmt->fetch_assoc()) {
$rows[] = $row;
}
try this
$i=0;
while ($row = mysql_fetch_assoc($results_result)) {
$result[$i]["column1"] = $row["column1"];
$result[$i]["column2"] = $row["column2"];
$i++;
}
To display the output use:
foreach ($result as $row) {
echo $row["column1"];
echo $row["column2"];
}
I currently have a query which fetches data and then runs another query. I then echo out the results of the second query. What I would like to do is to move the echo outside of the foreach loop please but I don't know how to do that.
$ids = 2,3;
$id = explode(",",$ids);
$barcode = array();
foreach($id as $value) {
$sql_query = $db->prepare("SELECT barcode FROM product WHERE id=:value");
$sql_query->bindParam(":value", $value);
$sql_query->execute();
$row = $sql_query->fetch(PDO::FETCH_ASSOC);
$barcode = $row['barcode'];
/* I want to move this part starting from here outside the foreach */
$sql_select_all = $db->prepare("SELECT * FROM inventory WHERE barcode=:barcode");
$sql_select_all->bindParam(":barcode", $barcode);
$sql_select_all->execute();
while($row = $sql_select_all->(PDO::FETCH_ASSOC)){
$name = $row['name'];
$img = $row['image'];
$desc = $row['desc'];
echo $name.$img.$desc;
}
/* ending here */
}
How can I move the echo line out of the foreach loop?
try
foreach($id as $value){
$sql_query = $db->prepare("SELECT barcode FROM product WHERE id=:value");
$sql_query->bindParam(":value", $value);
$sql_query->execute();
$row = $sql_query->fetch(PDO::FETCH_ASSOC);
// make array with the entry
$barcode[] = '"'.$row['barcode'].'"';
}
// after foreach we explode the array and add comma ,
$barcode = explode(',',$barcode)
// using mysql ( [IN][1] ) query instead of ( = )
$sql_select_all = $db->prepare("SELECT * FROM inventory WHERE barcode in ($barcode)'");
$sql_select_all->bindParam(":barcode", $barcode);
$sql_select_all->execute();
while($row = $sql_select_all->(PDO::FETCH_ASSOC)){
$name = $row['name'];
$img = $row['image'];
$desc = $row['desc'];
echo $name.$img.$desc;
using IN() Function
What you need to do is store the results of your second query into an array. Then you can move the echo out of the for loop and run a loop on your new array like this:
$ids = 2,3;
$id = explode(",",$ids);
$barcode = $results = array();
foreach($id as $value){
$sql_query = $db->prepare("SELECT barcode FROM product WHERE id=:value");
$sql_query->bindParam(":value", $value);
$sql_query->execute();
$row = $sql_query->fetch(PDO::FETCH_ASSOC);
$barcode = $row['barcode'];
/*i want to move this part starting from here outside the foreach*/
$sql_select_all = $db->prepare("SELECT * FROM inventory WHERE barcode=:barcode");
$sql_select_all->bindParam(":barcode", $barcode);
$sql_select_all->execute();
while($row = $sql_select_all->(PDO::FETCH_ASSOC)){
$results[] = $row;
//$name = $row['name'];
//$img = $row['image'];
//$desc = $row['desc'];
//echo $name.$img.$desc;
}
/*ending here*/
foreach($results as $row) {
echo $row['name'] . $row['image'] . $row['desc'];
}
}
I have the following code:
<?php
if(isset($_POST['indexSearchSubmit']))
{
foreach($_POST['industryList'] as $selected)
{
$_POST['industryList'] = $selected;
$locationListResults = $_POST['locationList'];
$results = mysqli_query($con,"SELECT * FROM currentListings WHERE location = '$locationListResults' AND industry = '$selected'");
while($row = mysqli_fetch_array($results))
{
echo $row['industry'];
echo $row['location'];
echo $row['title'];
echo $row['description'];
}
}
mysqli_close($con);
}
?>
Could anyone tell me how I would go about storing the echo part into a variable so I can then display it as and where I want in other parts of the site?
If I remove the echo and instead store $row as a variable when I echo that variable it only outputs once and doesn't run the loop.
You should use mysqli_fetch_all for this. The result will be an array where each element is a row and each row is an associative array with the same keys as row in your example
$data = mysqli_fetch_all($result);
$data[0]["industry"]; //Data in the first row
You can then loop over $data to output it any place on your page.
put in array,
$list = array();
while($row = mysqli_fetch_array($results))
{
$list[] = $row;
}
you may try this
$arrayList = array();
while($row = mysqli_featch_array($results)){
$arrayList[] = $row;
}
print_r($arrayList);
Put all the values in an array
$rows = array();
$x = 0;
while($row = mysqli_fetch_array($results))
{
$rows[$x]['industry'] = $row['industry'];
$rows[$x]['location'] = $row['location'];
$rows[$x]['title'] = $row['title'];
$rows[$x]['description'] = $row['description'];
$x++;
}
return $rows;
Then you can use $rows as an array.
foreach($rows as $v){
echo $v['industry']." ".$v['location']."<br />";
echo $v['title']." ".$v['description']."<br />";
}
I have a query which could return multiple rows.
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
$data[] = $row;
foreach ($data as $row){
echo $row['...'];
}
}
The problem is that it only prints one row, while i should have 2 rows printed.
Could someone help me out?
The problem is probably that you're re-using the $row variable. Also, the logic is a bit strange: why are you looping through $data every time you fetch a new row? Did you mean to do this instead:
while ($row = mysql_fetch_array($result)){
$data[] = $row;
}
foreach ($data as $row){
echo $row['...'];
}
Try something like:
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
foreach ($row as $k=>$v){
echo $k ." = " . $v;
}
}
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
$data[] = $row;
foreach ($data as $col){
echo $col['...'];
}
echo '<br>';
}
OR
echo '<table>';
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
$data[] = $row;
echo '<tr>';
foreach ($data as $col){
echo '<td>';
echo $col['...'];
echo '</td>';
}
echo '</tr>';
}
echo '</table'>;
Maybe the problem is that you iterate the $data array in side the while loop, I think you want to collect all the rows in the $data, and only when while breaks to iterate it to show the results.
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
$data[] = $row;
}
foreach ($data as $row){
echo $row['...'];
}
Or did I misunderstood?
Do some changes in your code for check no. of results
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
foreach ($row as $val){
echo $val;
}
}