Jump to content
CSnovice

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(
"SELECT a.A,
a.B,
...,
a.C,
FROM a
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
extract($row);
echo "<tr>";
foreach($row as $column => $field){
if($field == $...){
...
}
elseif($field == $C){
echo"<td>
<input type='text' name='C+[]' value='$C'>
</td>";
}
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

GROUP BY 

 in place of 

ORDER BY

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

Edited by CSnovice

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


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