This is a simple program to test your connection towards your host.
<?php
//This is a comment. This line is to connect to apache.
if(!mysql_connect("localhost", "root", ""))
{
//If above statement returns any error then it will enter this control statement
echo "This is an error".mysql_error();
//mysql_error() is an inbuilt function which returns the error you have encountered.
}
else
{
//This will be excuited if mysql_connect doesn't return any error.
echo "Connected to your host correctly";
}
?>
Explanation:: How to connect to host? So you use these following inbuilt function called mysql_connect().
mysql_connect() generally has 3 parameters. mysql_connect(“YourHostName”,”Username”,”Password”);
If you are using Localhost then
YourHostName = localhost
Username = root
password = (empty by default)
So, this function becomes as
mysql_connect(“localhost”,”root”,”");
In depth, mysql_connect() returns a non-zero value on success and on failure returns zero with error. So, if this function fails it satisfies ‘ if ‘ condition. So, it enter if statement(If any error occurs while connecting to the host it will return a zero. and Not of zero is one. So, it enter if statement). As an error is generated it will be displayed in echo statement. mysql_error() returns the error occured while connecting to host. We write echo statement as
error “Occured error is:”.mysql_error();
You should have observed a dot(.) begin placed after double-quotes( ” ). This is to follow the rules. Else it will return an error. So, you need to follow the format very strictly.






10 Comments on "connect to host in php"
Very interesting info !Perfect just what I was searching for! “The right to be heard does not autmatically include the right to be taken seriously.” by Hubert Humphrey.
usually articles a few extremely exciting stuff like this. If you’re new to this site.
It is really a great and useful piece of info. I’m happy that you simply shared this helpful info with us. Please keep us up to date like this. Thank you for sharing.
F*ckin’ tremendous things here. I’m very happy to look your post. Thank you a lot and i am looking forward to touch you. Will you kindly drop me a e-mail?
What’s Happening i am new to this, I stumbled upon this I’ve found It absolutely useful and it has helped me out loads. I hope to contribute & aid other users like its helped me. Great job.
Hey there! I’ve been reading your web site for some time now and finally got the courage to go ahead and give you a shout out from Porter Tx! Just wanted to tell you keep up the excellent job!
“Thank you, nice post! This was the thing I needed.”
I simply want to tell you that I am all new to blogging and site-building and certainly loved your blog. Probably I’m planning to bookmark your website . You definitely come with superb posts. Regards for revealing your website page.
I am apart of a commitee that is trying to start a clothing company. I am in charge of designing the clothing website because for right now you can only order stuff online. But I was just wondering how do you start a website for a clothing company? What are some good domain places to sign up for a domain..
Just wanted to give you a shout from the valley of the sun, great information. Much appreciated.