Jump to content


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


NULL value in specific column results in html field duplication when extracting mysql record set

Recommended Posts

I have the following general mySQL query:

$query = sprintf(
WHERE a.A = %s
ORDER by a.A;",
implode(" OR a.A = ", $_SESSION['values']));  

for some records, B has a value, and for others, B is NULL. That is what I want.

I extract the query with the following PHP:

for ($i = 0; $i < $nrows; $i++){
$row = mysqli_fetch_assoc($result);//fetches data stored within each row
echo "<tr>";
foreach($row as $column => $field){
if($field == $...){
elseif($field == $C){
<input type='text' name='C+[]' value='$C'>
echo "</tr>"

In the resulting html table, records containing not null B fields are presented accurately, while records with null B fields incur a duplication of field C. This offset occurs at the beginning of the record, pushing the final field in the record outside the table boundaries.

I think I've narrowed down the problem to the extract() function, as r_print readouts of every other variable in the script returns the accurate field names and values.

But, running print_r on $row after extract() provides an identical printout to other variables in the script.

What are some possible ways I can stop the duplication of field C from occurring? Happy to provide more information upon request.

Share this post

Link to post
Share on other sites

I've also tried using


 in place of 


in the SQL query, but the output remains the same.

Share this post

Link to post
Share on other sites

It was also suggested to me that I should refrain from using extract($row), as it creates unnecessary duplicate variables, but I am uncertain of another way to acess $result in order to format the html table. Also, the use of extract() only seems contentious when using it with $_POST or $_GET methods, a process that I am not following.


I am happy to try an alternative, but would like some suggestions as to what could work first.

Share this post

Link to post
Share on other sites

You have the query results that come to you in array format. Why do you need to use extract() on that? Simply loop thru as you are doing and generate your html table rows. I fail to see a problem.


while($row = mysqli_fetch_assoc($result))
echo "<tr>";
foreach ($row as $k=>$v)
echo "<td>$v</td>";
echo ("</tr>";


will output all the elements from your query results in table rows.

Share this post

Link to post
Share on other sites

  • 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.