SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using PHP cannot find the database but it's there

    Hello,

    Really new to using PHP. Used Java in the past, and looking to getting into becoming a web developer.

    Okay, so I have already hit several road blocks, but I managed to get past them all besides this. Using mysql, I type the command "show databases" I clearly see the database I created called "Test2". I have already established a successful connection to my server using the generic code:

    "$link = mysql_connect($hostname,$username,$password);"

    I get a successful connection. The problem is selecting the database I created. I keep getting this message " unable to find my database".

    The code I am using is pretty straightforward.
    if (!mysqli_select_db($link,'test2'))

    {

    $output = 'Unable to locate the test2 database.';

    include 'output.html';

    exit();

    Like I said, I can see it using this command in mysql "show databases"., but I am unable to figure why I am not able to connect to it, and pull data from it. It really is frustrating.

    Please can anyone show me how to do this correctly.

  2. #2
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I again attempted to adjust my code to this:
    <?php
    $username = "Jamal";
    $password = "red!2345";
    $hostname = "Jamal-pc";
    $name = mysql_connect($hostname,$username,$password)
    or die("Unable to connect to MySQL");
    echo "Connected to MySQL<br>";

    mysqli_select_db($name,"test") or die("can't find it");


    ?>

    Straightforward and simple, but I still getting an error message " mysqli_select_db() expects parameter 1 to be mysqli, resource given in C:\wamp\www\index.php on line 13.

    At this point, I am just shooting in the dark today. Really unproductive day spent all morning with "PHP" and haven't learned anything. It's really frustrating spending your day off trying to accomplish something and just completely blew the day away. Sorry to rant, but I feel better and would feel a great deal better if I understand what I am doing wrong here.

    Thanks.

  3. #3
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,387
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    The problem is that you're using functions from two different database extensions. mysql_connect is part of the mysql extension, while mysqli_select_db is from the mysqli extension. The mysql stuff is depreciated anyway and will be removed from PHP at some point, so you'd be better off sticking with mysqli:

    PHP Code:
    $link mysqli_connect($host$username$password'test2');

    /* check connection */
    if (mysqli_connect_errno()) {
        
    printf("Connect failed: %s\n"mysqli_connect_error());
        exit();


  4. #4
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your prompt response.

    Alright, so I have a successful connection to my database. How would I go about accessing it from the code you given me. As I am truly lost, at this point.

    What functions should I be using to just select the database? If you could kindly answer that, I would be so happy and salvage the rest of my day.

    Thanks

  5. #5
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,387
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by louis345 View Post
    What functions should I be using to just select the database? If you could kindly answer that, I would be so happy and salvage the rest of my day.
    The code example in my previous post connects to the DB server and selects the database you want to use. If you wanted to change to another DB after making the initial connection, then you would use the mysqli_select_db function as in your first post.

  6. #6
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hope you're still around, I am still stuck.

    I used your original code to connect to my database and I have a suspicion that something is wrong. When I run a query, it yields me nothing but just errors.

    Here is the small code I am running
    <?php
    $username = "Jamal";
    $password = "red!2345";
    $hostname = "Jamal-pc";
    $mysqli= mysql_connect($hostname,$username,$password,'japanesewords')
    or die("Unable to connect to MySQL");
    echo "Connected to MySQL<br>";
    $result = mysqli_query($mysqli, 'SELECT * FROM japanesedefinition');



    ?>
    I made use that everything is correct but I am still getting error messages: : mysqli_query() expects parameter 1 to be mysqli, resource given.

    I have tried to use google and not be lazy, but I have been at this code all day. Please let me know, if I am doing something wrong.

  7. #7
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    673
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Take a good look at fretburn's example again.

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,785
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Code:
    $username = "Jamal";
    $password = "red!2345";
    $hostname = "Jamal-pc"; 
    $mysqli= mysqli_connect($hostname,$username,$password,'japanesewords')
      or die("Unable to connect to MySQL");
    echo "Connected to MySQL<br>";
    $result = mysqli_query($mysqli, 'SELECT * FROM japanesedefinition');
    You have the highlighted character missing.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your help.

    I understand that "typo" now.

    It appears that I am still having trouble with this database. Can you look at this code:
    $test=$mysqli= mysqli_connect($hostname,$username)
    or die("Unable to connect to MySQL");
    echo 'it worked';
    $result = mysqli_select_db($test,'japanesedefinition');
    $result = mysqli_query($test,"SELECT * FROM Japanesewords");
    while($row = mysqli_fetch_array($result))
    {
    echo $row['FirstName'] . " " . $row['LastName'];
    echo "<br>";
    }

    ?>
    Alright, the error message I am getting is this:"mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\index.php on line 11"

    It's the story of my day today.

    Thanks in advance. I just want to make sure it works.

  10. #10
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    673
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    You have not defined all the connection fields.
    PHP Code:
    mysqli_connect($hostname,$username,$password,'japanesewords'

  11. #11
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So I did some error handling
    the problem appears to be is that I am able to connect to the server using my username etc, but I cannot find the database. In mysql, I am positive the table exists, but am unable to access it using my code. It's really frustrating getting this to work. It seems so straight forward where am I failing at this.

    PHP Code:
    <?php
    $username 
    "monty";
    $password "";
    $hostname "Jamal-pc"
    $test=$mysqlimysqli_connect($hostname,$username)
      or die(
    "Unable to connect to MySQL");
    echo 
    'it worked';

    if (!
    $test



      
    $output 'Unable to connect to the database server.'

      include 
    'output.html'

      exit(); 

    }
    mysqli_select_db($test,'japanesedefinition');
    if(!
    mysqli_select_db($test,'japanesewords'));{
     
    $output 'Unable to locate the  database.'

      include 
    'output.html'

      exit(); 
    }
    ?>
    It fires the error, but I am to new at this to really have a deep insight on what I am doing here. It also sucks that I don't have a teacher to ask. So I give you a million thanks for helping me. It really does mean a lot.
    Last edited by SpacePhoenix; Sep 5, 2013 at 23:07. Reason: placed php tags around php code

  12. #12
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,785
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You are still failing to include ALL the parameters needed by the connect statement. The first THREE are mandatory and you are only supplying the second and third.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  13. #13
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have since correct that mistake
    $test=$mysqli= mysqli_connect($hostname,$username,$password)
    or die("Unable to connect to MySQL");
    echo 'it worked';

    if (!$test)

    {

    $output = 'Unable to connect to the database server.';

    include 'output.html';

    exit();

    }
    mysqli_select_db($test,'japanesewords');
    if(!mysqli_select_db($test,"test"));{
    $output = 'Unable to locate the database.';

    include 'output.html';

    exit();
    }
    ?>

  14. #14
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    673
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    And how do you do with this simple test
    PHP Code:
    <?php    
    $mysqli 
    mysqli_connect($hostname,$username,$password,'japanesewords');
    if (
    mysqli_connect_errno()){
        
    $output 'Unable to connect to the database server.'
        include 
    'output.html';
        exit();
    }

    $query "SELECT * FROM japanesedefinition";
    $result $mysqli->query($query);
    while (
    $row $result->fetch_array(MYSQLI_ASSOC)){
        echo 
    "<pre>";
        
    print_r($row);
        echo 
    "</pre>";
    }
    ?>

  15. #15
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it worked, but how?

    I am so happy, but I need to understand how and why it worked? I think this presents me with a tremendous opportunity to learn.

    Thanks

  16. #16
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,785
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The hostname parameter tells the script which computer to look at to find the database.

    localhost identifies the computer as the one the script is running on but provided that the appropriate port is open on the computer running the database you can remote connect to it using a script running on a different computer by specifying an appropriate value in hostname to identify that remote computer.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  17. #17
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I understand that. My problem is I don't understand Drummin code at all. Granted, I been looking at PHP for 3 days now, but all examples I see online don't use the functions he is using. I have no idea what this mean" $result = $mysqli->query($query);" and this "while ($row = $result->fetch_array(MYSQLI_ASSOC))". Specifically, I don't understand the what is placed in the constructor of the array. For the examples, I have seen online, $result should be placed on the constructor. If you could kindly point me to the direction on where I can familiarize myself with this code or if you could explain to me like I was a 2 year old. I really want to know this. It's 3 am in the morning my time and I won't be able to sleep.

  18. #18
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did some digging is he using php like an object oriented language PDO to be precise. Using Java and looking at some notes online it is making a lot more sense.

    If there is anything you could add, I would greatly appreciate it.

    I do need to get to sleep.

    THANK YOU ALL. If I was a millionaire, I would def. buy everyone here a new computer. I really do appreciate this. Not sure on how else to show my gratitude online, but to say thanks a million times.


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
  •