How to query a single data on mySQL database using PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dragon Sid
    New Member
    • Jun 2011
    • 1

    How to query a single data on mySQL database using PHP

    Hello everyone!

    Can anyone tell me on how can i code using php the query on mysql database that gets a single value from it? i have here my code

    Code:
    $this->SetCon();
    $orgSQL = "SELECT org_id FROM users WHERE org_name = '$orgName';";
    $org_query_result = mysql_query($orgSQL);
    $org_key;
    while($row = mysql_fetch_row($org_query_result, MYSQL_ASSOC))
    {
       $org_key = $row['org_id'];
    }
    return $org_key;
    This code allows me to get a value from the database but the problem is i can only get the first value on my database i can't get the value in the database if it is not on the first row. Can anyone tell me of my mistake here?

    Im new to PHP and MYSQL environment.

    I will really appreciate any help.

    Thanks
    Last edited by Niheel; Jun 27 '11, 08:18 PM.
  • sidhx
    New Member
    • Dec 2009
    • 50

    #2
    hey Niheel use array variable like this $org_key[]= $row['org_id']; inside the loop and define it as $org_key=array( ); before starting the loop.

    Check this link:http://in2.php.net/manual/en/book.array.php

    Comment

    • John Doe
      New Member
      • Jun 2011
      • 20

      #3
      Sid,

      You will find that you are only getting the last org_id, not the first one.

      The 'mistake' is that you are reading the value of org_id into your $org_key variable, then moving to the next record in your database and again reading the org_id into your $org_key variable - which overwrites the old value that you put in there.

      The solution depends on what you are trying to achieve really. From your code, it appears to be a snippet from a function you have written. The function also appears to just pull the org_id of the specified $orgName from the database (in which case I would only expect one org_id to be returned).

      Comment

      Working...