Scenario:
Database Table
Column Type Length Sample Values
emp_code varchar 4 0011 0021 0012
emp_desc varchar 2 01 03 01
emp_stat varchar 1 1 0
Problem:
I am querying so that I can write the result into a text file.
$file = "data.txt";
$f = fopen($file, "a");
$qry = "SELECT * FROM tbl_emp";
$result = mysqli_query($conn, $qry );
if(!$result) {
echo "Cannot do query" . "<br/>";
exit;
}
$num_results =mysqli_num_rows($result);
for ($i=0; $i<=$num_results; $i++) {
$row = mysqli_fetch_assoc ($result);
$emp_code = (empty($row['emp_code']) ? " " : $row['emp_code']); //replace with 4 spaces
$emp_desc = (empty($row['emp_desc']) ? " " : $row['emp_desc ']); //replace with 2 spaces
$emp_stat = (empty($row['emp_code']) ? " " : $row['emp_code']); //replace with 1 space
$data = "$emp_code $emp_desc $emp_stat
";
fwrite($f,$data);
}
Data.Txt should contain:
0011011
002103
0012010
But this is the result:
0011011
002103
001201
If the value of the row is 0, it replaces it with blank when it shouldn't. What needs to be changed to get the correct data? TIA.