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
$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

<!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>

MySQLi has its own argument input for ports unlike MySQL where you need to include it as part of the server address. Try this

$link = mysqli_connect('localhost', 'root', 'testpassword', null, 8080);

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);

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\ est\chapter4\connect\index.php on line 5

Code that displayed error:

<?php
ini_set('display_errors', 1); 
error_reporting(E_ALL);

$link = mysqli_connect('localhost', 'root', 'testpassword', null, 8080);
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';
?>

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\ emplate” “–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?

Did you enable mysqli extension in php.ini?
uncomment
;extension=php_mysqli.dll
to
extension=php_mysqli.dll
restart apache

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?

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

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?

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.

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.

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.