Jump to content

query output to php variables


stmosaic

Recommended Posts

I know this is probably so easy I can't see it, but I'm going from PHP 5 to 7 and having to modify my existing pages. Been working on this for 2 days and am hopelessly confused. I used to put the query results into the PHP variables like $IDENT & $NAM to be used further down in my while loop. $dbcnx is the db connection that works and $sql is the SELECT statement further up the code and it works. No errors, but no data either. Now, I'm not sure how to do that without getting horridly complicated. This needs to be as simple and straight forward as possible, due to my health.

///===DISPLAY ALL LISTINGS===///
////
$result2 = mysqli_query($dbcnx,$sql);
$i=0;
while ($row = $result2->fetch())
{
$IDENT=$row["ID"];
$NAM=$row['dcategory'];
//the variables above are used later in the table to display query results.
//===ALTERNATING ROW COLORS===//
$i++;
if ($i % 2)
{
$bgc="FFCC00";
}
else
{
$bgc="FFFF00";
}
//===END ALTERNATING ROW COLORS===//

//===DISPLAY ALL LISTINGS===//
echo"

<tr bgcolor=\"#$bgc\">
    <td width=\"9%\">
    <div align=\"center\"><input type=\"text\" size=\"3\" maxlength=\"3\" value=\"$IDENT\">
    <input type=\"hidden\" name=\"Ident[]\" value=\"$IDENT\"></div>
    </td>
          
    <td width=\"45%\">
    <div align=\"center\"><font face=\"Arial, Helvetica, sans-serif\" size=2>$NAM</font></div>
    </td>
    
    <td width=\"13%\"> <div align=\"center\"><font face=\"Arial, Helvetica, sans-serif\" size=2>
    <a href=\"CAT_edit_form.php?ID=$IDENT\"><img src=\"$SCIMAGEPATH/modify.gif\" border=0></a></font></div>
    </td>
    <td width=\"16%\"><div align=\"center\"><font face=\"Arial, Helvetica, sans-serif\" size=2><a
href=\"CAT_delete.php?DEL=Y&ID=$IDENT\"><img src=\"$SCIMAGEPATH/delete.gif\"
border=\"0\"></a></font></div>
    </td>
</tr>

";
}
Link to comment
Share on other sites

Try this

while ($row = $result2->fetch_assoc())

 

Plus, you need to escape the content to prevent XSS and general display issues with certain content

    $IDENT = htmlentities($row["ID"], ENT_QUOTES);
    $NAM = htmlentities($row['dcategory'], ENT_QUOTES);

 

Also, replace the alternating color code with just this

//===ALTERNATING ROW COLORS===//
$i++;
$bgc = ($i % 2) ? 'FFCC00' : 'FFFF00';
//===END ALTERNATING ROW COLORS===//

 

Plus, get rid of the font tags and use proper CSS.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.