Results 1 to 7 of 7

Thread: php mysql

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Location
    UK
    Beans
    278
    Distro
    Kubuntu

    php mysql

    I have lots of mysql data on my website and wish to access it using a php script.
    phpmyadmin says I have one database 'lancasterfamily' and one of the tables is 'pgv_individuals'
    If I run the following code (cobbled from a tutorial website):-
    Code:
    <?php
    // Connecting, selecting database
    $link = mysql_connect('localhost', 'username', 'pwd')
        or die('Could not connect: ' . mysql_error());
    echo 'Connected successfully';
    
    mysql_select_db('lancasterfamily.pgv_individuals') or die('Could not select database');
    
    // Performing SQL query
    $query = 'SELECT * FROM my_table';
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    
    // Printing results in HTML
    echo "<table>\n";
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo "\t<tr>\n";
        foreach ($line as $col_value) {
            echo "\t\t<td>$col_value</td>\n";
        }
        echo "\t</tr>\n";
    }
    echo "</table>\n";
    
    // Free resultset
    mysql_free_result($result);
    
    // Closing connection
    mysql_close($link);
    ?>
    I get the following result:-
    Connected successfullyCould not select database

    Any help would be much appreciated
    Last edited by bill-lancaster; March 1st, 2014 at 09:26 AM.

  2. #2
    Join Date
    Mar 2009
    Location
    UK
    Beans
    278
    Distro
    Kubuntu

    Re: absolute beginner - php mysql

    That was stupid! Publishing my password was not a good idea - I've now changed it.

  3. #3

    Re: absolute beginner - php mysql

    Quote Originally Posted by bill-lancaster View Post
    That was stupid! Publishing my password was not a good idea - I've now changed it.
    You should edit the original post and either remove it, or just use <password> there.

    and your selecting a db.table here?
    Code:
    mysql_select_db('lancasterfamily.pgv_individuals'
    so try just
    Code:
    mysql_select_db('lancasterfamily') or die('Could not select database');
    or should that be
    Code:
    mysql_select_db('pgv_individuals') or die('Could not select database');
    ?

    What is the db name?

    found an old link with this basis as an example.
    PHP Code:
    <?php
    $link 
    mysql_connect('server.com''mysql_user''mysql_password');
    if (!
    $link) {
        die(
    'Could not connect: ' mysql_error());
    }
    echo 
    'Connected successfully';
    mysql_close($link);
    ?>

    Code:
    php -f test.php
    Connected successfully
    Last edited by Habitual; February 28th, 2014 at 06:22 PM.
    Windows assumes the user is an idiot.
    Linux demands proof.

  4. #4
    Join Date
    Mar 2009
    Location
    UK
    Beans
    278
    Distro
    Kubuntu

    Re: absolute beginner - php mysql

    Thanks for the ideas. Have change my post as sugested as well as the db password!

    Code:
    mysql_select_db('lancasterfamily') or die('Could not select database');
    Gives:-
    "Connected successfullyQuery failed: Table 'lancasterfamily.my_table' doesn't exist"

    Also:-
    Code:
    mysql_select_db('pgv_individuals') or die('Could not select database');
    produces:-
    "Connected successfullyCould not select database"

    I also found that old link, the code runs fine - I get get a connection.
    Adding further code to open a database gives me the same problems.

    Running phpMyAdmin on my webspace provider's control panel shows one database - 'lancasterfamily'

  5. #5
    Join Date
    Mar 2009
    Location
    UK
    Beans
    278
    Distro
    Kubuntu

    Re: absolute beginner - php mysql

    OK - don't know what I was doing wrong but now have full access to database
    Thanks for the help.

  6. #6
    Join Date
    Apr 2009
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: absolute beginner - php mysql

    For further reference, trying to make Habitual's answer a bit more clear...

    mysql_select_db requires a database, which seems to be lancasterfamily. That database has tables (pgv_individuals, at least). You need to select database first for mysql to know what tables should be queried. lancasterfamily.pgv_individuals is not a database, it is a table (pgv_individuals table of lancasterfamily database). Therefore...

    Code:
    mysql_select_db('lancasterfamily') or die('couldn't select db');
    When you are ready (the database is selected), you can start queries. Both of these are ok...they are, in fact, the same query:

    Code:
    $query = "SELECT * FROM pgv_individuals";
    and

    Code:
    $query = "SELECT * FROM lancasterfamily.pgv_individuals";
    If you were to try querying the following:

    Code:
    $query = "SELECT * FROM someotherfamily.pgv_individuals";
    it would not work. Why? Becase you have selected and opened the lancasterfamily database, not someotherfamily database (should it even exist). You need to do your stuff (queries) in the lancasterfamily database first, close it, and open someotherfamily database (using mysql_select_db) and do your queries there.

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
  •