132591 Posted November 29, 2006 Share Posted November 29, 2006 Why is this code not working? It should display the forums and their topics, but instead one of the "die()" functions executes, and I do not know why. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><style type="text/css"> body{ background: #3399FF; font-family: Arial; } td.center{ text-align: center; } a{ text-decoration: none; color: black; font-family: Arial; } a:hover{ color: white; text-decoration: underline; } a:visited{ color: purple; }</style><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /><title>REDEYEGAMEZ</title></head><body><table width="100%"> <td width="33%"></td> <td width="33%" class="center"><h1>FORUM</h1> <p>This is the redeyegamez forum. Here you can discuss different games in more detail, or just the website in general.</p></td> <td width="33%"></td></table><?php function displaytopics($forum_id, $link_target){ echo "\n<!-- In function displaytopics( '$forum_id', '$link_target' ); -->\n"; if ( ! ( $con = mysql_connect("connect""username","password") )) die( "<br>Oy vey! Failed mysql_connect( ) call in function displaytopics( ) with: " . mysql_error( ) . "<br>" ); if (!mysql_select_db("redeyegamezforum", $con)) die("<br>Failed to select database: " . mysql_error( ) . "<br>"); $sql = "SELECT * FROM topic WHERE parent_forum = '$forum_id'"; if(!$result = mysql_query($sql, $con)){ die( "<br>Error querying mySQL database.<br>"); } if ( mysql_num_rows( $result ) == 0 ) die( "<br />Query returned no results in function displaytopics( ). Goodbye!<br />" ); while($topic = mysql_fetch_assoc($result)){ echo "<tr>"; echo "<td class='topicfiller' colspan='2'>$nbsp;</td>"; echo "<td class='topiclink'>"; echo '<a href="' . $link_target . '?topicid =' . $topic['id'] . '">'; echo $topic['name'] . "</a></td>"; echo "<td class='topicdesc'>" . $topic['description'] . "</td></tr><br />"; } // end while( mysql_fetch_assoc( ) ) } // end function displaytopics( ) $con = mysql_connect("connect","username","password"); if(!$con){ die("error connnecting to forums database."); } if ( ! mysql_select_db("redeyegamezforum", $con) ) die( mysql_error( ) ); $sql = "CREATE TABLE forums( id INTEGER UNSIGNED NOT NULL auto_increment, name VARCHAR(100) NOT NULL, description TEXT NOT NULL, PRIMARY KEY(id) )"; mysql_query($sql, $con); $sql = "CREATE TABLE topic( parent_forum INTEGER UNSIGNED NOT NULL, id INTEGER UNSIGNED NOT NULL auto_increment, name VARCHAR(100), description TEXT NOT NULL, PRIMARY KEY(id) )"; mysql_query($sql, $con); $sql = "CREATE TABLE thread( parent_topic INTEGER UNSIGNED NOT NULL, id INTEGER UNSIGNED NOT NULL auto_increment, subject TEXT NOT NULL, replies INTEGER UNSIGNED, last_post TIMESTAMP, PRIMARY KEY(id) )"; mysql_query($sql, $con); $sql = "CREATE TABLE post( parent_topic INTEGER UNSIGNED NOT NULL, id INTEGER UNSIGNED NOT NULL auto_increment, author VARCHAR(100) NOT NULL, body TEXT, in_reply_to INTEGER UNSIGNED, date TIMESTAMP, PRIMARY KEY(id) )"; mysql_query($sql, $con); $sql = "SELECT * FROM forums WHERE name = 'REDEYEGAMEZ'"; if ( ! ( $result = mysql_query($sql, $con) ) ) die( "<br>" . mysql_error( ) . "<br> "); if ( ! ( $row = mysql_fetch_assoc($result) ) ) die( "<br>" . mysql_error( ) . "<br> "); if($row['name'] != "REDEYEGAMEZ"){ $sql = "INSERT INTO forums(name, description) VALUES('REDEYEGAMEZ','General talk about redeyegamez')"; mysql_query($sql, $con); } $sql = "SELECT * FROM topic WHERE name = 'Rants'"; if(!($result = mysql_query($sql, $con))) die("<br />" . mysql_error($con) . "<br />"); if (!($row = mysql_fetch_assoc($result))) die("<br />" . mysql_error($con) . "<br />"); if($row['name'] != "Rants"){ $sql = "INSERT INTO topic(parent_forum, name, description) VALUES(1, 'Rants', 'Things you don't like about our site.')"; mysql_query($sql, $con); } $sql = "SELECT * FROM forums"; if ( ! ( $result = mysql_query($sql, $con) ) ) die( "<br>" . mysql_error( ) . "<br> "); if ( mysql_num_rows( $result ) == 0 ) die( "<br>Query '" . $sql . "' returned zero rows.<br>" ); echo "<table width='100%' class='forumline' cellpadding='4' cellspacing='2' border='0' align='center'>\n"; echo "<tbody>"; while($forum = mysql_fetch_assoc($result)) { echo "\n<!-- in the while( ) loop! -->\n"; echo "\n<tr>"; echo "<td class='forumtitle' colspan='5'>"; echo $forum['name']; if ( ! isset( $forum[ 'id' ] ) ) die( "\n<!-- couldn't find ID for this forum -->\n" ); displaytopics($forum['id'], "topic.php"); }?></body></html> Link to comment Share on other sites More sharing options...
justsomeguy Posted November 29, 2006 Share Posted November 29, 2006 Start by saying which point the script dies at. Link to comment Share on other sites More sharing options...
132591 Posted November 29, 2006 Author Share Posted November 29, 2006 It dies at the point where it does: $sql = "SELECT * FROM topic WHERE name = 'Rants'"; if(!($result = mysql_query($sql, $con))) die("<br />" . mysql_error($con) . "<br />"); if (!($row = mysql_fetch_assoc($result))) die("<br />" . mysql_error($con) . "<br />"); if($row['name'] != "Rants"){ $sql = "INSERT INTO topic(parent_forum, name, description) VALUES(1, 'Rants', 'Things you don't like about our site.')"; mysql_query($sql, $con); } Link to comment Share on other sites More sharing options...
132591 Posted November 29, 2006 Author Share Posted November 29, 2006 Never mind I found the problem. The function "mysql_fetch_assoc()" will return false if now rows exist in the $result variable, and so my code was dying. Link to comment Share on other sites More sharing options...
justsomeguy Posted November 29, 2006 Share Posted November 29, 2006 Right. What I meant was that you had 13 die statements in your original code, it would have been clearer if you had indicated which one was responsible. Link to comment Share on other sites More sharing options...
132591 Posted November 30, 2006 Author Share Posted November 30, 2006 yeah, that would make sense. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now