FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


if statement





jasonf90
Hi guys,

i know this might sound a bit easy but i cant get it to work. I want to create a if statement.

My concept of the if statement was if the database list is emptey display a message like

"you have no fields in the database". Im using sessions with a userNO.

here is what i have so far.

<?php
if ($_SESSION['userno'])
{
//get id
$user = $_SESSION['userno'];
//connect to the database
include('connect.php');
//create the SQL statement
$sql = "SELECT * FROM tblpost WHERE userNo='$user' ORDER BY postName;";
//send sql statment
$result = mysql_query($sql);
//store the resurlts
while ($row = mysql_fetch_array ($result)){
?>
<?php
echo "<a href='view.php?id=".$row['postNo']."'>".$row['postName']."</a>";

}
}
else {echo ("You have not entered any Post <a href='insert.php'>Insert Product</a>");}

?>

Not sure where i have gone wrong.

thanks for the help
Navigator
jasonf90 wrote:
Hi guys,

i know this might sound a bit easy but i cant get it to work. I want to create a if statement.

My concept of the if statement was if the database list is emptey display a message like

"you have no fields in the database". Im using sessions with a userNO.

here is what i have so far.

<?php
if ($_SESSION['userno'])
{
//get id
$user = $_SESSION['userno'];
//connect to the database
include('connect.php');
//create the SQL statement
$sql = "SELECT * FROM tblpost WHERE userNo='$user' ORDER BY postName;";
//send sql statment
$result = mysql_query($sql);
//store the resurlts
while ($row = mysql_fetch_array ($result)){
?>
<?php
echo "<a href='view.php?id=".$row['postNo']."'>".$row['postName']."</a>";

}
}
else {echo ("You have not entered any Post <a href='insert.php'>Insert Product</a>");}

?>

Not sure where i have gone wrong.

thanks for the help


Are you positive userNo is set? try using the if condition with isset (google it) on that session variable.
jasonf90
Quote:
Are you positive userNo is set? try using the if condition with isset (google it) on that session variable.


Im pretty sure that its set because i do have data in the database and it shows the required data for that userNO that is signed in
Marcuzzo
1. don't forget to start the session
Code:
session_start(); 


2. it is better to check if the session variable is set before handling it's value
Code:
<?php
   // start the session
    session_start(); 

   // check if the session variable is set
   if ( isset( $_SESSION['userno'] ) )
   {
    // get data here
   }

?>


3. what is the code in connect.php? you include it but didn't post the content.
also, did you Select your database in connect.php? as it doesn't show in the code mysql_select_db

4. my last question is: "What is actually going wrong?"
all you said is that you don't know where you have gone wrong.
what is the output of your script?
do you get a PHP error message?
post the outcome here and we should be able you point you in the right direction

cheers
rvec
Do you mean this?
I added a mysql_num_rows() call.

Code:
<?php
if ($_SESSION['userno'])
{
  //get id
  $user = $_SESSION['userno'];
  //connect to the database
  include('connect.php');
  //create the SQL statement
  $sql = "SELECT * FROM tblpost WHERE userNo='$user' ORDER BY postName;";
  //send sql statment
  $result = mysql_query($sql);

  if (mysql_num_rows($result)) {
    //store the resurlts
    while ($row = mysql_fetch_array ($result)){
      ?>
      <?php
      echo "<a href='view.php?id=".$row['postNo']."'>".$row['postName']."</a>";

    }
  } else {
    echo ("You have not entered any Post <a href='insert.php'>Insert Product</a>");
}
else {echo 'No such user.';}
Navigator
I guess that this was one of those posts...for the sake of the post...
Related topics
Australian state to ban workplace e-mail spying
Novell acquires Linux security company
MySQL statement error (got me going)
Interest in Environmentally Conscious website
Error on running a Big SQL Statement on MySql
Question about a mySQL statement
break statement
Viking Metal
Error trying to display SQL statement in html table
the most hypocritical statement of 2008 so far
what is the difference between a Girl as a friend and a GF?
statement MySQL question
if statement not working
How to use an if statement
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.