Talashira Posted October 26, 2009 Share Posted October 26, 2009 I'm a PHP newbie, and I'm having trouble putting a conditional statement beneath the "while($array = mysql_fetch_array($sql))" line. Here's the working script: <?php $dbhost = "localhost"; $dbuser = "username"; //Censored for this forum. $dbpass = "********"; //Censored for this forum. $dbname = "dbName"; //Censored for this forum. $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); if(isset($_GET['query'])) { $query = $_GET['query']; } else { $query = ""; } if(isset($_GET['type'])) { $type = $_GET['type']; } else { $query = "count"; } if($type == "count") { $sql = mysql_query("SELECT count(title) FROM books WHERE MATCH(author_last, author_first, series, number, title, summary, number, genre, filename) AGAINST('$query' IN BOOLEAN MODE)"); $total = mysql_fetch_array($sql); $num = $total[0]; echo $num; } if($type == "results") { $sql = mysql_query("SELECT author_last, author_first, series, number, title, summary, genre, filename FROM books WHERE MATCH(author_last, author_first, series, number, title, summary, genre) AGAINST('$query' IN BOOLEAN MODE)"); while($array = mysql_fetch_array($sql)) { $book_author_last = $array['author_last']; $book_author_first = $array['author_first']; $book_series = $array['series']; $book_number = $array['number']; $book_title = $array['title']; $book_summary = $array['summary']; $book_genre = $array['genre']; $book_length = $array['length']; $book_filename = $array['filename']; echo "<div class=\"result\"><div class=\"resultCover\"><a href=\"content/" . $book_filename . ".pdf\" target=\"_blank\"><img src=\"covers/" . $book_filename . ".jpg\" border=\"0\" align=\"left\"></a></div><p><strong>Title</strong><br/><a href=\"content/" . $book_filename . ".pdf\" target=\"_blank\">" . $book_title . "</a><br/><br/><strong>Author</strong><br/>" . $book_author_first . " " . $book_author_last . "<br/><br/><strong>Genre</strong><br/>" . $book_genre . "<br/><br/><strong>Summary</strong><br/>" . $book_summary . "</p></div>"; } } mysql_close($conn); ?> ...and here's what I'd like to do with that bottom area: while($array = mysql_fetch_array($sql)) { $book_author_last = $array['author_last']; $book_author_first = $array['author_first']; $book_series = $array['series']; $book_number = $array['number']; $book_title = $array['title']; $book_summary = $array['summary']; $book_genre = $array['genre']; $book_length = $array['length']; $book_filename = $array['filename']; if($book_number != "") { echo "<div class=\"result\"><div class=\"resultCover\"><a href=\"content/" . $book_filename . ".pdf\" target=\"_blank\"><img src=\"covers/" . $book_filename . ".jpg\" border=\"0\" align=\"left\"></a></div><p><strong>Title</strong><br/><a href=\"content/" . $book_filename . ".pdf\" target=\"_blank\">" . $book_title . "</a> (Book " . $book_number . " in the " . $book_series . " Series)<br/><br/><strong>Author</strong><br/>" . $book_author_first . " " . $book_author_last . "<br/><br/><strong>Genre</strong><br/>" . $book_genre . "<br/><br/><strong>Summary</strong><br/>" . $book_summary . "</p></div>"; } else { echo "<div class=\"result\"><div class=\"resultCover\"><a href=\"content/" . $book_filename . ".pdf\" target=\"_blank\"><img src=\"covers/" . $book_filename . ".jpg\" border=\"0\" align=\"left\"></a></div><p><strong>Title</strong><br/><a href=\"content/" . $book_filename . ".pdf\" target=\"_blank\">" . $book_title . "</a><br/><br/><strong>Author</strong><br/>" . $book_author_first . " " . $book_author_last . "<br/><br/><strong>Genre</strong><br/>" . $book_genre . "<br/><br/><strong>Summary</strong><br/>" . $book_summary . "</p></div>"; } } } mysql_close($conn); I just want it to echo a different block of text when the $book_number is NULL. I get the following error when I try that: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in search.php on line 20 Thoughts? Suggestions? Help? Link to comment https://forums.phpfreaks.com/topic/179073-solved-having-trouble-with-conditional-statement/ Share on other sites More sharing options...
ashton321 Posted October 26, 2009 Share Posted October 26, 2009 try if($book_number == null){ output } Link to comment https://forums.phpfreaks.com/topic/179073-solved-having-trouble-with-conditional-statement/#findComment-945071 Share on other sites More sharing options...
Talashira Posted October 27, 2009 Author Share Posted October 27, 2009 That worked perfectly, ashton321! Thank you so much! Link to comment https://forums.phpfreaks.com/topic/179073-solved-having-trouble-with-conditional-statement/#findComment-945550 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.