Jump to content

[SOLVED] Having Trouble with Conditional Statement


Talashira

Recommended Posts

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? :confused:

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.