Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    mysql not returning results

    so here's my problem: i am trying to list out database results but for some reason my mysql query isn't returning any results and the var_dump is returning false. i know that there is a connection being made because the rest of my script is working correctly. the part i'm concerned with is this:
    Code:
    if($threadname != "")
    	{
    		$sql = "SELECT * FROM `".$threadname."`";
    		$result = mysql_query($sql);
    		var_dump($result);
    	
    		
    		
    		$postdata = mysql_fetch_row($result);
    		echo $postdata[0];
    		echo "<br/>";
    		echo $postdata[1];
    		echo "<br/>";
    		echo $postdata[2];
    		echo "<br/>";
    		echo $postdata[3];
    		echo "<br/>";
    		echo $postdata[4];
    		echo "<br/>";
    		
    	}
    here is my entire script:
    Code:
    <?php
    
    	session_start();
    	$username = $_SESSION['username'];
    	$threadname = $_GET['threadname'];
    	$threadname = str_replace(' ', '&nbsp;', $threadname);
    	//echo $threadname;
    	$dbuser = "";
    	$dbpass = "";
    	
    	$time = date ("Y-m-d H:i:s");
    	echo $time;
    	
    
    	
    	$post = $_POST['post'];
    	$post = stripslashes($post);
    	
    	$newthreadname = $_POST['newthreadname'];
    	$newthreadname = str_replace(' ', '&nbsp;', $newthreadname);
    	$newthreadname = stripslashes($newthreadname);
    	
    	//var_dump($newthreadname);
    	//var_dump($post);
    	
    	//echo $time;
    	
    	$connect = mysql_connect(localhost, $dbuser, $dbpass);
    	mysql_select_db(forum);
    	
    	$post = mysql_real_escape_string($post);
    	$newthreadname = mysql_real_escape_string($newthreadname);
    	
    	
    
    	
    	//if the submit new thread button is pressed this code executes
    	if(array_key_exists('submit', $_POST))
    	{
    		//checking to see if both fields were filled in
    		if($post == "" || $newthreadname == "")
    		{
    		
    			echo "<center>Post and Thread Name Required</center>";
    		}
    		else
    		{		
    		
    			//thread creation
    			$sql = "CREATE TABLE `forum`.`".$newthreadname."` (`postid` INT(4) NOT NULL AUTO_INCREMENT, `threadname` VARCHAR(50) NOT NULL, `username` VARCHAR(50) NOT NULL, `timestamp` VARCHAR(25) NOT NULL, `post` TEXT NOT NULL, PRIMARY KEY (`postid`)) ENGINE = MyISAM;";
    			mysql_query($sql);
    			
    			//adding thread name to threadname list for hyperlinks above
    			$sql = "INSERT INTO `forum`.`thread_names` (`id`, `threadname`) VALUES (NULL, '".$newthreadname."');";
    			mysql_query($sql);
    			
    			
    			//insertion
    			$post = stripslashes($post);
    			$post = mysql_real_escape_string($post);
    			$sql = "INSERT INTO `forum`.`".$newthreadname."` (`postid`, `threadname`, `username`, `timestamp`, `post`) VALUES (NULL, '$newthreadname', '$username', '$time', '$post');";
    			
    			if(mysql_query($sql))
    			{
    				echo "<center>Thread Created</center>";
    				//header("location:forum.php");
    			}
    			
    		}
    		
    	}
    		
    	
    	$sql = "SELECT * FROM `thread_names`";
    	$result = mysql_query($sql);
    	$num = mysql_num_rows($result);
    	//echo $num;
    	//echo "<br/>";
    	
    	
    	//listing out hyperinks to different forums threads
    	echo "<a href='forum.php'>Home</a>";
    	echo "<br/>";
    	for($i = 0; $i < $num; $i ++)
    	{
    		$names = mysql_result($result, $i, 1);
    		$names = str_replace(' ', '&nbsp;', $names);
    		echo "<a href='forum.php?threadname=".$names."'>".$names."</a>";
    		echo "<br/>";
    	}
    	
    		
    	//if a threadname link is selected the nameis passed through get and causes this code to display the post and replys made 
    	if($threadname != "")
    	{
    		$sql = "SELECT * FROM `".$threadname."`";
    		$result = mysql_query($sql);
    		var_dump($result);
    	
    		
    		
    		$postdata = mysql_fetch_row($result);
    		echo $postdata[0];
    		echo "<br/>";
    		echo $postdata[1];
    		echo "<br/>";
    		echo $postdata[2];
    		echo "<br/>";
    		echo $postdata[3];
    		echo "<br/>";
    		echo $postdata[4];
    		echo "<br/>";
    		
    	}
    	
    	//Reply box shows up only if a threadname has been selected	
    	if($threadname != "")
    	{
    		echo '<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">';
    		echo ' <tr>';
    		echo '<td>';
    	
    		echo '<table width="500" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">';
    	
    		echo '<form name="createreply" id="createreply" method="post" action="forum.php" enctype="multipart/form-data">';
    		echo '<tr>';
    		echo '<td><table width="%100" >';
    		echo '<tr><center>Reply to this thread:</center>';
    		echo "<td>User: <input type='text' maxlength='50' width='50' name='username' id='username' value=".$username." readonly='readonly'/></td>";
    		echo "<td>Thread Name: <input type='text' name='threadname' id='threadname'  value='".$threadname."' readonly='readonly'/><input type='hidden' name='time' id='time' value=".$time." /></td>" ;
    		echo '</tr>';
    		echo '</table>';
    		echo '<table width="500" border="0" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">';
    		echo '<tr>';
    		echo '<td align="center"><textarea rows="6" cols="60" name="post" id="post"/></textarea>';
    		echo '</tr>';
    		echo '<table width="500" border="0" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">';
    		echo '<tr>';
    		echo '<td align="right"><input type="submit" name="reply" value="Reply" /></td></td>';
    		echo '</tr>';
    		echo '</table>';
    		echo '</table>';
    		echo '</td>';
    		echo '</tr>';
    		echo '</table>';
    		echo '</td>';
    		echo '</tr>';
    		echo '</table>';
    	}
    	
    ?>
    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <br/>
    <table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
      <tr>
        <td>
    
    <table width="500" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
    
    <form name="createthread" id="createthread" method="post" action="forum.php" enctype="multipart/form-data">
      <tr>
        <td><table width="%100" >
      <tr><center>Create A New Thread:</center>
        <td>User: <input type="text" maxlength="50" width="50" name="username" id="username" value="<?php echo $username; ?>" readonly="readonly"/></td>
        <td>New Thread Name: <input type="text" name="newthreadname" id="newthreadname" /><input type="hidden" name="time" id="time" value="<?php echo $time; ?>"/></td>
      </tr>
    </table>
    <table width="500" border="0" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">
      <tr>
        <td align="center"><textarea rows="6" cols="60" name="post" id="post"/></textarea>
      </tr>
      <table width="500" border="0" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">
      <tr>
        <td align="right"><input type="submit" name="submit" value="Submit New Thread" /></form)</td></td>
      </tr>
    </table>
    </table>
    </td>
      </tr>
    </table>
    </td>
      </tr>
    </table>
    </body>
    </html>
    also if anyone can explain or point me in the direction of correctly using error logs that would be a great help.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    As a rule you should always check every query you make to the database to determine if the query succeeded or failed, and if it failed, provide specific error information.

    PHP Code:
    $sql "SELECT * FROM `".$threadname."`";
    $result mysql_query($sql);
    if (!
    $result) {
        die(
    "Query error! query: $sql<br />Error: " mysql_error());


  • #3
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks for the tip it eventually helped me figure out what was going on.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •