sql doesn't wants to leave empty fields in a table empty when i call echo in PHP
Whenever i call a SQL function it fills out everything but empty fields accordingly to id number where id number says which record of name, accoun开发者_如何学编程t it takes. SQL instead fills the empty fields with data from previous id record. How to tell SQL to not fill the empty fields and leave them empty as they are ?
Ok, let's try it the other way round...
Here's an example that doesn't exhibit the behavior you've described
<?php
$pdo = new PDO("mysql:host=localhost;dbname=test", 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('CREATE TEMPORARY TABLE soTest (id int auto_increment, x varchar(16) NOT NULL, y varchar(16) DEFAULT NULL, primary key(id))');
$pdo->exec("INSERT INTO soTest (x,y) VALUES (1, NULL), (2,'foo'), (3, NULL), (4, 'bar')");
echo "test 1: \n";
foreach( $pdo->query('SELECT id,x,y FROM soTest', PDO::FETCH_ASSOC) as $row) {
$y = is_null($row['y']) ? '--null--' : $row['y'];
printf(" %s %s %s\n", $row['id'], $row['x'], $y);
}
echo "test 2: \n";
foreach( $pdo->query('SELECT id,x,y FROM soTest WHERE id IN (1,3)', PDO::FETCH_ASSOC) as $row) {
$y = is_null($row['y']) ? '--null--' : $row['y'];
printf(" %s %s %s\n", $row['id'], $row['x'], $y);
}
prints
test 1:
1 1 --null--
2 2 foo
3 3 --null--
4 4 bar
test 2:
1 1 --null--
3 3 --null--
Your code does something differently. What is it?
精彩评论