$sql = "SELECT * FROM table";
$result = mysql_query($sql);
while($row = mysql_fetch_row($result)){
// PRINT COLUMN NAMES WITH EMPTY VALUE
}
how can i do this?
thanks
$sql = "SELECT * FROM table";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
foreach ($row as $columnName => $value)
{
if (empty($value))
{
echo $columnName;
}
}
}
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>";
}
}
?>
Im trying to print it in excel but I dont get it why the foreach loop give me an Warning: Illegal string offset but in while loop it just run smoothly
this is the while loop
EDITED
include 'db.php';
$sql = "SELECT * FROM customer";
$result = mysql_query($sql);
$excel = array();
while($row = mysql_fetch_array($result)){
$wew = $row["fname"]."\t".$row["lname"]."\t".$row["email"];
array_push($excel,$wew);
}
echo implode("\n",array_values($excel));
This is my foreach loop
include 'db.php';
$sql = "SELECT * FROM customer";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$excel = array();
foreach($row as $r){
$wew = $r["fname"]."\t".$r["lname"]."\t".$r["email"];
array_push($excel,$wew);
}
echo implode("\n",array_values($excel));
Im trying to understand it but couldn't find how solve this one.
Your $r is of string type and not an array so:
foreach($row as $r){
$wew = $row["fname"]."\t".$row["lname"]."\t".$row["email"];
array_push($excel,$wew);
}
OR
$wew = "";
foreach($row as $r){
$wew .= $row["fname"]."\t".$row["lname"]."\t".$row["email"];
}
array_push($excel,$wew);
In the code below I store one array in customer table. Now I want data from agent table but who related with array value.
<?php
$query = "SELECT * FROM customer";
$no1=1;
$result = mysql_query($query);
while($values = mysql_fetch_array($result))
{
$values= unserialize($values['part_no']);
foreach($values as $value)
{
echo $value."<br>";
$query2 = "SELECT * FROM agent WHERE number=$value";
$result2 = mysql_query($query2);
while($values2 = mysql_fetch_array($result2))
{
echo '' . $row['name'] . ''; // get value from agent table.
$no1++;
// echo $no1."<br>";
}
}
}
?>
Try this
<?php
$query = "SELECT * FROM customer";
$no1=1;
$result = mysql_query($query);
while($values = mysql_fetch_array($result))
{
$values= unserialize($values['part_no']);
foreach($values as $value)
{
$search_value = "'".implode("','", $value)."'";
echo $value."<br>";
$query2 = "SELECT * FROM agent WHERE number IN ($search_value)";
$result2 = mysql_query($query2);
while($values2 = mysql_fetch_array($result2))
{
echo '' . $values2['name'] . ''; // get value from agent table.
$no1++;
// echo $no1."<br>";
}
}
}
?>
Better you should use implode.
while($values = mysql_fetch_array($result))
{
$val= unserialize($values['part_no']);
$query2 = "SELECT * FROM agent WHERE number IN (".implode(',',$val).")";
$result2 = mysql_query($query2);
while($values2 = mysql_fetch_array($result2))
{
echo '' . $row['name'] . ''; // get value from agent table.
$no1++;
// echo $no1."<br>";
}
}
This is the entire code the error must be in query 3 or 4. As you can see query 3 just gets info to build query 4 which should return the results.
I've got query 1 & 2 working ok which have the correct data showing.
<?php
session_start();
//printf('<pre>%s</pre>', print_r($_SESSION, true));
require('includes/config.inc.php');
require('includes/session.php');
//WORKING
DB_Connect();
$query = "SELECT * FROM food_delivery_orders_items WHERE food_delivery_orders_items.type = 'product' AND food_delivery_orders_items.order_id=".$_SESSION['pdf_quote']['id']."";
$result = mysql_query( $query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$hash = $row['hash'];
$foreignid = $row['foreign_id'];
$qty = $row['cnt'];
}
$query2 = "SELECT * FROM food_delivery_products WHERE food_delivery_products.id = ".$foreignid."";
$result2 = mysql_query( $query2) or die(mysql_error());
while($row = mysql_fetch_array($result2))
{
$name = $row['name'];
$description = $row['description'];
}
//---------------------------------------------------------------------------------------------------------------
$query3 = "SELECT * FROM food_delivery_orders_items WHERE food_delivery_orders_items.type = 'extra' AND food_delivery_orders_items.order_id=".$_SESSION['pdf_quote']['id']."";
$result3 = mysql_query( $query3)or die(mysql_error()) ;
while($row = mysql_fetch_array($result3))
{
$foreignidextra = $row['foreign_id'];
$qtyextra = $row['cnt'];
}
$query4 = "SELECT * FROM food_delivery_extras WHERE food_delivery_extras.id = ".$foreignidextra."";
$result4 = mysql_query( $query4) or die(mysql_error());
while($row = mysql_fetch_array($result4))
{
$nameextra = $row['name'];
}
echo $nameextra;
echo $qtyextra;
DB_Disconnect();
//$products = implode(", ",$_SESSION['pdf_quote']['product_arr']);;
//print $products
?>
print $row; is only valid inside the loop if you want to print all of them. Otherwise it will print only the last $row. Make it
while($row = mysql_fetch_array($result3))
{
$foreignidextra = $row['foreign_id'];
$qtyextra = $row['cnt'];
print $row;
}
its because you are printing the $row after the while loop gets executed
So it only prints the final result set
Use something like this instead:
$query3 = "SELECT * FROM food_delivery_orders_items WHERE food_delivery_orders_items.type = 'extra' AND food_delivery_orders_items.order_id=".$_SESSION['pdf_quote']['id']."";
$result3 = mysql_query( $query3) ;
while($row = mysql_fetch_array($result3))
{
$foreignidextra = $row['foreign_id'];
$qtyextra = $row['cnt'];
print_r($row);
}
you can also use the var_dump($row); instead of print_r($row);
this will output all the values stored in the $row array each time the loop iterates
I'm currently adding fileID to idarray[] and then do a foreach. But in addition to fileID I also need serverID and id in the array and the foreach, how do I do this? So basically I need to find out how to add 2 more values to the array and then also use those extra values in the foreach.
$result = mysql_query("SELECT * FROM redundfiles WHERE removed=1");
while($row = mysql_fetch_array($result))
{
$idarray[] = $row['fileID'];
}
foreach ($idarray as $value)
{
$result = mysql_query("SELECT * FROM redundfiles WHERE fileID=$value AND removed=0 AND serverID=$XXXserverIDhereXXX");
while($row = mysql_fetch_array($result))
{
$sql = "DELETE FROM redundfiles WHERE id='".$XXXidhereXXX."' ";
mysql_query($sql) or die(mysql_error());
}
}
Why not just store the whole row into an array?
$rowarray[] = array();
$result = mysql_query("SELECT * FROM redundfiles WHERE removed=1");
while($row = mysql_fetch_array($result))
{
$rowarray[] = $row;
}
foreach ($rowarray as $row)
{
$fileid = $row['fileID'];
$serverid = $row['serverID'];
$result = mysql_query("SELECT * FROM redundfiles WHERE fileID=$fileid AND removed=0 AND serverID=$serverid");
while($row = mysql_fetch_array($result))
{
$sql = "DELETE FROM redundfiles WHERE id='".$XXXidhereXXX."' ";
mysql_query($sql) or die(mysql_error());
}
}
But really, your whole code piece here is a mess. You can accomplish this way more efficiently using JOINs.