Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 8 of 8
  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    24
    Member #
    36599
    Liked
    1 times

    trying to use php to connect to mySQL

    Here's the php portion of my web page (with the user name and password x'ed out):

    <?php
    echo '6';
    echo '<br>';
    echo 'Trying to connect to query the mySQL server';
    echo '<br> <br>';

    define ('DB_USER', 'xxxxxxxxxxxxxxxx');
    define ('DB_PASSWORD', 'xxxxxxxxxxxxxx');
    define ('DB_HOST', 'localhost');
    define ('DB_NAME', 'bluesmat_test');

    $dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
    mysql_select_db (DB_NAME);

    //my first query
    $result = mysql_query ("SELECT * FROM USstates");

    if ($result) {
    echo 'It worked!!';
    /* <p><?= $row['state_name'] ?></p>
    <p><?= $row['state_abbreviation'] ?></p>
    <p><?= $row['capital'] ?></p>;
    */
    } else {
    echo 'There was a problem!';
    }
    ?>

    When I run this, 'There was a problem!' is displayed so it seems like $result is not being set. No other warnings or errors are displayed. When I query the USstates table using phpAdmin, I can see the records.

    The commented out code is what I need to fix next. There, I'm just trying to display the records found in the table.

    Thanks for the help.

  2.  

  3. #2
    Junior Member
    Join Date
    Jun 2013
    Posts
    24
    Member #
    36599
    Liked
    1 times
    Here's the updated code:

    <?php
    echo '9';
    echo '<br>';
    echo 'Trying to connect to query the mySQL server';
    echo '<br> <br>';

    define ('DB_USER', 'xxxxxxxxxxxxxxxx');
    define ('DB_PASSWORD', 'xxxxxxxxxxxxxxx');
    define ('DB_HOST', 'localhost');
    // define ('DB_NAME', 'bluesmat_test');

    $dbhandle = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die("Unable to connect to MySQL");
    echo "Connected to MySQL<br>";
    $selected = mysql_select_db ('bluesmat_test', $dbhandle) or die("Could not select bluesmat_test");

    //my first query
    $result = mysql_query ("SELECT * FROM USstates");

    if ($result) {
    echo 'It worked!!';
    /* <p><?= $row['state_name'] ?></p>
    <p><?= $row['state_abbreviation'] ?></p>
    <p><?= $row['capital'] ?></p>;
    */
    } else {
    echo 'There was a problem!';
    }
    ?>

    When I run this, I get as far as "Connected to MySQL" then "Could not select bluesmat_test" is displayed.

    Not sure what is wrong.

  4. #3
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,483
    Member #
    425
    Liked
    2783 times
    You don't have a bluesmat_test database in your MySQL. Maybe you should use bluesmatt​_test instead?
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  5. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    24
    Member #
    36599
    Liked
    1 times

    Question

    Thanks for reply Game.

    There is a database name 'bluesmat_test' (one 't' in bluesmat) with one table named 'USstates'. There is a user named 'bluesmat_matt' (one 't' in bluesmat) with all privileges. I started using the 'i' version of 'mysqli_connect' but the error still displays.

    Here's the current version of the code (with my password x'ed out):

    <?php
    echo '11';
    echo '<br>';
    echo 'Trying to connect to query the mySQL server';
    echo '<br> <br>';

    $dbhandle = mysqli_connect ('localhost', 'bluesmat_matt', 'xxxxxxxxxxx') or die("Unable to connect to MySQL");
    echo "Connected to MySQL<br>";
    echo '<br>';
    $selected = mysql_select_db ('bluesmat_test', $dbhandle) or die("Could not select bluesmat_test");
    echo "Database selected.";

    /* my first query - show tables */
    $result = mysql_query('SHOW TABLES',$dbhandle) or die('cannot show tables');
    if ($result) {
    echo 'first query worked!!';
    } else {
    echo 'There was a problem with the first query!';
    }

    // my 2nd query
    $result = mysql_query ("SELECT * FROM USstates");

    if ($result) {
    echo 'second query worked!!';
    } else {
    echo 'There was a problem with the 2nd query!';
    }
    ?>

    --------------------------------------------------------------------
    When I run this, here's the output:

    11
    Trying to connect to query the mySQL server

    Connected to MySQL


    Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home3/bluesmat/public_html/test2.php on line 22
    Could not select bluesmat_test
    ---------------------------------------------------------------

    There are only 2 arguments to the mysql_select_db(), 'bluesmat_test' and $dbhandle. I can't see how the database name is wrong (see attached screen shot). I'm not sure about $dbhandle.
    Attached Images Attached Images

  6. #5
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,483
    Member #
    425
    Liked
    2783 times
    I'm not a PHP guy, so this is just a guess on my part...but what happens when you change mysql_select_db to mysqli_select_db ?
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  7. #6
    WDF Staff AlphaMare's Avatar
    Join Date
    Oct 2009
    Location
    Montreal, Canada
    Posts
    4,570
    Member #
    20277
    Liked
    878 times
    You have a mixture of single and double quotes in your code - sing single quotes in an "echo" will print the variable name, not the value.

    Here's something to try and see if the db is being read - t
    ry this code after connecting to your database:

    $result = mysql_query("show tables"); // run the query and assign the result to $result
    while($table = mysql_fetch_array($result)) { // go through each row that was returned in $result
    echo($table[0] . "<BR>"); // print the table that was returned on that row.}
    Good design should never say "Look at me!"
    It should say "Look at this." ~ David Craib


    http://digitalinsite.ca ~ my current site . . info@digitalinsite.ca ~ my email

    If you feel that someone's post helped you fix your problem, answered your question, or just made you feel better, feel free to "Like" their post. The "Like" link is at the bottom right of each post, along side the "reply" link. And if you are being helped here, try to help someone else - pass it on!

  8. #7
    Junior Member
    Join Date
    Jun 2013
    Posts
    24
    Member #
    36599
    Liked
    1 times
    Thanks for the help. The script is working now! Here's the updated code (with my password x'ed out). Notice no 'i' in the mysql_connect function and double quotes used everywhere.:

    <?php
    echo "My version";
    echo "<br>";
    echo "Trying to connect to query the mySQL server";
    echo "<br> <br>";

    $dbhandle = mysql_connect ('localhost', 'bluesmat_matt', 'xxxxxxxxxxxxx') or die("Unable to connect to MySQL");
    echo "Connected to MySQL<br>";
    echo "<br>";
    $selected = mysql_select_db ('bluesmat_test', $dbhandle) or die("Could not select bluesmat_test");
    echo "Database selected.";
    echo "<br>";

    /* my first query - show tables */
    $result = mysql_query('SHOW TABLES',$dbhandle) or die('cannot show tables');
    while($table = mysql_fetch_array($result)) {
    echo($table[0] . "<BR>");
    }
    ?>

    Here's the output (there are 2 tables in the database now):

    My version
    Trying to connect to query the mySQL server

    Connected to MySQL

    Database selected.
    UScities
    USstates

  9. #8
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    You can't mix MySQL with MySQLi

    We should all be using MySQLi from now on, as MySQL will be deprecated in the future.

    So, do a Google search for MySQLi tutorial, or view the php manual.
    PHP: Mysqli - Manual


    Here is the introduction to MySQL ... you'll see their deprecation warning about using MySQL:
    http://www.php.net/manual/en/intro.mysql.php
    Last edited by mlseim; Jul 05th, 2013 at 11:49 AM.



Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:48 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com