Need help with WHILE statement

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Skipper02
    Sergeant

    • Jun 2008
    • 25

    Need help with WHILE statement

    I am trying to display data from my tables using the following code but nothing is being displayed. I have verified that the data is there in the table inside the database. No echo statements within the while loop is executing. The code that I am using is;

    ?php
    session_start();
    $userId = $_SESSION['userId'];
    $password = $_SESSION['password'];
    $db_host= "localhost";
    $db_user = "XXXX_YYYY";
    $db_password = "ZZZZ";
    $db_name ="XXXX_StudyKitchenData";
    $db = mysql_connect($db_host, $db_user, $db_password);
    mysql_select_db($db_name, $db);
    $sql = "SELECT Email FROM UserDetails WHERE Email = '$userId' AND Password = '$password'";
    $result = mysql_query($sql, $db);
    if(!mysql_num_rows($result)== 1){
    unset($_SESSION['userId'], $_SESSION['password']); // clear bad username/password from session;
    header("location:http://www.studykitchen.com/login.php"); // redirect for them to login again
    }
    $query = "SELECT Page, Date, Time, ID FROM mydiary WHERE Email ='userId'";
    $result = mysql_query($query, $db);
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
    //I did not get the following echo messages nor any error messages
    echo "Date : ".$row['Date']." Time : ".$row['Time']." ID : ".$row['ID']." <br>" .
    "".$row['Page']."<br>".
    "".$row[<HR color="#0000ff" noshade size="5">]."<br><br>";
    }
    mysql_close($db);
    ?>

    Please guide.
  • Karen Mac
    General

    • Apr 2006
    • 8332

    #2
    Re: Need help with WHILE statement

    Im not an expert but.. shouldnt the result come after the FETCH or While?

    Karen

    VodaHost

    Your Website People!
    1-302-283-3777 North America / International
    02036089024 / United Kingdom
    291916438 / Australia

    ------------------------

    Top 3 Best Sellers

    Web Hosting - Unlimited disk space & bandwidth.

    Reseller Hosting - Start your own web hosting business.

    Search Engine & Directory Submission - 300 directories + (Google,Yahoo,Bing)


    Comment

    • navaldesign
      General & Forum Moderator

      • Oct 2005
      • 12080

      #3
      Re: Need help with WHILE statement

      add an error reporting line so you can see what is wrong (your code seems correct):


      if (!$result){
      echo "Error: ".mysql_error();
      }

      after

      $result = mysql_query($query, $db);


      If you get no echo at all, it meand that $result is not a valid MySQL resource. This can be due to many reasons. Generally speaking, you should be adding error reporting in all cases so that debugging will result easier.

      And, i am allowed, your code is absolutely insecure and vulnerable to an injection attempt. If your final application is based on the above, hacking your MySQL will be quite easy.
      Navaldesign
      Logger Lite: Low Cost, Customizable, multifeatured Login script
      Instant Download Cart: a Powerfull, Customized, in site, DB driven, e-products Cart
      DBTechnosystems.com Forms, Databases, Shopping Carts, Instant Download Carts, Loggin Systems and more....
      Advanced BlueVoda Form Processor : No coding form processor! Just install and use! Now with built in CAPTCHA!

      Comment

      • Skipper02
        Sergeant

        • Jun 2008
        • 25

        #4
        Re: Need help with WHILE statement

        Navaldesign,

        Again my grateful thanks to you for your guidance. I found the two syntax errors. I missed out the dollar sign in front of the variable in the query statement and didnot put the correct backslashes before defining the color for the horizontal line.

        This is not my final application. Actually I am testing parts of it before I publish the whole website. Please do feel free to provide any suggestion/guidance to make my MYSQL more secure. You are way ahead of me in programming so any advice from you will be much appreciated.

        I have just one more issue to resolve and then will be ready to publish the website. I will open another thread for it.

        Comment

        Working...
        X