Jump to content

Apache and MySql


Mensuo
 Share

Recommended Posts

I’ve gotten pretty good with PHP and MySql. Up until now I was uploading everything up to my hosting server and viewing it online to test my PHP. I’ve just installed Apache, PHP, and MySql on my machine. I’m just trying to use this as a testing server.Here’s the little bit of code I’ve run so far:

<html><body><?php$con = mysql_connect("localhost","peter","abc123");if (!$con)  {  die('Could not connect: ' . mysql_error());  }if (mysql_query("CREATE DATABASE my_db",$con))  {  echo "Database created";  }else  {  echo "Error creating database: " . mysql_error();  }mysql_close($con);?> </body> </html>

And here is the output:Fatal error: Call to undefined function mysql_connect() in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 6I was getting a message saying that it couldn’t connect to the localhost. All I really need to know is how to connect and make a database. Any help anyone can give me, or even just point me to a simple tutorial that starts at the very beginning (since this is the first time I’m really dealing with my own testing server) would be greatly appreciated.Thanks, Charley

Link to comment
Share on other sites

That message means that you don't have the MySQL include installed with PHP, so the Mysql_ functions don't exist in your installed version of php. I believe all you'd have to do is download php_mysql.dll from the php.net website and alter your php.ini file with this line:extension=php_mysql.dll.But then again i've never had to hand-install MySQL, so I could be wrong.

Link to comment
Share on other sites

If you've installed PHP from the ZIP file, the php_mysql.dll should be present in the "ext" dir.Find the line in PHP starting with

extension_dir =

and change it so it says:

extension_dir = "D:\PHP\ext"

(assuming you've installed PHP in "D:\PHP\". Otherwise, replace that with the place where PHP truly is.)Next, find the line

;extension=php_mysql.dll

and remove the ";" to enable the MySQL extension, i.e. turn it to

extension=php_mysql.dll

Restart Apache (ideally with "Stop" and "Start", not just "Restart"), and try your script again.

Link to comment
Share on other sites

Thanks so much for your help! I really do appreciate it. Now I’m getting the error I was getting before (which is a step in the right direction). When I run the script the output is:Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'localhost' (10061) in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 6Could not connect: Can't connect to MySQL server on 'localhost' (10061)I’ve tried changing the mysql.default_user and mysql.default_password to fit with the script, and taken the username and password out of the script and left them blank (like the php.ini file was when I found it). I think once I can figure this one out I’ll be set.Thanks, Charley

Link to comment
Share on other sites

Alright, now PHP and MySQL are all playing nice with each other! I can create and view data through a web page. I think there is just one thing left and I’ll really be all set this time.Now what I’m trying to do is use SSH to connect to the local host so that I can run SQL statements without hard coding them into a webpage first. If I try to connect to the localhost on port 22 it tells me that ‘The host “localhost” is unreachable”. If I try to connect on port 3306 it tells me ‘Server responds “Connection closed by remote host”.’I’d really appreciate do appreciate all the help you’ve given me this far.Thanks, Charley

Link to comment
Share on other sites

Hmm - afaik you don't need to secure the connection between the web and database servers if they are on the same localhost as the data doesn't actually travel along a public connection, just along an internal socket between the two machines.

Link to comment
Share on other sites

You’ve been so much help so far and again I really do appreciate it!I can see that everything is running fine when I upload everything to the hosting server, everything does what it’s supposed to. But when I try to run it from my testing server it says “Unknown MySQL Server Host 'db123.abc.net'”. Everything works just fine if I manually change the MYSQL_CONNECT() server variable from ‘db123.abc.net’ to ‘localhost’, but of course I don’t want hard code every page before I upload it.So is there a way to rename localhost or forward any server request to localhost? Or am I just going in the wrong direction on this one?Thanks, Charley

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...