My first mysql query ~ Is anything wrong with this way?

Discussion in 'MySQL' started by Colbyt, Sep 25, 2007.

  1. #1
    This is my first ever mysql query that I have written and after some trial and error it works. The echo part is just for testing purposes. I will assign that to another variable for the actual script.

    $result = mysql_query( "SELECT * FROM user WHERE user_name='$user_name'" ) or error( mysql_error() );
    	$row = mysql_fetch_array($result, MYSQL_NUM);
    	echo $row[2];
    PHP:
    Is there anything wrong with using the $row[#] method to retrieve a single item from a db row?
    Is there a better way to do the same thing.
     
    Colbyt, Sep 25, 2007 IP
  2. GoodOmens

    GoodOmens Peon

    Messages:
    73
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can call the row header itself such as $row['blahh'] if your column header is blahh.

    Makes your code a little more readable.

    As a coder your job is to make your code as readable as possible. I can't count how many times I've gone back to something I wrote and spent a good while trying to figure out what the hell I was trying to do.
     
    GoodOmens, Sep 25, 2007 IP
    Colbyt likes this.
  3. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #3
    I think you said, "if my row header is named email, then I can use instead of [2]" ????????????

    This works no matter whee in the row it is?
     
    Colbyt, Sep 25, 2007 IP
  4. GoodOmens

    GoodOmens Peon

    Messages:
    73
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
     
    GoodOmens, Sep 25, 2007 IP
  5. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #5
    I tried it with ' ' and with " " and niether worked. Both of course were inside the [ ].

    Maybe I did something wrong.

    Okay I tried this and it worked:

    It was the
    that made it fail to work.

    Green for you and thanks for your help.
     
    Colbyt, Sep 25, 2007 IP