Jump to content

Quick PHP Question


Shaun13

Recommended Posts

Hi, this is a snipet from my newest project ShaunsBoards. It is some simple forum software.

 

<? 
$tbl_name2="members";
$myusername=$_SESSION['myusername'];
$mydn="SELECT displayname FROM $tbl_name2 WHERE username = '$myusername'";
$mydisplayname=mysql_query($mydn);

if(session_is_registered(myusername)){
echo "Welcome ".$mydisplayname." | <a href='logout.php'>Logout</a>";
}
else
{
echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
}
?>

 

This is a small snipet from my forums index. The $myusername variable is taken from the session cookie and that is in the mysql database. What I want it to do is get the persons username ($myusername) in the mysql table and get their display name which is also in that table. Now, when I leave it as "welcome.$myusername" it works fine, but right now I get this:

 

Welcome Resource id #4 | Logout

 

What am I doing wrong?

 

~Shaun

Link to comment
Share on other sites

first of all... please wait more than 5 minutes before bumping the topic..

 

now to the question...

 

you could try using

"Welcome $mydisplayname | <a href=\"logout.php\">Logout</a>";

 

or

'Welcome '.$mydisplayname.' | <a href="logout.php">Logout</a>';

 

maybe it will work...

Link to comment
Share on other sites

yes... i see what you mean now...

 

$myusername get the value of the username session...

 

but the $mydisplayname I'm not sure why it's not working... but that might be because I've never used that way to read from a DB yet :D sorry, but I'm gonna have to hand this over to someone else..

 

Good luck

Link to comment
Share on other sites

Try this:

 

<?php
$tbl_name2="members";
$myusername=$_SESSION['myusername'];
$mydn="SELECT displayname FROM $tbl_name2 WHERE username = '$myusername'";
$mydisplayname=mysql_query($mydn);
if (mysql_num_rows($mydisplayname)>0) {
   while ($row = mysql_fetch_assoc($result)) {
      extract($row);
      if(session_is_registered(myusername)){
         echo "Welcome ".$mydisplayname." | <a href='logout.php'>Logout</a>";
      }
      else
      {
         echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
      }
   }
}
?>

Link to comment
Share on other sites

Nope, didn't work either. I got this:

 

Welcome Resource id #4 | LogoutWelcome Resource id #4 | Logout

 

Here is the PHP of the whole page:

 

<?php
session_start();
$host="localhost"; // Host name 
$username="=P"; // Mysql username 
$password="=P"; // Mysql password 
$db_name="=P"; // Database name 
$tbl_name="posts"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending 
$result=mysql_query($sql);
?>
<center>
<?php
$tbl_name2="members";
$myusername=$_SESSION['myusername'];
$mydn="SELECT displayname FROM $tbl_name2 WHERE username = '$myusername'";
$mydisplayname=mysql_query($mydn);
if (mysql_num_rows($mydisplayname)>0) {
   while ($row = mysql_fetch_assoc($result)) {
      extract($row);
      if(session_is_registered(myusername)){
         echo "Welcome ".$mydisplayname." | <a href='logout.php'>Logout</a>";
      }
      else
      {
         echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
      }
   }
}
?>
</center>
<br /><br />
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="40%" align="center" bgcolor="#CCDDCC"><strong>Topic</strong></td>
<td width="20%" align="center" bgcolor="#CCDDCC"><strong>Author</strong></td>
<td width="15%" align="center" bgcolor="#CCDDCC"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Date/Time</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row 
?>
<tr>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['title']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['author']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['views']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['posts']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>

<?php
// Exit looping and close connection 
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#CCDDCC">
<? 
if(session_is_registered(myusername)){
echo "<a href='create_topic.php'><strong>Create New Topic</strong> </a>";
}
else
{
echo "<a href='login.php'>Login</a> to start topics!";
}
?>
</tr>
</table>

Link to comment
Share on other sites

Oh shoot. I forgot to change some variables. Sorry.

 

