Problem of print the data from mysql

Discussion in 'PHP' started by mark103, Oct 1, 2010.

  1. #1
    Hi guys,

    I have a problem of prints of the data from table1. I am checking out the data of username and password in the url to go through in members table and then checking the username in table1 to get matching data and select rows with the fields id of teststrings1, teststrings2 and teststrings3, then print out the data.


    Here it is the code

    
    <?php
    session_start();
        define('DB_HOST', 'localhost');
        define('DB_USER', 'myusername');
        define('DB_PASSWORD', 'password');
        define('DB_DATABASE', 'mydatabasetable');
           
        $errmsg_arr = array();
        $errflag = false;
    
        $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        if(!$link) {
      die('Failed to connect to server: ' . mysql_error());
        }
    
        $db = mysql_select_db(DB_DATABASE);
        if(!$db) {
      die("Unable to select database");
        }
    
       function clean($var){
    
    return mysql_real_escape_string(strip_tags($var));
        }
      
        $username = clean($_GET['user']);
        $password = clean($_GET['pass']);
        if($username == '') {
      $errmsg_arr[] = 'username ID missing';
      $errflag = true;
        }
        if($password == '') {
      $errmsg_arr[] = 'PASSWORD ID missing';
      $errflag = true;
        }
    
        if($errflag) {
      $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
      echo implode('<br />',$errmsg_arr);
       }
       else {
      $qry="SELECT * FROM members WHERE username='$username' AND passwd='$password'";
      $result=mysql_query($qry) or die('Error:<br />' . $qry . '<br />' . mysql_error());
    
    
        if($result) { 
      $qrytable1="SELECT teststrings1, teststrings2, teststrings3 FROM table1 WHERE username='$username'";
      $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());
      echo "<p id='teststrings1'>";
      echo $row['teststrings1'] . "</p>";
      echo "<p id='teststrings2'>";
      echo $row['teststrings2'] . "</p>";
      echo "<p id='teststrings3'>";
      echo $row['teststrings3'] . "</p>";
      }
     }
    ?>
    
    PHP:
    When I am am checking the data of username and password through in members table and then checking the username in table1 before select the rows with the fields id of teststrings1, teststrings2 and teststrings3 to prints out, but it have print out as blank on php page.

    Any idea?
     
    mark103, Oct 1, 2010 IP
  2. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Hi, mark103,

    You forgot to fetch_array before printing:
    
    $row = mysql_fetch_array($result1);
    
    PHP:
    Regards :)
     
    koko5, Oct 1, 2010 IP
  3. mark103

    mark103 Active Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Thanks koko5, but you haven't suggest in which line that I should replace it?
     
    mark103, Oct 1, 2010 IP
  4. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #4
    Let me know is it ok now.
    Edit: You must paste this line there, not editing existing line!
     
    Last edited: Oct 1, 2010
    koko5, Oct 1, 2010 IP
  5. mark103

    mark103 Active Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #5
    Thanks for the help, I think the problem have fixed there. However, I have added more than one row in the table, but it did not collect the data from the rows and print out on php page.

    Please help!!!!!!
     
    mark103, Oct 1, 2010 IP
  6. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #6
    Than you should put this in a loop:
    
    $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());
    while ($row = mysql_fetch_array($result1)){
    echo "<p id='teststrings1'>";
    echo $row['teststrings1'] . "</p>";
    echo "<p id='teststrings2'>";
    echo $row['teststrings2'] . "</p>";
    echo "<p id='teststrings3'>";
    echo $row['teststrings3'] . "</p>"; 
    }
    
    PHP:
     
    koko5, Oct 1, 2010 IP
  7. mark103

    mark103 Active Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #7
    Thank you very much for your help which it much appreicated.
    However, I need the last things before I will mark this thread as resolve.
    How can add the delete button on php page for each row??
     
    mark103, Oct 1, 2010 IP
  8. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #8
    Echo the button code (which must have method post/get etc.) in the loop and id of the row, so you can delete it :)
     
    koko5, Oct 1, 2010 IP
  9. mark103

    mark103 Active Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #9
    Yeah, I know that so please could you post an example code for the echo button code for each row that can be delete it by click on the button? :)
     
    mark103, Oct 1, 2010 IP
  10. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #10
    It's too late here but you must give it a try and if there is problem, post in this thread and someone will help :)
     
    koko5, Oct 1, 2010 IP