SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Digital Warrior Renegade's Avatar
    Join Date
    Nov 2000
    Location
    Portland, OR
    Posts
    480
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Stupid newbie question

    Hi everyone, I am new to PHP, but not to programming. I am well versed in C, so the learning process is going quite smoothly and quickly. More so than I would have thought

    But I digress....

    In plain simple english, here is the query I am trying to perform on the MySQL server:

    "What is the id of the person whose first name is ____"?

    Here is my code:

    $sql = "SELECT id FROM users WHERE firstname='$firstname';";
    $result = mysql_query($sql);
    $userrow = mysql_fetch_array($result);
    $userid = $userrow["id"];

    The $firstname var gets its value from the user who selects the name of the person. I have verified that $firstname contains the desired data.

    It seems that $userid is always 0, no matter what.

    What am I doing wrong here? Suggestions?
    Thanks

  2. #2
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    $sql "SELECT id FROM users WHERE firstname='$firstname'";
    $result mysql_query($sql);
    while(
    $row mysql_fetch_array($result)){
          echo 
    $row['id'];

    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  3. #3
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  4. #4
    ********* Addict
    Join Date
    Apr 2000
    Location
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure id is a column on the database? That has already happened to me.

  5. #5
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is that you have a ; at the end of the SQL query inside the ". You don't need it as PHP automatically adds it in for you. So the end of your query should look like '"; not ';";
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  6. #6
    Digital Warrior Renegade's Avatar
    Join Date
    Nov 2000
    Location
    Portland, OR
    Posts
    480
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow, thanks for the quick replies...

    Yes, id does exist as a column in the DB....lol

    I copied your code, exbabylon, and pasted it into mine, still no workie. In fact, it didn't even echo anything out to the screen. The screen was blank as if nothing was echoed at all

    Removing semi colons didn't help either.

    Anything else I might try?

  7. #7
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try:

    <?php
    mysql_connect("host", "username", "pass");
    mysql_select_db("databasename");
    print $firstname; # Check whether it really exists!
    $sql = "SELECT id FROM users WHERE firstname='$firstname'";
    $result = mysql_query($sql);
    $userrow = mysql_fetch_array($result);
    $userid = $userrow["id"];
    print $userid; #Try and print it
    ?>
    Last edited by petesmc; Jun 8, 2001 at 14:04.

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pete, I hate to sound like a nag, but I don't want anyone to get confused when they read your code

    $print $firstname;

    should actually be

    print $firstname;
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Opps....DIdn't notice that...THanks...

  10. #10
    Digital Warrior Renegade's Avatar
    Join Date
    Nov 2000
    Location
    Portland, OR
    Posts
    480
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, figured it out.

    I was getting input from user from a selection list containing first and last names, seperated by a comma. I used explode() to extract each seperate name....whitespace and all

    I used trim() on $firstname and voila! worked perfectly.

    DOH!

    thanks everyone....


Bookmarks

Posting Permissions

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