Hey guys,

I have some code which returns the column names, but not the actual data. Here is the code I have:

$success = mysql_query($final) or die(mysql_error());
echo "<table><tr>";

foreach($_POST['fields'] as $f){
echo "<th>".$f."</th>";
}
echo "</tr>";
$i=0;
while($row = mysql_fetch_assoc($success)){
echo "<tr>";
foreach($row as $r){
echo "<td>".$r."</td>";
$i++;
}
echo "</tr>";
}

echo "</table>";

This displays the correct column names in the table, but doesn't display any results. I've checked that $final contains a correct string by echoing it so it can be seen, but nothing is shown. The only thing I can think of is if $r needs square brackets containing a column name?

Thanks in advance for any help :)

So basically try this:

$success = mysql_query($final) or die(mysql_error());
echo "<table><tr>";

foreach($_POST['fields'] as $f){
echo "<th>".$f."</th>";
}
echo "</tr>";
while($row = mysql_fetch_assoc($success)){
echo "<tr>";
echo "<td>".$row['FIELD_NAME']."</td>"; // $r['username']; as an example
}
echo "</tr>";
}

echo "</table>";

I don't understand what "i" was doing there, or the foreach statement. Since the while loop will end when it returns false (or the query is ended).

Hope this helps

Insert this code between lines 8 and 9 and post what is displayed:

echo('<pre>' . print_r($row, 1) . '</pre>');

This will display and array of values for each row. <pre> tags are just for nicer formatting. If each row is an array of expected values then your table should print correctly.

I tried both of these, but still nothing is displaying. The "i" was just there from a previous bit of code, but wasn't needed anymore. Because the field names aren't known to me, I used the foreach loop used to get the column names again and put:

while($row = mysql_fetch_array($success)){
    echo "<tr>";
    foreach($_POST['fields'] as $f){
        echo "<td>".$row[$f]."</td>";
    }
echo "</tr>";
}

is this right?

$success = mysql_query($final) or die(mysql_error());
echo "<table><tr>";
foreach($_POST['fields'] as $f){
echo "<th>".$f."</th>";
}
echo "</tr>";
$i=0;
while($row = mysql_fetch_assoc($success)){
echo "<tr>";
foreach($row as $r){
echo "<td>".$r."</td>";
$i++;
}
echo "</tr>";
}
echo "</table>";

That should work, whats the mysql query and the actual text output to the browser? it might just not be pulling any rows.

try updating

echo "<td>".$r."</td>";

to

echo "<td>".var_dump($r)."</td>";

or print out $i at the end and see if its actually gone up

while($row = mysql_fetch_array($success)){
echo "<tr>";
foreach($_POST['fields'] as $f){
echo "<td>".$row[$f]."</td>";
}
echo "</tr>";
}

is this right?

yes, make sure the $_POST values are a case sensitive match to the name of the field

Ok, I'm going to take a guess that it's something to do with being in a table format, for some reason. I tried putting the $i in it and then displaying it just before it got incremented, but that wouldn't show either.

I echoed $final too, above the table, and that came out fine, no spelling errors or change in case, so no problems with that.

I've just had a thought. The $_POST['fields'] returns the field name in the format table_name.field_name so do I need to get rid of the table name in front of it first?

If so how would I go about splitting up the string? Thanks in advance

I imagine your code is hitting a fatal error somewhere and exiting, try doing a var_dump() on key points and something might be not what you think it is, like:

error_reporting(E_ALL);
var_dump($_POST);
foreach($_POST['fields'] as $f){
echo "<td>".$f."</td>";
}
var_dump($success);
while($row = mysql_fetch_assoc($success)){
    var_dump($row);
}

If so how would I go about splitting up the string?

for that you could use strpos & substr:

$f = substr($f,strpos($f, '.'));

im not sure if that will include the . or cut the string just after, if it does just add +1:

$f = substr($f,strpos($f, '.')+1);

you could try using this i wrote for another guy recently, i actually started using it myself

<?php
error_reporting(E_ALL);
$n=0;
$sql="SELECT cusid, cuscompany from customers ORDER BY cusid ASC";
$result=mysqli_query($A['MH'],$sql) or die(mysqli_error($A['MH']));

while ($row=mysqli_fetch_assoc($result)) {
    if($n == 0){
        $htmlheaders = "<tr>\r\n";
        $htmldata = "<tr>\r\n";
        $i = 0;
        foreach($row as $k=>$v){
            if($i == 0){
                $i++;
                $headers = $k;
                $htmlheaders .= "<th>{$k}</th>\r\n";
                $data = '"'.str_replace('"',"'",$v).'"';
                $htmldata .= "<td>{$v}</td>\r\n";
            }else{
                $headers .= ",{$k}";
                $htmlheaders .= "<th>{$k}</th>\r\n";
                $data .= ',"'.str_replace('"',"'",$v).'"';
                $htmldata .= "<td>{$v}</td>\r\n";
            }
        }
        $headers .= "\r\n";
        $htmlheaders .= "</tr>\r\n";
        $data .= "\r\n";
        $htmldata .= "</tr>\r\n";
    }else{
        $htmldata .= "<tr>\r\n";
        $i = 0;
        foreach($row as $k=>$v){
            if($i == 0){
                $i++;
                $data .= '"'.str_replace('"',"'",$v).'"';
                $htmldata .= "<td>{$v}</td>\r\n";
            }else{
                $data .= ',"'.str_replace('"',"'",$v).'"';
                $htmldata .= "<td>{$v}</td>\r\n";
            }
        }
        $data .= "\r\n";
        $htmldata .= "</tr>\r\n";
    }
    $n++;
}
//$n == number of rows
echo $headers.$data;//csv
echo "<table>\r\n{$htmlheaders}{$htmldata}</table>";//html
?>

just replace $result at the start to the $result of your mysql query and it auto prints out the headers & values in csv and a html table

Thanks for your reply again Biiim. I've tried using your example, but it returned with an error about $A not being a declared variable. What is this used for? When I took that bit out it came up with an error saying that mysqli_query requires 2 parameters, only 1 given.

Thanks again for your help with this :)

I've finally managed to get it working.

Thanks again for all your help and replies, they were very much appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.