Having issues handling null field from mysql database table. I haven’t been successfull with Google
search and implementation so i throug i try this avenue.
Even tho theres a null field in the record i still like to pull the record as I can substitue the null
field with somethihng else later with PHP.
Basically what im looking for is a way to implement a if statement of some sort to accomplish this task.
if null still querie the record instead of hanging.
try{
$STH = $this->DBH->prepare("SELECT tbl_newproject.prj_name,
tbl_version.ver_version AS 'Version'
FROM tbl_newproject, tbl_version
where tbl_newproject.prj_index = $projectID AND
tbl_version.ver_prjIndex = tbl_newproject.inc_version
");
$STH->execute();
$results1 = $STH->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) { echo 'table error : ' . $e->getMessage(); }
You will need to substitute something else for the NULL in the SQL as PHP has no concept of NULL and soneeds it converted to something else in order to be able to store a value in the first place.
A PHP null value is something completely different with a completely different meaning - about the same amount of difference as between son and sun…
so, basically
if tbl_newproject.inc_version is null then do the following:
tbl_version.ver_prjIndex = tbl_newproject.inc_version
else dont bother wtih tbl_version.ver_prjIndex = tbl_newproject.inc_version but continue pulling information for tbl_newproject.prj_name and other fields.
after having done some research i came up with something like tis but im still missing something parheaps a return value of some sort, not sure…
( tbl_newproject.inc_version IS NOT NULL or tbl_version.ver_index = tbl_newproject.inc_version.inc_version)
A PHP null value is pretty similar to SQL null value, meaning “no value is set”.
No special action is required in PHP to handle database nulls, as long as prepared statement for the input values is used: in this case PHP null will be translated into SQL null automatically.
When selecting data, a null from database is always translated into PHP null.
To solve your problem you have to use LEFT JOIN. This kind of join is used to let the left-handed table return all the rows, no matter whether there is a counterpart in the right-handed table or not.