Topshed Posted February 24, 2008 Share Posted February 24, 2008 I have a problem with NULL I set up 2 variables at the start of the script to find the record I want to find. Var 1 ($modnum) is a 5 digit numeric number (Int unsigned) Var 2 ($suff) is a 2 digit Alpha code with most times is a NULL the code look line this:- $modnum = 10102; $suff = NULL; (or if $suff needs a value) $suff = 'ZX'; I then do a select using this bit of code $db = "SELECT * FROM $tble WHERE model = $modnum "; if (!$suff) { } else { $db .= "AND sufix = '$suff'"; } I believe I have a problem with setting up my variable $suff because if it is null, but there is another record with the same number AND a suff entry I end up with both records? why would an entry with AB in suff be found when I am looking for a NULL thanks Topshed Quote Link to comment Share on other sites More sharing options...
Northern Flame Posted February 24, 2008 Share Posted February 24, 2008 try $modnum = 10102; $suff = 0; (or if $suff needs a value) $suff = 'ZX'; $db = "SELECT * FROM $tble WHERE model = $modnum "; if (!empty($suff)) { $db .= "AND sufix = '$suff'"; } Quote Link to comment Share on other sites More sharing options...
Topshed Posted February 24, 2008 Author Share Posted February 24, 2008 Nope afraid it still did it modnum = 10102; $suff = 0; (or if $suff needs a value) $suff = 'ZX'; $db = "SELECT * FROM $tble WHERE model = $modnum "; if (!empty($suff)) { $db .= "AND sufix = '$suff'"; } But thanks for the suggestion Topshed Quote Link to comment Share on other sites More sharing options...
mainewoods Posted February 25, 2008 Share Posted February 25, 2008 I think I understand what you're saying, try: modnum = 10102; $suff = ''; //initialize to null string (or if $suff needs a value) $suff = 'ZX'; $db = "SELECT * FROM $tble WHERE model = $modnum AND sufix = '$suff'"; --in the case of $suff = '', the query string should end up looking something like this which should work: SELECT * FROM $tble WHERE model = 10102 AND sufix = '' Quote Link to comment Share on other sites More sharing options...
Topshed Posted February 25, 2008 Author Share Posted February 25, 2008 Think I will back up a little here Below is the code with the table and sundry bits hidden <?php $tble = 'aefe'; // next the inputs $modnum = 10103; $suff = ''; // End inputs ?> <?php nclude_once"../includes/My_conn.php"; $connect = mysqli_connect($host,$account,$password) OR DIE("Error !! Unable to connect to database"); I $db = mysqli_select_db($connect,"$dbname") OR DIE( "Unable to select database "); $db = "SELECT * FROM $tble WHERE model = $modnum "; if (!$suff) { } else { $db .= "AND sufix = '$suff'"; } if ($result = mysqli_query($connect,$db)) { if (mysqli_num_rows($result)) { while ($row = mysqli_fetch_assoc($result)){ ?> <table width='1020' align='center' border='2' cellspacing='2' > ..... ..... ..... ..... </table> <br /> <?php } } }; //removing the ";" appears to make no differance to the working ?> ?> <br /> </div> </body> </html> There are five records where $modnum = 10103; and the five $suff are = A,B,C,D and NULL If my inputs are $modnum = 10103; $suff = 'A'; // or B C D The code works fine However if my inputs are $modnum = 10103; $suff = ''; The code list's all five records so it appears the NULL is not being picked up in the line if (!$suff) { } else { $db .= "AND sufix = '$suff'"; Please someone Help Thanks Roy Quote Link to comment Share on other sites More sharing options...
freenity Posted February 26, 2008 Share Posted February 26, 2008 Wait...... What is this??? if (!$suff) { } else { $db .= "AND sufix = '$suff'"; } Should be: if ($stuff) //read if NOT null then do { $db .= "AND sufix = '$suff'"; } Thats all, the code you have check if $stuff is not null it does nothing. and then in else (NULL) it adds the suffix btw. you can make this check with sql: SELECT * FROM $tble WHERE model = $modnum AND sufix IS NOT NULL; Quote Link to comment Share on other sites More sharing options...
Demonic Posted February 26, 2008 Share Posted February 26, 2008 or u could read up on: !is_null() Quote Link to comment Share on other sites More sharing options...
Topshed Posted February 26, 2008 Author Share Posted February 26, 2008 Oh Thank you I can and pasted that piece of code a few weeks back on here, so I never even thought that it could be the culprit. Ah well I have learned a little more Kind Regards Roy... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.