Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post

    Get single column value from defined row using primary key.

    is there a way I can get a database row, then get a value from that row and put it into a variable?

    E.g.
    -----Column1-----Column2-----Column3-----Column4-----PrimaryKey
    Row: Value1-----Value2--------Value3-----Value4--------123------

    $variable = Value2;

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    Something like below should do the trick (untested):
    Code:
    // Connect to DB and select database here
    // change "example" to your table's name
    $result = mysql_query("SELECT Value2 FROM example
     WHERE PrimaryKey ='123'") or die(mysql_error());  
    
    $row = mysql_fetch_array( $result );
    echo $row['Value2'];

  • Users who have thanked shadowmaniac for this post:

    martynball (01-18-2010)

  • #3
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    The primary key sets the column value(s) as being unique. Just query the respective column(s) and fetch the result.
    Last edited by MattF; 01-18-2010 at 05:41 PM.

  • Users who have thanked MattF for this post:

    martynball (01-18-2010)

  • #4
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    Apparently there is an unexpected "}" on line 55, but I see no problem with it :/

    I am just trying to check if the correct value is being pulled in.
    PHP Code:
    <?php
    //Delete
    if (isset($_POST['deleteID'])) {

    $id mysql_real_escape_string($_POST['deleteID']);
    $deleteFile mysql_query("SELECT Nfilename FROM uploaded_files
        WHERE ID ='$id'"
    ) or die(mysql_error());  
    $row mysql_fetch_array$deleteFile );
        echo 
    $row['DeleteFile'];
        if (!
    deleteFile)
        {
        die(
    'Error: ' .mysql_error())
        }}
    $delete "DELETE FROM uploaded_files WHERE ID='$id'";
    $result mysql_query ($delete);
    if (!
    $result)
        {
        die(
    'Error: ' .mysql_error());
        }
        echo 
    "<center><div style=\"background-color:#999999; border:0px; color:#339900; font-family:Calibri; font-weight:bold;\">Deleted entry number: $id. And Deleted image related to that entry.</div></center>";
        }
    ?>

  • #5
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    Fixed that error.

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Indent your code, and put braces on their own lines. It makes it a lot easier when you have to visually inspect your code for errors such as those. For example:

    Code:
    if (isset($_POST['deleteID']))
    {
        $id = mysql_real_escape_string($_POST['deleteID']);
        $deleteFile = mysql_query("SELECT Nfilename FROM uploaded_files
            WHERE ID ='$id'") or die(mysql_error());
        [the rest of the code here]
    
        if ([something here])
        {
            [some more code here]
        }
    }
    Using an extra tab per level works wonders for ease of inspection.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •