Idiot needs help.

Discussion in 'PHP' started by aaron_nimocks, Aug 9, 2006.

  1. #1
    Im trying to make queries to get go to my DB and pull out everything that starts with the letter "A" in a row. I think I get it all except I dont know how to display the results. Heres what I got.

    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
    mysql_select_db($dbname);
    
    $sql = "SELECT * FROM babynames WHERE Name LIKE `a%`";
    $result = mysql_query($sql);
    
    PHP:
    Just need the line to display the results. Thanks for looking and helping the stupid. :)
     
    aaron_nimocks, Aug 9, 2006 IP
  2. ip076

    ip076 Peon

    Messages:
    79
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    
    //Loop thru the rows in the result
    while($row = mysql_fetch_assoc($result))
    {
    //Display the information you want
    echo $row['Names'] . "<br />\n";
    }
    
    PHP:
    This will loop thru all the returned rows and print each name on a new line in your browser (the <br />). The \n will make the source more readable instead of throwing it all on the same line when you view source.

    You said something about including them all on the same row, I'm not sure what you meant, but if you want them all to show on the same row, replace the <br /> with a space or whatever characters you want to separate the names with.
     
    ip076, Aug 9, 2006 IP
  3. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #3
    I still get an error as you can see here http://www.www.dbseller.com/babynames/

    Line 22 is the "while" line. This is the complete code that I am using. Does it look like I am missing something?

    <?php
    // This is an example of config.php
    $dbhost = 'localhost';
    $dbuser = '';
    $dbpass = '';
    $dbname = '';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
    mysql_select_db($dbname);
    
    $sql = "SELECT * FROM babynames WHERE Name LIKE `r%`";
    $result = mysql_query($sql);
    
    while($row = mysql_fetch_assoc($result))
    {
    echo $row['Names'] . "<br />\n";
    }
    
    ?>
    PHP:
     
    aaron_nimocks, Aug 9, 2006 IP
  4. ajscottsr

    ajscottsr Peon

    Messages:
    388
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The error you are getting is telling you that $result isn't a valid resource.

    That means the MySQL query is probably generated an error.

    Run your MySQL query directly into MySQL and see what you get:

     
    ajscottsr, Aug 9, 2006 IP
  5. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #5
    I get the correct results with that.
     
    aaron_nimocks, Aug 9, 2006 IP
  6. ajscottsr

    ajscottsr Peon

    Messages:
    388
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #6
    on line 11, add an || or die; to the end (remove the original ;) and see if it ends before it errors out.
     
    ajscottsr, Aug 9, 2006 IP
  7. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #7
    If you are saying make the line look like:

    $result = mysql_query($sql) || or die;

    then yes it does error out now.
     
    aaron_nimocks, Aug 9, 2006 IP
  8. ajscottsr

    ajscottsr Peon

    Messages:
    388
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #8
    sorry, take out the ||, its a double... you can use || ... you can also use "or" ... you can't use both (my bad).
     
    ajscottsr, Aug 9, 2006 IP
  9. ajscottsr

    ajscottsr Peon

    Messages:
    388
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Also, from mysql, run the following:

    describe babynames

    post the output here.

    The error you are getting definitely indicates something went wrong with the MySQL query, database or connection.
     
    ajscottsr, Aug 9, 2006 IP
  10. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #10
    Still an error with

    $result = mysql_query($sql) or die;
     
    aaron_nimocks, Aug 9, 2006 IP
  11. ajscottsr

    ajscottsr Peon

    Messages:
    388
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I just spotted something...

    In your code:

    Add an ; to the end of it.
     
    ajscottsr, Aug 9, 2006 IP
  12. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #12
    Not sure how to display the results of that but it displayed all 4 rows that I have.

    ID varchar(255) YES NULL
    Name varchar(255) YES NULL
    Meaning varchar(255) YES NULL
    Origin varchar(255) YES NULL
     
    aaron_nimocks, Aug 9, 2006 IP
  13. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #13
    I had the ; just didnt paste it on here I guess
     
    aaron_nimocks, Aug 9, 2006 IP
  14. ajscottsr

    ajscottsr Peon

    Messages:
    388
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Look carefully at your $row['Names']

    The database output reflects 'Name'

    Try something different for me:

     
    ajscottsr, Aug 9, 2006 IP
  15. jnm

    jnm Guest

    Messages:
    343
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #15
    not the problem but in your echo statement you have the field name as Names when it should be Name.
     
    jnm, Aug 9, 2006 IP
    aaron_nimocks likes this.
  16. ajscottsr

    ajscottsr Peon

    Messages:
    388
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #16
    I see you are back to the mysql resource error...

    This has to be something wrong with the MySQL stuff.

    Add "or die;" to the end of the mysql_select_db.

    Other than that, I'm just not seeing it.

    I have to run for the night.

    If you don't have it working by AM, PM me the entire code (leave out your passwords) and I'll setup a sample DB on my server and put in the code and find it for you.
     
    ajscottsr, Aug 9, 2006 IP
    aaron_nimocks likes this.
  17. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #17
    aaron_nimocks, Aug 9, 2006 IP
  18. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #18
    added that back in. Showing no error now. Almost there. :)
     
    aaron_nimocks, Aug 9, 2006 IP
  19. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #19
    Son of a case sensitive!

    All the names start with uppercase so thats why the lower case r wasnt returning nothing. All works now. :)

    Thanks
     
    aaron_nimocks, Aug 9, 2006 IP
  20. ip076

    ip076 Peon

    Messages:
    79
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #20
    You could possibly prevent something like that to converting both to lowercase to compare them, maybe something like:

    SELECT * FROM babynames WHERE LOWER(Name) LIKE LOWER(a%)
     
    ip076, Aug 9, 2006 IP