Basic PHP/MySql Connection (1 Viewer)

speedman_2001

Registered User.
Local time
Today, 16:48
Joined
May 23, 2008
Messages
30
Ok guys been working on this for a bit now and keep running into different issues. First the basis of everything. Building a web page in Expression Web 4 running the development server for testing. Using PHP 5.2.17 currently. PHP code being used is pretty basic and is posted below. The issue I am currently running into is that when I preview the web page I am presented with a blank screen. I don't get a connection error or pass. If I try to view the phpinfo() within the php it comes back correctly so the php portion is processing correctly. At one point I was using PHP 5.3, but since our hosting service for our MySql server doesn't accept 5.3 due to password authentication issues I downgraded to 5.2.

Any thoughts on what I'm doing wrong?

PHP:
<?php
$username = 'username';
$password = 'password';
$hostname = 'hostname';
$conn = mysql_connect($hostname, $username, $password);
    if (!$conn)
    {
        die(mysql_error());
    }
    else
    {
        echo 'Connected to MySQL';
    }
    mysql_close($conn);
?>
 

peregrin

Registered User.
Local time
Today, 17:48
Joined
Feb 9, 2012
Messages
13
Can you independently connect to the MySQL server to make sure it works on its own? For kicks you might try mysqli functions instead of the older mysql functions - if this is a new project it may be best to start out with mysqli anyway since that will be better in future (with unicode support, particularly). Though its probably not important I'd put some kind of print statement in there just so you at least see that something is working:

echo 'Hello world';
if mysql.connect() ...

If this is a local development server sometimes using localhost rather than 127.0.0.1 works better (or vice versa - I've forgotten which).
 

speedman_2001

Registered User.
Local time
Today, 16:48
Joined
May 23, 2008
Messages
30
Hey guys sorry for the delay and thank you for the responses.


Peregrin: I’m currently using an Access program as the front end to the same SQL server using an ODBC Driver. This works correctly on multiple computers but we’re trying to get away from the ODBC and Access for this and go to web interface. Also, the same configuration I mentioned earlier works correctly when uploaded and run off of our hosting service’s server. They’re running an apache server with PHP 5.2.17 as well. As for the echo, when I take out the connection information in the PHP and only put an echo in place as you suggested it works just fine. My echo shows up on screen, same with PHP’s getinfo(), all information is returned. When the connection info is still in the PHP, if I put an echo before after the variable declaration but before the connection attempt, the echo prints fine. If the echo is placed anywhere after the attempt to connect I do not see it. Appears there is an issue when trying to make the connection. Correct I’m using a local dev server, default is localhost but when replacing same issue appears. I’m really new to building web pages and using PHP so there is quite a bit of a learning curve when it comes to all of this. As soon as I get a chance I’ll check out mysqli and get back to you guys. Thanks for the help.
 

speedman_2001

Registered User.
Local time
Today, 16:48
Joined
May 23, 2008
Messages
30
Ok had a chance to work on mysqli and appears I'm getting the same issue as before, screen just shows up white with no information on the connection. Any thoughts?
 

peregrin

Registered User.
Local time
Today, 17:48
Joined
Feb 9, 2012
Messages
13
One thing to be sure to to is try to get a direct command prompt on your mysql server by itself - just to be sure it's working on its own, and that your username, password is correct, and so on. I'm not an expert (I do have a LAMP server running at home with PHP and mySQL that I've played around with). Your phpinfo() page should give you some information about the mysql server (which is mostly cryptic to me, but should at least show whether php is picking up the mysql server at all). Have you already created the database in MySQL? Do you have a user other than root?
 

speedman_2001

Registered User.
Local time
Today, 16:48
Joined
May 23, 2008
Messages
30
Peregrin: Thanks for the help man and sorry for getting back to you so late. Never got it working directly on my PC/Server, but as soon as I upload my files to our hosting site it works flawlessly. A little annoying at times but good ol FTP helps out with that. Thanks again.
 

Users who are viewing this thread

Top Bottom