Jump to content

Simple PHP MySQL SELECT not working


Lambneck

Recommended Posts

Not really sure what to ask because I don't know what the problem is. Maybe just need a fresh set of eyes to find out whats wrong.

I am trying selecting content from a database table but its not working.  I am not getting any errors just a blank screen where the content should be displayed.

 

html

<table>
			<tbody>
				<tr>
					<th>Topic</th>
					<th>Name</th>
					<th>Date</th>
		  		</tr>
		  		<?php include 'server/forum.php'; ?>
			</tbody>
		</table>

 

php

<?php

require_once('load_data.php');

$con = mysql_connect($db_host, $db_user, $db_pwd);

if (!$con)
{
die('Could not connect to database: ' . mysql_error());
}

$dbcon = mysql_select_db($database);

if (!$dbcon)
{
  die('Could not select database: ' . mysql_error());
}

$sql = "SELECT * FROM $table ORDER BY id";
$result = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql);

if (!$result) 
{
die("Query to show fields from table failed:".mysql_error());
}

$row = mysql_fetch_array($result)

while($row)
{
echo '<tr>';    
echo '<td class="forumtd"><b>';
echo '<a href="topic.php?id='.$row['id'].'">'.stripslashes(htmlspecialchars($row['topic'])).'</a>';
echo "</b></td>";
echo '<td class="forumtd"><em>';
echo stripslashes(htmlspecialchars($row['name']));
echo "</em></td>";
echo '<td class="forumtd">';
echo date("l M dS, Y", $row['date']);
echo "</td>";
echo "</tr>";	
}

mysql_close($con);

?>

Link to comment
Share on other sites

I think you're getting your code stuck in an infinite loop because this:

while($row)

will ALWAYS be true since you assigned it outside the loop (unless mysql_fetch_array() fails of course).

 

I think what you meant to do is this:

<?php

require_once('load_data.php');

$con = mysql_connect($db_host, $db_user, $db_pwd);

if (!$con)
{
die('Could not connect to database: ' . mysql_error());
}

$dbcon = mysql_select_db($database);

if (!$dbcon)
{
  die('Could not select database: ' . mysql_error());
}

$sql = "SELECT * FROM $table ORDER BY id";
$result = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql);

if (!$result) 
{
die("Query to show fields from table failed:".mysql_error());
}

while($row = mysql_fetch_array($result)) //while we get a valid row from mysql_fetch_array()
{
echo '<tr>';    
echo '<td class="forumtd"><b>';
echo '<a href="topic.php?id='.$row['id'].'">'.stripslashes(htmlspecialchars($row['topic'])).'</a>';
echo "</b></td>";
echo '<td class="forumtd"><em>';
echo stripslashes(htmlspecialchars($row['name']));
echo "</em></td>";
echo '<td class="forumtd">';
echo date("l M dS, Y", $row['date']);
echo "</td>";
echo "</tr>";	
}

mysql_close($con);

?>

Just another unrelated tip, you're checking for errors with SQL queries twice in some places, like here:

	$result = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql);

if (!$result) 
{
die("Query to show fields from table failed:".mysql_error());
}

If you put or die(blah); after a mysql_query(), the script will just exit with that message if it fails, so your second check ( if(!$result) ) is useless.

Link to comment
Share on other sites

Hmm, well, mysql should be giving an error if it can't find the table and I don't know what $table is supposed to be, but just to make sure, echo $sql somewhere so you can see what your query actually is. Also make sure you have rows in that table to output.

 

Add this to the top of the script just for good measure:

error_reporting(E_ALL);
ini_set('display_errors', 1);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.