How To Retrieve Data After Login

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Diksha
    New Member
    • Dec 2015
    • 3

    How To Retrieve Data After Login

    Code:
    <?php
    include("config.php");
    
    $name= $_POST['name'];
    $password= $_POST['password'];
    
    $query = mysql_query("SELECT * FROM data WHERE name='$name', password='$password' ");
    if(mysql_num_rows($query))
    {
    WHILE ($rows = mysql_fetch_array($query)):
    echo "<center>";
    $name = $rows['name'];
    $password = $rows['password'];
    $age = $rows['age'];
    $email = $rows['email'];
    $contact = $rows['contact'];
    $address = $rows['address'];
    $course = $rows['course'];
    $education = $rows['education'];
    echo "$name<br>$password<br>$age<br>$email<br>$contact<br>$address<br>$course<br>$education<br>";
    echo "</center>";
    endwhile;
    } 
    else {
    echo"Error!!";
    }
    ?>
    This code only shows the Error (which i have given in the echo statement).. I really don't know where is the problem in this code.. I will be very glad if anyone can help me in this.. pls
    Last edited by Rabbit; Dec 21 '15, 06:13 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #2
    I will point out your errors below, but first.
    1. When posting question use the [CODE/] around your code it makes it easier for other people to read your code.

    2. Use indents in your code also to make it easier to read. People are less likely to help if they cannot easily read your code.

    3. You should stop using mysql and switch to either mysqli or pdo. To change this you need to enable these modules in your php.ini file. That is found where ever you installed your php. They are discontinuing the use of the old mysql.

    Now to your specific errors

    You have four errors.
    1. In creating your query string, you do did not properly format the string. When you join your variables with your literal string, you must use the . to connect the components. Also when you have multiple items in your "where" clause you must join them with and or or, not a comma

    2. In the if condition you need to capture the result of the query

    3. In the while condition you need to refer to the result not the query when retrieving your data.

    4. In your final output string again you failed to concatenate the string using the . between variables and literal strings.

    Take a look at the changes I made below. Click the "expand" to view the code properly. In each case I left (but commented out) your original code. Then I inserted the corrected code below.

    Code:
    <?php
       include("config.php");
    
       $name= $_POST['name'];
       $password= $_POST['password'];
    
    //your incorrect code
    //   $query = mysql_query("SELECT * FROM data WHERE name='$name', password='$password' ");
    
       $query = mysql_query("SELECT * FROM data WHERE name=[B][I][U]".$name." and[/U][/I][/B] password=[B][I][U]".$password[/U][/I][/B]);
    
    
    // your incorrect code
    //   if(mysql_num_rows($query))
       if([B][U][I]$result = [/I][/U][/B]mysql_num_rows($query))
       {
    
    //your incorrect code
    //      WHILE ($rows = mysql_fetch_array($query)):
          WHILE ($rows = mysql_fetch_array([B][U][I]$result[/I][/U][/B])):
            echo "<center>";
            $name = $rows['name'];
            $password = $rows['password'];
            $age = $rows['age'];
            $email = $rows['email'];
            $contact = $rows['contact'];
            $address = $rows['address'];
            $course = $rows['course'];
            $education = $rows['education'];
    
    //your incorrect code
     //       echo "$name<br>$password<br>$age<br>$email<br>$contact< br>$address<br>$course<br>$education<br>";
    
            echo $name[B][I][U]."<br>".[/U][/I][/B]$password[B][I][U]."<br>".[/U][/I][/B]$age[B][I][U]."<br>".[/U][/I][/B]$email[B][I][U]."<br>".[/U][/I][/B]$contact[B][I][U]."<br>".[/U][/I][/B]$address[B][I][U]."<br>".[/U][/I][/B]$course[B][I][U]."<br>".[/U][/I][/B]$education."<br>";
    
            echo "</center>";
    
         endwhile;
    
      }else{
    
        echo"Error!!";
      }
    ?>
    Last edited by Claus Mygind; Dec 21 '15, 04:45 PM. Reason: additional errors found in original code

    Comment

    • Diksha
      New Member
      • Dec 2015
      • 3

      #3
      Thanks for Your Help!!

      Comment

      Working...