<?php
$tbl_name2="members";
$myusername=$_SESSION['myusername'];
$mydn="SELECT displayname FROM $tbl_name2 WHERE username = '$myusername'";
$mydisplayname=mysql_query($mydn);
if (mysql_num_rows($mydisplayname)>0) {
   while ($row = mysql_fetch_assoc($mydisplayname)) {
      extract($row);
      if(session_is_registered(myusername)){
         echo "Welcome ".$mydisplayname." | <a href='logout.php'>Logout</a>";
      }
      else
      {
         echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
      }
   }
}
?>

Link to comment
Share on other sites

Aww man. Still came up with the same thing. I will post both the login.php and index.php files.

 

login.php:

<!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=iso-8859-1" />
<title>~ShaunsBoards~Login</title>
</head>
<center>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCDDCC">
<tr>
<td colspan="3" align="center"><strong><font color="#336633">ShaunsBoards Login </font></strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login">
</td>
</tr>
<tr>
<td align="center" colspan="4"><a href="register.php">Register</a></td>
</tr>
</table>
</td>
</form>
</tr>
</table><br /><br /><br />
</center>

</body>
</html>

 

checklogin.php:

 

<?php
$host="localhost"; // Host name 
$username="=P"; // Mysql username 
$password="=P"; // Mysql password 
$db_name="=P"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from signup form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
session_register("myusername");
header("location:index.php");
$_SESSION['myusername']=$myusername;

}
else {
echo "Wrong Username or Password";
}

?>

 

index.php:

 

<?php
session_start();
$host="localhost"; // Host name 
$username="=P"; // Mysql username 
$password="=P"; // Mysql password 
$db_name="=P"; // Database name 
$tbl_name="posts"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending 
$result=mysql_query($sql);
?>
<center>
<?php
$tbl_name2="members";
$myusername=$_SESSION['myusername'];
$mydn="SELECT displayname FROM $tbl_name2 WHERE username = '$myusername'";
$mydisplayname=mysql_query($mydn);
if (mysql_num_rows($mydisplayname)>0) {
   while ($row = mysql_fetch_assoc($mydisplayname)) {
      extract($row);
      if(session_is_registered(myusername)){
         echo "Welcome ".$mydisplayname." | <a href='logout.php'>Logout</a>";
      }
      else
      {
         echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
      }
   }
}
?>
</center>
<br /><br />
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="40%" align="center" bgcolor="#CCDDCC"><strong>Topic</strong></td>
<td width="20%" align="center" bgcolor="#CCDDCC"><strong>Author</strong></td>
<td width="15%" align="center" bgcolor="#CCDDCC"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Date/Time</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row 
?>
<tr>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['title']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['author']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['views']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['posts']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>

<?php
// Exit looping and close connection 
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#CCDDCC">
<? 
if(session_is_registered(myusername)){
echo "<a href='create_topic.php'><strong>Create New Topic</strong> </a>";
}
else
{
echo "<a href='login.php'>Login</a> to start topics!";
}
?>
</tr>
</table>

 

Feel free to change some things around and try. I really need this fixed. Thanks in advance.

 

~Shaun

Link to comment
Share on other sites

Ok, thanks for the tip. I only used $_SESSION because I don't know how to call session_register from the next page? Do you know?

In other words, on index.php I have $myusername=$_SESSION['myusername'];

 

How would i set up the variable for session register? Would it be:

$myusername=sessionregister['myusername']

 

???

 

~Shaun

Link to comment
Share on other sites

Like I said, don't use session_register. I rigged up a quick (hopefully working) solution for you. Also, NEVER put a password in a session.

 

checklogin.php

<?php
$host="localhost"; // Host name 
$username="=P"; // Mysql username 
$password="=P"; // Mysql password 
$db_name="=P"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from signup form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = $_POST['myusername'];
header("location:index.php");

}
else {
echo "Wrong Username or Password";
}

?>

 

index.php

<?php
session_start();
$host="localhost"; // Host name 
$username="=P"; // Mysql username 
$password="=P"; // Mysql password 
$db_name="=P"; // Database name 
$tbl_name="posts"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending 
$result=mysql_query($sql);
?>
<center>
<?php
$tbl_name2="members";
$myusername=$_SESSION['myusername'];
$mydn="SELECT displayname FROM $tbl_name2 WHERE username = '$myusername'";
$mydisplayname=mysql_query($mydn);
if (mysql_num_rows($mydisplayname)>0) {
   while ($row = mysql_fetch_assoc($mydisplayname)) {
      extract($row);
      if(session_is_registered(myusername)){
         echo "Welcome ".$mydisplayname." | <a href='logout.php'>Logout</a>";
      }
      else
      {
         echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
      }
   }
}
?>
</center>
<br /><br />
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="40%" align="center" bgcolor="#CCDDCC"><strong>Topic</strong></td>
<td width="20%" align="center" bgcolor="#CCDDCC"><strong>Author</strong></td>
<td width="15%" align="center" bgcolor="#CCDDCC"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Date/Time</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row 
?>
<tr>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['title']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['author']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['views']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['posts']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>

<?php
// Exit looping and close connection 
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#CCDDCC">
<? 
if(isset($_SESSION['mysuername'])){
echo "<a href='create_topic.php'><strong>Create New Topic</strong> </a>";
}
else
{
echo "<a href='login.php'>Login</a> to start topics!";
}
?>
</tr>
</table>

Link to comment
Share on other sites

Put this in index.php and tell us what it outputs (I've put some error_messages in there just to get an idea where the code is going haywire).

 

<?php
session_start();
error_reporting(E_ALL);
$host="localhost"; // Host name 
$username="=P"; // Mysql username 
$password="=P"; // Mysql password 
$db_name="=P"; // Database name 
$tbl_name="posts"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending 
$result=mysql_query($sql);
?>
<center>
<?php
$tbl_name2="members";
$myusername=$_SESSION['myusername'];
$mydn="SELECT displayname FROM $tbl_name2 WHERE username = '$myusername'";
$mydisplayname=mysql_query($mydn);
if (mysql_num_rows($mydisplayname)>0) {
   while ($row = mysql_fetch_assoc($mydisplayname)) {
      extract($row);
      if(session_is_registered(myusername)){
         echo "Welcome ".$mydisplayname." | <a href='logout.php'>Logout</a>";
      }
      else
      {
         echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
      }
   }
}
else {
   echo "You have encountered an error.";
}
?>
</center>
<br /><br />
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="40%" align="center" bgcolor="#CCDDCC"><strong>Topic</strong></td>
<td width="20%" align="center" bgcolor="#CCDDCC"><strong>Author</strong></td>
<td width="15%" align="center" bgcolor="#CCDDCC"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Date/Time</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row 
?>
<tr>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['title']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['author']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['views']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['posts']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>

<?php
// Exit looping and close connection 
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#CCDDCC">
<? 
if(isset($_SESSION['mysuername'])){
echo "<a href='create_topic.php'><strong>Create New Topic</strong> </a>";
}
else
{
echo "<a href='login.php'>Login</a> to start topics!";
}
?>
</tr>
</table>

Link to comment
Share on other sites

Logged out I get:

 

Notice: Undefined index: myusername in /var/www/=P/shaunsboards/index.php on line 21

You have encountered an error.

 

Logged in I get:

 

Notice: Use of undefined constant myusername - assumed 'myusername' in /var/www/=P/shaunsboards/index.php on line 27

Welcome Resource id #4 | Logout

 

Whats it all mean, lol.

 

~Shaun

 

Link to comment
Share on other sites

I forgot to replace session_is_registered. Durf!

 

<?php
session_start();
error_reporting(E_ALL ^ E_NOTICE);
$host="localhost"; // Host name 
$username="=P"; // Mysql username 
$password="=P"; // Mysql password 
$db_name="=P"; // Database name 
$tbl_name="posts"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending 
$result=mysql_query($sql);
?>
<center>
<?php
$tbl_name2="members";
$myusername=$_SESSION['myusername'];
$mydn="SELECT displayname FROM $tbl_name2 WHERE username = '$myusername'";
$mydisplayname=mysql_query($mydn);
if (mysql_num_rows($mydisplayname)>0) {
   while ($row = mysql_fetch_assoc($mydisplayname)) {
      extract($row);
      if(isset($_SESSION['myusername'])){
         echo "Welcome ".$mydisplayname." | <a href='logout.php'>Logout</a>";
      }
      else
      {
         echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
      }
   }
}
else {
   echo "You have encountered an error.";
}
?>
</center>
<br /><br />
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="40%" align="center" bgcolor="#CCDDCC"><strong>Topic</strong></td>
<td width="20%" align="center" bgcolor="#CCDDCC"><strong>Author</strong></td>
<td width="15%" align="center" bgcolor="#CCDDCC"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Date/Time</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row 
?>
<tr>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['title']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['author']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['views']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['posts']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>

<?php
// Exit looping and close connection 
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#CCDDCC">
<? 
if(isset($_SESSION['mysuername'])){
echo "<a href='create_topic.php'><strong>Create New Topic</strong> </a>";
}
else
{
echo "<a href='login.php'>Login</a> to start topics!";
}
?>
</tr>
</table>

Link to comment
Share on other sites

Here we go:

~Logged In~

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /var/www/=P/shaunsboards/index.php:3) in /var/www/=P/shaunsboards/index.php on line 4

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/=P/shaunsboards/index.php:3) in /var/www/=P/shaunsboards/index.php on line 4

Welcome Resource id #4 | Logout

 

 

~Logged Out~

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /var/www/=P/shaunsboards/index.php:3) in /var/www/=P/shaunsboards/index.php on line 4

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/=P/shaunsboards/index.php:3) in /var/www/=P/shaunsboards/index.php on line 4

You have encountered an error.

Link to comment
Share on other sites

checklogin.php

<?php
session_start();
$host="localhost"; // Host name 
$username="=P"; // Mysql username 
$password="=P"; // Mysql password 
$db_name="=P"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from signup form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = $_POST['myusername'];
header("location:index.php");

}
else {
echo "Wrong Username or Password";
}

?>

Link to comment
Share on other sites

Fixed some spelling errors, and changed a few things.

 

index.php

<?php
session_start();
error_reporting(E_ALL ^ E_NOTICE);
$host="localhost"; // Host name 
$username="=P"; // Mysql username 
$password="=P"; // Mysql password 
$db_name="=P"; // Database name 
$tbl_name="posts"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending 
$result=mysql_query($sql);
?>
<center>
<?php
$tbl_name2="members";
$myusername=$_SESSION['myusername'];
$mydn="SELECT displayname FROM $tbl_name2 WHERE username = '$myusername'";
$mydisplayname=mysql_query($mydn);
if (mysql_num_rows($mydisplayname)>0) {
   while ($row = mysql_fetch_assoc($mydisplayname)) {
      extract($row);
      if(isset($_SESSION['myusername'])){
         echo "Welcome , $myusername | <a href='logout.php'>Logout</a>";
      }
      else
      {
         echo "<a href='login.php'>Login</a> | <a href='register.php'>Register</a>";
      }
   }
}
else {
   echo "You have encountered an error.";
}
?>
</center>
<br /><br />
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="40%" align="center" bgcolor="#CCDDCC"><strong>Topic</strong></td>
<td width="20%" align="center" bgcolor="#CCDDCC"><strong>Author</strong></td>
<td width="15%" align="center" bgcolor="#CCDDCC"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#CCDDCC"><strong>Date/Time</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row 
?>
<tr>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['title']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['author']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['views']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['posts']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>

<?php
// Exit looping and close connection 
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#CCDDCC">
<? 
if(isset($_SESSION['myusername'])){
echo "<a href='create_topic.php'><strong>Create New Topic</strong> </a>";
}
else
{
echo "<a href='login.php'>Login</a> to start topics!";
}
?>
</tr>
</table>

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.