В PHP, SQLITE, у меня есть следующий код:
public function queryRecord_GetRecord_M2ID_M3ID_All($M4_M2,$M4_M3)
{
try
{
$sql = "SELECT * FROM M4 "
. "WHERE M4_M2 = :M4_M2 AND M4_M3 = :M4_M3 ";
$stmt = $this->pdo->prepare($sql);
$stmt->bindValue(':M4_M2', $M4_M2);
$stmt->bindValue(':M4_M3', $M4_M3);
$stmt->execute();
$mRecords = [];
while($row = $stmt->fetch(\PDO::FETCH_ASSOC))
{
echo "\n row['M4_Image1'] ======> {$row['M4_Image1']}\n";
if ($row['M4_Image1'] === NULL)
{
$row['M4_Image1'] = "";
}
$mRecords[] = [
'M4_M2' => $row['M4_M2'],
'M4_M3' => $row['M4_M3'],
'M4_Barcode' => $row['M4_Barcode'],
'M4_Enable' => $row['M4_Enable'],
'M4_Name' => $row['M4_Name'],
'M4_Price' => $row['M4_Price'],
'M4_Discount' => $row['M4_Discount'],
'M4_Image1' => $row['M4_Image1'],
'M4_Image2' => $row['M4_Image2'],
'M4_Image3' => $row['M4_Image3'],
'M4_Image4' => $row['M4_Image4'],
'M4_Image5' => $row['M4_Image5'],
'M4_Image6' => $row['M4_Image6'],
'M4_Image7' => $row['M4_Image7'],
'M4_Image8' => $row['M4_Image8'],
'M4_Image9' => $row['M4_Image9'],
'M4_Image10' => $row['M4_Image10'],
'M4_A' => $row['M4_A'],
'M4_B' => $row['M4_B'],
'M4_C' => $row['M4_C'],
'M4_D' => $row['M4_D'],
'M4_E' => $row['M4_E'],
'M4_F' => $row['M4_F'],
'M4_G' => $row['M4_G'],
'M4_H' => $row['M4_H'],
'M4_I' => $row['M4_I'],
'M4_J' => $row['M4_J']
];
}
return $mRecords;
}
catch (\PDOException $e)
{
return FALSE;
}
}
если столбец M4_Image1 пуст, возвращается null, поэтому я написал следующий код:
if ($row['M4_Image1'] === NULL)
{
$row['M4_Image1'] = "";
}
Если я не напишу этот код, у меня будет «нулевая» строка для части M4_Image1 в $ mRecords
Как я могу использовать «» вместо null, когда столбец M4_Image1 пуст?
В таким образом мне не нужно писать код, чтобы скрыть null в ""
Изменить:
I added the database create too :
class SQLiteCreateDatabase
{
private $pdo;
public function __construct($pdo) {
$this->pdo = $pdo;
}
public function createTable() {
try
{
$commands = [
'CREATE TABLE IF NOT EXISTS M4 (
M4_M2 TEXT,
M4_M3 TEXT,
M4_Barcode TEXT,
M4_Enable TEXT,
M4_Name TEXT,
M4_Price TEXT,
M4_Discount TEXT,
M4_Image1 TEXT,
M4_Image2 TEXT,
M4_Image3 TEXT,
M4_Image4 TEXT,
M4_Image5 TEXT,
M4_Image6 TEXT,
M4_Image7 TEXT,
M4_Image8 TEXT,
M4_Image9 TEXT,
M4_Image10 TEXT,
M4_A TEXT,
M4_B TEXT,
M4_C TEXT,
M4_D TEXT,
M4_E TEXT,
M4_F TEXT,
M4_G TEXT,
M4_H TEXT,
M4_I TEXT,
M4_J TEXT
)'//,
//'CREATE UNIQUE INDEX idx_positions_title ON M4 (M4_M2,M4_M3,M4_Barcode)'
];
foreach ($commands as $command) {
$this->pdo->exec($command);
}
return TRUE;
}
catch (\PDOException $e)
{
return FALSE;
}
}
}