how to call a column value from a tablein database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satyasampan
    New Member
    • Aug 2012
    • 9

    how to call a column value from a tablein database

    Code:
    $sql = "UPDATE `students` 
       SET `name` = '$name',
       `surname` = '$surname',
       `father` = '$father',
       `mobile` = '$mobile',
       `address` = '$address' 
       WHERE 
          `students`.`rollnum` = '$501' LIMIT 1";
     
    mysql_query($sql) or die ("Error: ".mysql_error());
    
    WHERE `students`.`rollnum` = '501'
    how can i update a rollnumber without giving its value directly... if i want to update details of 502,it ll be waste of time to open this file and change number each time...
    please help
    Last edited by zmbd; Sep 10 '12, 04:50 AM. Reason: (Z) placed required code tags. Stepped code for easier read.
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    Code:
      $rollnum="501";
      $sql= "....WHERE `students`.`rollnum` = '$rollnum' LIMIT 1";

    Comment

    • mskctg
      New Member
      • Aug 2012
      • 9

      #3
      use a variable for rollNum
      your code should be look like this
      Code:
      $sql = "UPDATE students SET name = $name, surname = $surname, father = $father, mobile = $mobile, address = $address, Where rollnum = $rollnum";
      mysql_query($sql) or die ("Error: ".mysql_error());

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        @mskctg: but wat if $name contains one (or more) spaces?
        The original code was better, it included the name in single quotes..

        About 'LIMIT 1': we dont have enough info to tell if this is correct, or not....

        Comment

        • mskctg
          New Member
          • Aug 2012
          • 9

          #5
          @Luuk: Why should we use spaces in variable name? If its needed we should use underscore like $first_name.

          'LIMIT' keyword is used to limit the query result. if we use 'LIMIT 1' it will show only one row from our query result. if we use 'LIMIT 5' it will show first five rows.

          'LIMIT' keyword take TWO Parameter. Suppose We use 'LIMIT 5,10' it will show 10 rows starting from row number 5.

          Comment

          Working...