I've installed a WAMP server. Now I want to connect to a MySQL database using php. I have one problem you need to get the username.
This is the code I use. Its from php.net http://php.net/manual/en/mysqli.quickstart.connections.php
<?php
$db_host = "localhost";
$db_username = "root";
$db_password = "password";
$db_name = "test";
$mysqli = new mysqli($db_host, $db_username, $db_password, $db_name);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
?>
I get the following error:
Warning: mysqli::mysqli(): in C:\wamp\www\Atletieker\dbconnect.php on line 7
Failed to connect to MySQL: (1045) Accès refusé pour l'utilisateur: 'root'#'#localhost' (mot de passe: OUI)
Warning: main(): Couldn't fetch mysqli in C:\wamp\www\Atletieker\dbconnect.php on line 11
How do I get the username of my database?
Try this...
$db_host = "127.0.0.1";
$db_username = "root";
$db_password = "";
$db_name = "test";
Obviously the $db_password should be empty, if you haven't changed any settings the default password should be blank.
Related
Connection output failed I already change the quotations but still it failed
This is my code in config.php
<?php
$host = "localhost";
$username = "root";
$password = "";
$db_name = "db_airlines'";
try{
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$db = new PDO('mysql:host= ' . $host . ';dbname=' . $db_name . ';charset=utf8', $username, $password);
echo $host;
}
catch(Exception $e){
echo "Failed to connect to database";
} ?>
failed output prompts: Warning: PDO::__construct():
php_network_getaddresses: getaddrinfo failed: No such host is known
but if I change it to $db = new PDO('mysql:host=localhost'; $username, $password); // this works.
Why is that?
$db = new PDO('mysql:host=' . $host . ';dbname=' . $db_name . ';charset=utf8', $username, $password);
The Space before your host name is causing this issue.
I am trying to connect mysql database to a php program. I used following program to establish connection. But it is not giving any error even I make any mistake with the code.
<?php
// creating database connection
$dbhost ="localhost";
$dbuser = "root";
$dbpass = "1234";
$dbname = "my_new";
//connection
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
//testing connection
if(mysqli_connect_errno()){
die ("database connection failed :".
mysqli_connect_error() .
"(".mysqli_connect_errno().")"
);
}
?>
In my computer, your code is work well. And I find that mysql_connect will not throw exception.
my I am trying to make login registration page in php.
apache and mysql is up and running.
I am executing the following code
<?php
$db_host = "localhost:777";
$db_username = "root";
$db_pass = "";
$db_name = "login";
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 am using localhost:777 because port 80 is being used by skype.
phpMyAdmin is up and running.
The output i am getting is nothing but the above code only.
can anyone help me on this issue?
Use MySQLi or PDO not MySQL.
You're passing the variables as a string using " " in the mysql_connect function
The syntax is as follows: mysqli_connect(host,username,password,dbname,port,socket);
<?php
$db_host = "localhost";
$db_port = "777";
$db_username = "root";
$db_pass = "";
$db_name = "login";
$conn = mysqli_connect($db_host,$db_username,$db_pass,$db_name,$db_port);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try that and it should work. Please make sure you understand the code above and the error you made. Do not simply copy and paste please you will not learn from it.
I am fresher for Open shift.
Here is my problem in Open shift.
I completed creating application with php, Mysql and PhpMyAdmin
Then I went to phpMyAdmin and created a new database 'Students_list'
Now I am unable to connect to my database with the following code in php pages.
$db_host = $_ENV['127.12.123.2']; //sample host
$db_user = $_ENV['adminnxUpXA6'];
$db_pass = $_ENV['lyPfbtHYpDFcU'];
$db_name = $_ENV['students_list']; //this is the database I created in PhpMyAdmin
$db = new mysqli($db_host, $db_user, $db_pass);
if ($db->connect_errno) {
die('Connect Error (' . $db->connect_errno . ') '
. $db->connect_error);
}
mysqli_select_db($db,$db_name);
May I please know where I am comitting the error.
Try this instead:
$db_host = getenv('OPENSHIFT_MYSQL_DB_HOST'); //sample host
$db_user = getenv('OPENSHIFT_MYSQL_DB_USERNAME');
$db_pass = getenv('OPENSHIFT_MYSQL_DB_PASSWORD');
$db_name = 'students_list'; //this is the database I created in PhpMyAdmin
$db = new mysqli($db_host, $db_user, $db_pass);
if ($db->connect_errno) {
die('Connect Error (' . $db->connect_errno . ') '
. $db->connect_error);
}
mysqli_select_db($db,$db_name);
You are using $_ENV, which contains environment variables from the operating system. Most probably what you wanted to do is this:
$db_host = 'ip';
$db_user = 'username';
$db_pass = 'password';
$db_name = 'students_list';
The structure for the mysqli connection string is this:
mysqli(Hostname,Username,Password,DatabaseName);
I am having issues connecting from a LAMP over to an DB2 iseries. Any thoughts on this code? I cannot get it to connect. How can I confirm that the iseries driver works, is installed, etc. Or is my code wrong? Thanks in advance...
$db_name = "mydatabase";
$db_host = "ip address";
$db_user = "user";
$db_pass = "mypass";
$dsn = "DRIVER={iSeries Access ODBC Driver};" .
"CommLinks=tcpip(Host=$db_host);" .
"DatabaseName=$db_name;" .
"uid=$db_user; pwd=$db_pass";
$odbc = odbc_connect($dsn, $db_user, $db_pass);
if (!$db = odbc_connect ($dsn, $db_user, $db_pass)) echo 'Error!';
else echo 'Success!';
$res400 = odbc_columns($odbc, "mylibrary", "%", "myfile", "%") or die(odbc_errormsg());
echo odbc_result_all($res400);