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 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts

    mysql_fetch_array - Question!

    Alright, well I've been trying to figure this out and can't seem to find out why the info that is in the database isn't displaying onto the page. I have info that is being held in these tables, but when I call the mysql_fetch_array function to grab the info out of the database and then I try and echo it out onto the page, I get my die message. Any ideas? Heres my script.


    PHP Code:

    <?php

    $con 
    mysql_connect('localhost','username','pass','db');

    if(!
    $con)
    {
    die(
    'Database error while trying to connect');
    }

    mysql_select_db('db' $con);

    $query mysql_query("SELECT id, fname, lname, password, email, age FROM register") or die('Database error.');

    while(
    $display mysql_fetch_array($query))

    {

    echo 
    "First Name: " .$display ['fname']. "<br />Last Name: " $display ['lname']. "<br />Password: " .$display['password']. "<br />Email: " .$display ['email']. "<br />Age: " .$display['age']."";
     
    }


    ?>

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Try this:

    PHP Code:
    $query mysql_query("SELECT id, fname, lname, password, email, age FROM register") or die("Query error: ".mysql_error()); 

  • #3
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Ok, I did that. Its saying that no Database was selected. Doesn't make sense because I'm pretty sure when I say: 'FROM register' means that I'm selecting a database.

  • #4
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Register is a table.

    Let's say you wanted to work on a couple different projects. You are making a shopping cart application for yourself and your cousin asks you to create a message board.

    You would create two different databases. One named shopping_cart and one named messageboard (or whatever).

    Each of those databases would have their own tables. Register is a table that is part of your 'db' database.

    EDIT: For that matter, it is dying when you are initially connecting. You need to fix this line:

    $con = mysql_connect('localhost','username','pass','db');

    I don't believe you pass the database at that point so it should be:
    $con = mysql_connect('localhost','username','pass');

    http://php.net/mysql_connect
    Last edited by arnyinc; 01-04-2008 at 07:35 PM.

  • #5
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Any ideas on what that could be. I know I have a table in the database called 'register'. The fields are id(primy key, Autoincrement), fname, lname, password, email, age, but yet its saying that I didn't select a database... Not making sense. I should also say that I have stuff in the field for the mysql_fetch_array to display onto the page.

  • #6
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    If you start with arnyinc's suggestion of removing the db part from the initial connect then in your mysql_select_db you need to change the full stop to a comma i.e.:

    PHP Code:

    $con 
    mysql_connect('localhost','username','pass');

    if(!
    $con)
    {
    die(
    'Database error while trying to connect');
    }

    mysql_select_db('db' ,$con); 
    Mike

  • #7
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Ahh.... Thank you very much for your help. I missed that complately. I actually noticed it before I came back to the thread to see if you had posted back any more replys because I went ahead and checked to see what I had for my other connection script that I had for when I submited the stuff to the database, and shortly after noticed what I my problem was. However I did remove the extra DB and it seems to work fine. Once again, thank you for your time.

  • #8
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Hey anarchy3200, do you talk on a IM services? If you do I was wondering if I could get your screen name for if I had some quick question I could ask you. I'm fairly new to PHP and to have someone I could ask a quick question really fast would be great. My Yahoo! screen name is 'manskater777@yahoo.com'.

    Thanks
    Jon W


  •  

    Posting Permissions

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