[solved]Connecting to MySQL database using PHP

Alles, was MySQL betrifft, kann hier besprochen werden.

[solved]Connecting to MySQL database using PHP

Postby daso » 24. July 2011 15:37

I really didn't know which category to put this in so I apologize now for any inconvenience caused, etc.

I have installed XAMPP for Mac on my computer, and it's installed fine, I get the 'Welcome to XAMPP' page when I go to http://localhost/, I can access phpMyAdmin, etc. the installation is fine. I've made a new database in phpMyAdmin called store as it will be a database for an online store, I've also made a user and assigned full control on that database at the moment. I'm doing this all locally because I haven't got a host yet by the way, oh and by the way Apache, MySQL and FTP are all running. So I've got a config.php file where I try to connect to the database in phpMyAdmin, I've put in the code to connect to it:
Code: Select all
<?php 
$db_host = "localhost";
$db_username = "daniel";
$db_pass = "admin";
$db_name = "store";

mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");             
?>


I then made a new PHP file in the same directory which just quickly tests the connection. test.php:
Code: Select all
<?php   
require "config.php"; 
echo "<h1>Success! Connection to database established!</h1>";   
?>


When I run the PHP which parses correctly through my browser, I go to http://localhost/store/test.php I get the following message through my browser:
Object not found!

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

If you think this is a server error, please contact the webmaster.

Error 404

localhost
Sun Jul 24 15:27:06 2011
Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_perl/2.0.4 Perl/v5.10.1


If I run a basic PHP file which just echos the text hello! it works perfectly fine, it was also in the same directory which showed it was working.

It's telling me it can't find the database I believe, so i'm stuck because I can't connect to the database. Does anyone out here know how I would connect to the database I made in phpMyAdmin, which is hosted locally using XAMPP? I would like to connect to it using PHP.
Thank you in advance. Dan.
daso
 
Posts: 3
Joined: 24. July 2011 15:08

Re: Connecting to MySQL database using PHP

Postby daso » 24. July 2011 16:40

This is rather embarrassing, I just noticed I had a spelling error in the file name that I saved the config.php file I created, well not a spelling error I named it configuration.php. Please ignore this thread.
Last edited by daso on 21. June 2012 16:11, edited 1 time in total.
daso
 
Posts: 3
Joined: 24. July 2011 15:08

Connecting to MySQL database using PHP

Postby princely » 21. June 2012 10:54

hi can any one teach me the basic and how to connect. Im learning to create a website kindly help me
princely
 
Posts: 2
Joined: 21. June 2012 10:27
XAMPP Version: 1.8.2-2
Operating System: Xphistory rountisng

Re: Connecting to MySQL database using PHP

Postby daso » 21. June 2012 16:10

You need your MYSQL database connection details to hand.
Then after your <?php tag, insert the following code:

Code: Select all
mysql_connect("HOST","USERNAME","PASSWORD") or die ("Could not connect to mysql server.");
mysql_select_db("DATABASE_NAME") or die ("Could not connect to database.");


Then insert your server url where it says HOST, your mysql server username where it says USERNAME, and insert your mysql password where it says PASSWORD.
On the following line where it says DATABASE_NAME, replace it with the database you'd like to connect to.

It is important you keep all the quotes in the string.

The 'or die()' function will display that message if any error occurs connecting to the mysql server or database.
daso
 
Posts: 3
Joined: 24. July 2011 15:08

Re: Connecting to MySQL database using PHP

Postby princely » 22. June 2012 07:10

daso ! thank u so much it helped me
princely
 
Posts: 2
Joined: 21. June 2012 10:27
XAMPP Version: 1.8.2-2
Operating System: Xphistory rountisng

Connecting to MySQL database using PHP

Postby Gunjan » 01. June 2013 16:20

Hi!!!

I tried to connect to MySQL database the same way u did...but while running the php file i type localhost/test.php instead of localhost/student/test.php.....and i get the following output:
Success! Connection to database student established
but when i tried localhost/student/test.php it gives me the following error message:
Object not found!

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

If you think this is a server error, please contact the webmaster.

Error 404

localhost
Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7

i just wanted to confirm if my approach is right???

PS:"student" is the name of my database that i created in xampp
Gunjan
 
Posts: 2
Joined: 01. June 2013 16:11
XAMPP Version: 5.6.3
Operating System: Windows 7

Re: [solved]Connecting to MySQL database using PHP

Postby Altrea » 01. June 2013 16:27

Hi Gunjan,

URLs are completely independend from databases and their names.
Why do you think you should request http://localhost/student/test.php to connect to your student database?

best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 6536
Joined: 17. August 2009 13:05
XAMPP Version: 5.5.19
Operating System: W7Ux64


Return to MySQL

Who is online

Users browsing this forum: No registered users and 3 guests