SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help connecting to MySql from PHP

    Hello all,

    Please be patient with me as I try to explain my problem, I'm a total newb to all of this. I could really use some assistance on this as I have searched for days trying to find the answer and have failed to find the answer.

    The setup you need to know:

    1) Apache Server is installed and up and running fine
    2) MySql service is running
    3) I can use mysql through command console to see my DB and tables
    4) I'm running on Windows XP Media Edition

    What's happenning:

    As I work my way through the tutorials in the SitePoint Book titled "Build your own Database Driven Web Site, Using PHP and MySQL" everything worked fine; installation of Apache, installation of MySQL, and even the chapters on adding tables..etc. The problem occurs when I get to the part where I have to connect to MySql through PHP.

    When I type http://localhost:8080/test/chapter4/connect and hit enter in the browser I get nothing...ziltch...nada...If I view source it's empty. I've tried variations of connections and still I'm getting nothing. HALP! what am I doing wrong? For those of you familiar with the book I'm stuck on page 121.

    I suspect the problem is something related to the root of mySql but I haven't been able to figure out what. Is there something I have to do with mySql to tell it the root?

    Two files (located at htdocs/test/chapter4/connect)

    the scripts:

    index.php


    PHP Code:
    <?php
    $link 
    mysqli_connect('localhost:8080''root''testpassword');
    if (!
    $link)
    {
        
    $output 'Unable to connect to the database server.';
        include 
    'output.html.php';
        exit();
    }

    if (!
    mysqli_set_charset($link'utf8'))
    {
        
    $output 'Unable to set database connection encoding.';
        include 
    'output.html.php';
        exit();
    }

    if (!
    mysqli_select_db($link'ijdb'))
    {
        
    $output 'Unable to locate the joke database.';
        include 
    'output.html.php';
        exit();
    }

    $output 'Database connection established.';
    include 
    'output.html.php';
    ?>

    output.html.php


    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
            <title>PHP Output</title>
            <meta http-equiv="content-type"
                    content="text/html; charset=utf-8"/>
        </head>
        <body>
            <p>
                <?php echo $output?>
            </p>
        </body>
    </html>
    Last edited by SpacePhoenix; Dec 11, 2010 at 17:19. Reason: put php tags round php code to improve readability

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    MySQLi has its own argument input for ports unlike MySQL where you need to include it as part of the server address. Try this

    PHP Code:
    $link mysqli_connect('localhost''root''testpassword'null8080); 

  3. #3
    SitePoint Enthusiast hostentic's Avatar
    Join Date
    Oct 2010
    Location
    USA
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also for future reference you might want to add this at the beginning of your php file so if you encounter any errors they will be displayed :

    ini_set('display_errors', 1);
    error_reporting(E_ALL);

  4. #4
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sgtlegend...I understand what you're saying and changed locahost:8080 to localhost in the PHP code and still I get nothing...I don't even get the error messages for not making a connection.

    Using at top of index.php:

    ini_set('display_errors', 1);
    error_reporting(E_ALL);

    I get the following error message:

    Fatal error: Call to undefined function mysqli_connect() in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\chapter4\connect\index.php on line 5


    Code that displayed error:

    PHP Code:
    <?php
    ini_set
    ('display_errors'1); 
    error_reporting(E_ALL);

    $link mysqli_connect('localhost''root''testpassword'null8080);
    if (!
    $link)
    {
        
    $output 'Unable to connect to the database server.';
        include 
    'output.html.php';
        exit();
    }

    if (!
    mysqli_set_charset($link'utf8'))
    {
        
    $output 'Unable to set database connection encoding.';
        include 
    'output.html.php';
        exit();
    }

    if (!
    mysqli_select_db($link'ijdb'))
    {
        
    $output 'Unable to locate the joke database.';
        include 
    'output.html.php';
        exit();
    }

    $output 'Database connection established.';
    include 
    'output.html.php';
    ?>
    Last edited by SpacePhoenix; Dec 11, 2010 at 17:20. Reason: put php tags round php code to improve readability

  5. #5
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have checked the following things:

    php info

    PHP Version 5.2.14

    System Windows NT MASKED 5.1 build 2600
    Build Date Jul 21 2010 18:41:42
    Configure Command cscript /nologo configure.js "--enable-snapshot-build" "--enable-debug-pack" "--with-snapshot-template=d:\php-sdk\snap_5_2\vc6\x86\template" "--with-php-build=d:\php-sdk\snap_5_2\vc6\x86\php_build" "--with-pdo-oci=D:\php-sdk\oracle\instantclient10\sdk,shared" "--with-oci8=D:\php-sdk\oracle\instantclient10\sdk,shared" "--without-pi3web"
    Server API Apache 2.0 Handler
    Virtual Directory Support enabled
    Configuration File (php.ini) Path C:\WINDOWS
    Loaded Configuration File C:\Program Files\PHP\php.ini
    Scan this dir for additional .ini files (none)
    additional .ini files parsed (none)
    PHP API 20041225
    PHP Extension 20060613
    Zend Extension 220060519
    Debug Build no
    Thread Safety enabled
    Zend Memory Manager enabled
    IPv6 Support enabled
    Registered PHP Streams php, file, data, http, ftp, compress.zlib
    Registered Stream Socket Transports tcp, udp
    Registered Stream Filters convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, zlib.*


    ***************

    I also checked C:\WINDOWS\system32\drivers\etc\host to ensure that localhost wasn't commented out or something..it's fine and set to 127.0.0.1

    I cannot for the life of me figure out why I can't get connected...

    Apache is running, MySql Service is running...everything looks good

    Any other thoughts?

  6. #6
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you enable mysqli extension in php.ini?
    uncomment
    ;extension=php_mysqli.dll
    to
    extension=php_mysqli.dll
    restart apache
    my mobile portal
    ghiris.ro

  7. #7
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep...

    checked the php.ini file and
    it is not commented out

    extension=php_mysqli.dll


    why am I getting a fatal error saying undefined function call..isn't mysqli_connect a built in function of PHP?

  8. #8
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    php_mysqli.dll is an extension

    are sure that the mysql server is running on port 8080?
    the default port for mysql is 3306
    usually, the 8080 port is used when the 80 is blocked by isp
    my mobile portal
    ghiris.ro

  9. #9
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it is running on 3306...so how do I change it? The book says nothing about having to change the port your MySql uses. Did the author forget to mention that critical piece of information?

    Ok...here's the deal

    I was anticipating the future installation of IIS to learn ASP and told apache to use port 8080. So if my mySql is using port 3306 and Apache is using 8080 is there something not communicating? I'm referencing my PHP pages at http://localhost:8080/... do I need to change something so I can get connected to the MySql database server?

  10. #10
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no, you don't have to change anything.

    if the mysql server is running on default port (3306), you don't have to put the optional port argument in mysqli_connect function.
    my mobile portal
    ghiris.ro

  11. #11
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've had all sorts of problems when i was setting this up originally. I think what worked for me was making sure i was running the text editor as 'admin'. i think anything else saved int he htdocs floder just isnt visible.

    So open your text editor with 'run as administrator' and do exactly what it says in the book.

  12. #12
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well...I have no idea what the author is trying to teach us by having us install Apache, SQL and PHP separately...I finally gave up on his approach, uninstalled everything, downloaded XAMP, installed PHP, MySql, and Apache. Then I went back and rebuilt the database using their WYSIWYG editor and made my connection to the mySQL database in about five minutes. At least now I can proceeed to the next part of the book...learning PHP. I guess I'll just have to forgoe the parts on using mySQL admin through the cmd cosole and modify my tables through XAMPs MySql Admin/Editor. I can't seem to pull up anything using mysql through a command line console.

    so...I've got it working with the exact same code from above. All I did was uninstall everything the book told me to do and downloaded XAMP and installed PHP, MySql, and Apache with their package installer. It also provides a much more user friend interface for me to interact with the applications/servers.


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
  •