...

View Full Version : mysql not returning results



riceje7
09-04-2009, 02:48 PM
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:
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:
<?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.

Fumigator
09-04-2009, 03:16 PM
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.



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

riceje7
09-04-2009, 05:00 PM
thanks for the tip it eventually helped me figure out what was going on.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum