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...
Related
I have a mysql database running on my local machine and I use wamp. I can login with phpmyadmin. Everything looks fine, create tables and users.
When I try to connect with php I get the message connection refused. The same when I try 127.0.0.1/connect.php in my browser. If I have a wrong user in my php file I get the message that user ist wrong.
Here my connect.php
<?php
$conn = mysqli_connect("127.0.0.1", "root", "", "users");
if ($conn) {
die('no connection: '. mysqli_connect_error());
}
echo 'connected';
mysqli_close($conn);
?
I tried everything, localhost, IP, 10.0.2.2, but nothing works. I also checked mysql settings and I read most of the posts here.
I solved the problem.
With this code it works perfect
<?php
$conn = new mysqli("127.0.0.1", "root", "", "users");
if (mysqli_connect_errno()) {
echo "no connection " . mysqli_connect_error();
}
echo 'conneted';
mysqli_close($conn);
?>
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");
I am creating an install php script that creates some tables in a database and creates a config file. Everything works but I don't know how to check if their host, username and password are correct. I currently check if the database is there or not.. any help would be appreciated. This is on a WAMP server using PHP 5.4.12. The variables come from a basic form.
session_start();
$_SESSION['dbdatabase'] = $_POST['dbdatabase'];
$_SESSION['dbhost'] = $_POST['dbhost'];
$_SESSION['dbusername'] = $_POST['dbusername'];
$_SESSION['dbpassword'] = $_POST['dbpassword'];
$conn=mysqli_connect($_SESSION['dbhost'],$_SESSION['dbusername'],$_SESSION['dbpassword']);
if(!mysqli_select_db($conn, $_SESSION['dbdatabase']))
{
die ('Database does not exists. Please create a database before installing Pazzilla BackOffice.');
}
else
{
The function you are looking for is mysqli_connect_error().
<?php
$link = mysqli_connect('localhost', 'invalid_user', 'password');
if (!$link) {
die('Invalid database credentials. Mysql said: ' . mysqli_connect_error());
}
?>
<?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());
}
?>
I'm trying to connect to a mysql database with php and myadmin. I've tried a lot of codes I could find online, but I just can't put this thing to work...
Can anyone please tell me what I might be doing wrong?
this is the php script I am using:
<?php
$useremail = $_POST["useremail"];
$password = $_POST["password"];
if($useremail && $password){
// open database
$connect = mysql_connect("localhost", "carlos", "nenem");
if(!$connect){
die ("Not able to connected to the database: " .mysql_error());
}
// select database
$select_db = mysql_select_db("vergilioDB", $connect);
if(!$connect_db){
die("Not able to connect to the database: " .mysql_error());
}
mysql_close($connect);
} else {
die("Please enter useremail and password, or REGISTER if you are a new user!");
}
?>
Carefull, $select_db != $connect_db
The variable names are different, rewrite to:
$select_db = mysql_select_db("vergilioDB", $connect);
if(!$select_db){
die("Not able to connect to the database: " .mysql_error());
}
note that you are closing your MySQL connection regardless of anything else before you ever use it for anything..
mysql_close($connect);
You likely want to separate your connection logic from your login logic. You likely need the DB connection to validate the password no matter what and your DB connection should be the first thing you do to make sure you can even DO anything with a given page...