+ Reply to Thread
Results 1 to 2 of 2

Thread: Hello I am trying to determine what I am doing wrong.. with PHP and mysql

  1. #1
    comppro is offline x10Hosting Member comppro is an unknown quantity at this point
    Join Date
    Dec 2011
    Posts
    2

    Question Hello I am trying to determine what I am doing wrong.. with PHP and mysql

    Fair warning
    I do not know alot about mysql or php

    I am making an exception sheet, which I plan to migrate to a different local web server when it is finished.

    Basiaclly a user will log information.. and I want them to only view the ones that they entered.. Right now I am just focusing on being able to just view the records..

    I am having a rough time getting it to display my tables at all.. My method so far is write the code see what doesn't work and fix it. So far I have been able top may a login system, the ability to upload data to the sql server, and it can tell an admin from a regular user..

    My problem
    the site doesn't pull any errors, but it also doesn't display any data what so ever.. Any ideals?

    Here is the code I am using so far.

    Code:
    <?php
    //Load Files
    include 'connect.php';
    include 'header.php';
    
    //Load databases
    $sql = "SELECT
                exception.element_5_1, 
                exception.element_5_2, 
                exception.element_5_3, 
                exception.element_1_1, 
                exception.element_1_2, 
                exception.element_1_4,
                exception.element_2_1, 
                exception.element_2_2, 
                exception.element_2_4, 
                exception.element_4, 
                exception.element_3, 
                exception.topic_by
            WHERE
                exception.topic_by = " . mysql_real_escape_string($_GET['id']);
                
    
    
            $result = mysql_query($sql);
    
    
    
    
    
    //Make sure logged in and correctly privileged
    if($_SESSION['signed_in'] == false | $_SESSION['user_level'] != 0 )
    
    
    {
            //the user is not an admin
            echo 'Sorry, you do not have sufficient rights to access this page.';
    }
            else
    {    
    
    
    //if not signed in        
    if(!$_SESSION['signed_in'])
            {
                 echo '<tr><td colspan=2>You must be <a href="signin.php">signed in</a> to reply. You can also <a href="signup.php">sign up</a> for an account.';
            }
                else
        {        
            
            
            if(!$result)
    {
           while($posts_row = mysql_fetch_assoc($posts_result))
    
    //create table for use to view records.
               echo ' All Topics for the user '.$posts_row['topic_by'].'
            
        <table style="text-align: left; width: 762px; height: 77px;" border="1" cellpadding="2" cellspacing="2">
        <tbody>
        <tr>
          <td>Date</td>
          <td>Time In</td>
          <td>Time Out</td>
          <td>Code used</td>
          <td>Reason</td>
        </tr>
        <tr>
          <td>  '.$posts_row['element_5_1'].' - '.$posts_row['element_5_2'].' - '.$posts_row['element_5_3'].'</td>
          <td>  '.$posts_row['element_1_1'].' : '.$posts_row['element_1_2'].'   '.$posts_row['element_1_4'].'</td>
          <td>  '.$posts_row['element_2_1'].' : '.$posts_row['element_2_2'].'   '.$posts_row['element_2_4'].'</td>
          <td> '. $posts_row['element_4'].'</td>
          <td> '. $posts_row['element_3'].'</td>
        </tr>
        </tbody>
        </table> ';
     }
                    
        
            else
       {
            echo 'The topic could not be displayed, please try again later.';
       }
    
    
    
    
       }
    }
    
    
    
    
    include 'footer.php';
    ?>

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Hello I am trying to determine what I am doing wrong.. with PHP and mysql

    Note that you can use [PHP] tags for PHP code and it will be colorized according to. Similarly, there's [HTML] for HTML.

    Quote Originally Posted by comppro View Post
    My problem the site doesn't pull any errors, but it also doesn't display any data what so ever.
    You must write code to explicitly handle DB errors; it doesn't happen automatically. When you handle errors, make sure you don't output database error messages to non-admin users as it discloses too much information. Instead, log the MySQL error message. For some errors (such as those related to missing or invalid values), output your own error message to the user and what action the user can take to address it. For the rest, inform the user that there was an internal error.

    Using PDO (instead of the old mysql driver; more below), you can use exceptions by setting the error mode of the DB connection.

    Quote Originally Posted by comppro View Post
    PHP Code:
    <?php
    //Load Files
    include 'connect.php';
    Global variables (and implicit state) are bad. Instead, create a functions or (better yet) a class to manage the DB connection and segregate the credentials. You can use static variables and properties to store the connection so subsequent calls to the connect function don't have to recreate it. See "Display all that would be secret while Mysql is broken" and "[PHP] MySQL and PHP" for some illustrative examples of the technique.

    Quote Originally Posted by comppro View Post
    PHP Code:
    //Load databases
    $sql "SELECT
                exception.element_5_1, 
    [...]
                exception.element_3, 
                exception.topic_by
            WHERE
                exception.topic_by = " 
    mysql_real_escape_string($_GET['id']); 
    Read up on the syntax of SELECT as supported by MySQL. You're missing the table (the FROM clause).

    The column names are unreadable. Like variable names, column names should be descriptive, making them at least somewhat self-documenting.

    Quote Originally Posted by comppro View Post
    PHP Code:
            $result mysql_query($sql); 
    The mysql extension is outdated and on its way to deprecation. Instead, use PDO, which has many useful improvements, such as prepared statements and support for the Traversable interface, so you can loop over results with foreach. If you need a PDO tutorial, try "Writing MySQL Scripts with PHP and PDO".

    Quote Originally Posted by comppro View Post
    PHP Code:
    //Make sure logged in and correctly privileged
    if($_SESSION['signed_in'] == false $_SESSION['user_level'] != )
    { [...]
    }
            else
    {    
    //if not signed in        
    if(!$_SESSION['signed_in']) 
    Instead of nesting "if" blocks in "else" blocks, use elseif for mutually exclusive branches. The result (along with a decent indent style) will make the code more readable.

    Excepting the result table, table elements are used non-semantically. Don't use tables for layout, use CSS.

    Database access and display are separate concerns and should thus be handled by separate modules.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. what's wrong with this mysql command ?
    By viao12322 in forum Free Hosting
    Replies: 5
    Last Post: 01-05-2011, 10:00 PM
  2. Wrong MySQL DB count in cPanel
    By misson in forum Free Hosting
    Replies: 1
    Last Post: 11-06-2009, 01:24 PM
  3. Unable to determine my server
    By jfoley in forum Free Hosting
    Replies: 3
    Last Post: 07-28-2008, 12:43 AM
  4. MySQL Error! : Wrong Version
    By David in forum Free Hosting
    Replies: 2
    Last Post: 01-04-2008, 03:06 PM
  5. Something wrong with MySQL
    By The Corner in forum Free Hosting
    Replies: 1
    Last Post: 11-17-2007, 01:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers