I am new to programming. I have a website on iPage. Now, I am learning PHP and one of the things I am learning is to connect PHP to mySql database. I am using the following:
mysql_connect(host name, username, password)
My question is, why I am not getting an error?
no matter what username and password and even host name i enter, it just accepts it!
This is the code I am trying (the username and password are just as example)
<?php
mysql_connect('ipage','admin','password');
echo 'Connected!';
?>
when I run it, it just says connected even though my username and password are not admin, password.
Use mysqli_*, beacuse mysql_* is deprecated and will be removed in the future:
$conn = mysqli_connect('localhost', 'username', 'password');
if(mysqli_connect_errno($conn))
{
die('Error in connection to MySQL: ' . mysqli_connect_error());
}
else
{
echo 'Connected successfully';
}
You are not checking if you are connected.
You need to use something like this:
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
My suggestion is to start reading some docs, they have some great examples and you can really learn a lot. DOCUMENTATION
Related
I am trying to build a dynamic website for connecting pages with database I used the code as follows. connection with server is Ok but unable to select database. data base name, user id, password, ip of host all gave but not working. please help....
define('DB_NAME', '');
define('DB_USER', '');
define('DB_PASSWORD', '');
define('DB_HOST', '');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
else {
echo 'connected to server..................'; }
$db_selected = mysql_select_db($DB_NAME, $link);
if ($db_selected) {
print "Database Found";
}
else {
print "Database NOT Found";
}
Mysql has been deprecated and will eventually be removed. Consider using PDO or MySqli.
Here is a link to the PHP documentation page for making connections to a database using MySqli.
http://php.net/manual/en/mysqli.quickstart.connections.php
If you follow the instructions carefully you 'should' be able to connect. If not, perhaps you can post the error message you receive.
Try to give proper credential to connect to mysql server. mysqli_connect("localhost","root","password","db_name");
Even if I have used LAMPP many times, this time something goes wrong. When I visit the browser(chrome) nothing echos. Here is my code:
index.php
<?php
error_reporting(E_ALL); /*after edit*/
$link = mysqli_connect('localhost', 'root', 'root', 'db');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>
Do I miss something? The output is nothing. By the way i write my files in
var/www/html/my_pages
and i call it this way: localhost/my_pages. Simple echos are working and php in general is fine. Something goes wrong with my db connection.
Use this code
<?php
$link = mysqli_connect('localhost', 'root', 'root', 'db');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
else
{
echo 'Connected successfully';
}
?>
Yes probably, because mysqli_connect() method return the object, not Boolean value.
You can verify the connection with the following code:
if($link->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
return false;
}
else{
echo "Connection Successful";
return $link;
}
Why not using PDO.
$dsn = "mysql:host=localhost;dbname=db";
try {
$pdo = new PDO($dsn, "root", "");
} catch (PDOException $ex) {
$ex->getMessage();
}
with PDO you can change anytime you need the Db vendor:
http://php.net/manual/en/book.pdo.php
<?php
echo phpinfo();
?>
Run this file and get all PHP and apache details. Search for mysqli support in it. If it is supported, you should have something like below.
Also check for your root directory
Thanks everyone for the response! I found the problem. Something went wrong and mysqli wasn't enabled in php. That's why i had this error Fatal Error:Call to undefined function mysqli_connect() in /var/www/html/diamond/index.php on line 8 I reinstalled php and problem solved :)
I creat a database in cpanel and write small php code.
<?php
$dbhost='localhost';
$dbuser='-------';
$dbpass='*******';
$db = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_set_charset('utf8',$db);
if(!$db){
echo 'can not connect to server';
}
else{
$database = '--------_ps';
$select = mysql_select_db($database);
if($select){
echo 'database selected. <br/>';
}
else{
echo 'database not select. <br/>';
}
mysql_close($db);
}
?>
I see database not select, When I change --------_ps database to phpmyadmin data base 'Database operations
information_schema' echo database selected.
I am not beginner on php but I don't know any way to solve this.
Please help me.
Try this code -witch also provides some basic feedback as to what errors your script may encounter:
<?php
...
$conn = #mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect to the Database Server");
mysql_set_charset('utf8', $conn );
mysql_select_db($database, $conn) or die("Could not find the Database");
...
?>
Now, the above script works. If you still encounter problems, can you please give some feedback with the error log of your web server?
One more thing: Make sure you have created the database correctly with the phpMyAdmin, AND also have set the user with the credentials you are using...
This is the code to connect to the mysql database:
$con = mysql_connect("", "", "");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("", $con);
I want to echo 'connected'/'disconnected' depending on state.
How can it be done?
Do it Like this
if ($con) {
echo 'connected';
} else {
echo 'not connected';
}
Or try this
echo $con ? 'connected' : 'not connected';
Firstly, use the mysqli_xxx() functions instead of the old obsolete mysql_xx() functions.
This is strongly recommended anyway because the old library is in the process of being deprecated, but will also make your question easier to answer. (you could also use the PDO library, for which the answer will be similar, but for this answer I'll stick with mysqli for simplicity)
With the mysqli library, you get a variable that contains your DB connection, which you can examine at any point.
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
Now, you can query the $mysqli variable to find out what is happening.
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
Later on, you can query the variable using the ping command: http://www.php.net/manual/en/mysqli.ping.php
$mysqli->ping();
or maybe the stat command if you want more info: http://www.php.net/manual/en/mysqli.stat.php
Try using mysql_ping
mysql_ping — Ping a server connection or reconnect if there is no
connection
something like this should help you
if(mysql_ping($con))
echo 'connected';
else
echo 'disconnected';
<?php
$cons=mysql_connect("localhost","root","");
mysql_select_db("infogallery") or die('Not Connected to the data base:'.mysql_error());
?>
I write above code for connection with mysql but when i run this scripts..nothing display on the brouser...what can i do for the connection with mysql....
If nothing is displayed, then it means it succeeded. Add more code which queries the database and displays some results.
Don't connect as the root account. Create an account specifically for playing around with.
Once you've done that, modify your code as follows:
$cons = mysql_connect('localhost', 'username', 'password');
if ($cons === FALSE) {
die("Failed to connect to MySQL: " . mysql_error());
}
mysql_select_db(etc.....);
You don't check if the connection failed, then try to do a database operation on that potentially failed connection. The or die(...) you have will only show the error caused by the select attempt, and the error message from the failed connection will be lost.
I like to just do
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("infogallery") or die(mysql_error());
echo "So far, so good.";
How about something like the following:
<?php
try {
$cons = mysql_connect("localhost","username","password");
} catch ($e) {
die('Failed to connect to the database: ' . mysql_error());
}
try {
mysql_select_db("infogallery", $cons);
} catch ($e) {
die('Failed to select the database: ' . mysql_error());
}
?>