48North Posted September 24, 2011 Share Posted September 24, 2011 Hey, Just had to delve into the world of PHP and MYSQL for the first time since A_levels :o and am a bit stuck on some script that I put together / edited. Essentially it's a very small script that will just list dates that certain courses will run (using a couple of MySQL SELECT queries). Now it was working properly up to the point where I added in that it should convert the date into a readable format as it pulls it from the database - and I now get the strange problem that in the event it returns no records it doesn't go for the IF or the ELSE at the bottom. I'm sure there are a million more elegant ways to do what I've done, but I'm trying to learn and just can't work out what's going on. The script: <?php $mysqli = new mysqli("dbaddr", "dbinfo", "..............."); if (mysqli_connect_errno()) { printf("Something's gone a bit wrong - please call us or use the contact us form: %s/n", mysqli_connect_error()); exit(); } else { $pageid = get_the_ID(); printf("The page id is".$pageid.".");/*debug only*/ $sql = "SELECT typeID FROM course_type WHERE webpageID = '".$pageid."'"; $res = mysqli_query($mysqli, $sql); $typearray = mysqli_fetch_array($res, MYSQLI_ASSOC); $typevalue = $typearray['typeID']; /*Load the type ID for the page into variable typevalue */ printf("The type id is".$typevalue."."); /*debug only*/ $sql1 = "SELECT DATE_FORMAT(startdate, '%D %M, %Y') AS newtextdate FROM course WHERE typeID = ".$typevalue." AND startdate >= CURDATE()"; /* convert MYSQ date into text date for showing on webpage */ $res1 = mysqli_query($mysqli, $sql1); var_dump ($res1); /* Just to help debug */ /* where the problems begin */ if ($res1) { /*if there is a value loaded */ while ($newArray = mysqli_fetch_array($res1, MYSQLI_ASSOC)) { $start = $newArray['newtextdate']; /*put the only record into the start variable */ echo "<h6> show if in this loop".$start."</h6>";} } else { printf("<h5>Please call us for dates</h5>"); } /*mysqli_free_result($res);*/ mysqli_close($mysqli); } ?> So as an example: 1) If $res1 has some records in it - then it outputs them line by line in h6 tags as it should - fine 2) If $res1 has no records - then it doesn't output any lines (no h6 tags at all either) AND it doesn't output "Please call us for dates". So essentially doesn't appear to fulfil either operator. I'm missing something obvious I'm sure, has anyone got any clues. I'm not asking for the straight answer as of yet but a hint or nudge would be really appreciated! Cheers Sam Quote Link to comment https://forums.phpfreaks.com/topic/247801-else-statement-not-fulfilled/ Share on other sites More sharing options...
Pikachu2000 Posted September 24, 2011 Share Posted September 24, 2011 $res1 holds either a query result resource if the query executes successfully, or a boolean FALSE if the query fails. A query that returns an empty results set has still executed successfully, so the else{} will not be reached in that case. What you need to do is use mysqli_num_rows to see how many records are returned in the if() portion of the conditional, and then use another conditional based on whether there are zero records, or more than zero records returned. Quote Link to comment https://forums.phpfreaks.com/topic/247801-else-statement-not-fulfilled/#findComment-1272474 Share on other sites More sharing options...
48North Posted September 24, 2011 Author Share Posted September 24, 2011 Thanks for the reply. Got it working now! I'm not sure if it's all correct but I can always tweak it later. For anyone interested this is now what it looks like (minus the connect at top) <?php if (mysqli_connect_errno()) { printf("Something's gone a bit wrong - please call us or use the contact us form: %s/n", mysqli_connect_error()); exit(); } else { $pageid = get_the_ID(); /*printf("The page id is".$pageid.".");debug only*/ $sql = "SELECT typeID FROM course_type WHERE webpageID = '".$pageid."'"; $res = mysqli_query($mysqli, $sql); $typearray = mysqli_fetch_array($res, MYSQLI_ASSOC); $typevalue = $typearray['typeID']; /*Load the type ID for the page into variable typevalue */ /*printf("The type id is".$typevalue."."); debug only*/ $sql1 = "SELECT DATE_FORMAT(startdate, '%D %M, %Y') AS newtextdate FROM course WHERE typeID = ".$typevalue." AND startdate >= CURDATE()"; /* convert MYSQ date into text date for showing on webpage */ $res1 = mysqli_query($mysqli, $sql1); /* where the problems begin */ if ($res1) { /*if there is a value loaded */ $rownos = mysqli_num_rows($res1); if ($rownos != 0) { while ($newArray = mysqli_fetch_array($res1, MYSQLI_ASSOC)) { $start = $newArray['newtextdate']; /*put the only record into the start variable */ echo "<h6>".$start."</h6>";} } else { printf("<h6>Please call us for dates</h6>"); } } /*mysqli_free_result($res);*/ mysqli_close($mysqli); } ?> I'm not sure how it ever worked before without counting the rows though... Quote Link to comment https://forums.phpfreaks.com/topic/247801-else-statement-not-fulfilled/#findComment-1272480 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.