I have MySQL server running on my local computer. I am trying to connect to it
through PHP from my website host. I get the following error:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2).
Here is the php I am running with the userid and password wiped out for security:
if ($dbc = #mysql_connect('localhost', 'userid', 'password')) {
print '<p>Successfully connected to MySQL!</p>';
// Try to create the database:
if (#mysql_query('CREATE DATABASE myblog',$dbc)) {
print '<p>The database has been created!</p>';
} else { // Could not create it.
print '<p style="color: red;">Could not create the database because:<br />' . mysql_error() . '.</p>';
}
// Try to select the database:
if ($x == #mysql_select_db('myblog,$dbc')) {
print '<p>The database has been selected.</p>';
} else {
print '<p style="color: red;">Could not select the database because:<br />' . mysql_error() . '.</p>';
}
mysql_close(); // Close the connection.
} else {
print '<p style="color: red;">Could not connect to MySQL:<br />' . mysql_error() . '.</p>';
Can someone help me with this connection?
It seems that your code should work unless your MySQL server is not properly configured... but you may try the MySQLi Route.
$servername = "computername";
$username = "username";
$password = "password";
$dbname = "dbname";
// Create connection
$mysqli = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
Related
Here is the code
<?php
$servername = "localhost";
$usrname = "root";
$pwd = "";
$db = "test_db";
// connect to the database
$conn= mysqli_connect($servername, $usrname, $pwd,$db);
if (!$conn){
die('connection failed' . mysqli_connect_error());
}
// Create database
$sql = "CREATE DATABASE IF NOT EXISTS test_db";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully<br>";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
?>
I am not sure what is going on as I am sure i made no errors while typing. As it keeps showing me that there is an unknown database despite the fact i made a CREATE DATABASE statement.
I do not know if there is something else i need to do but by all measures the code should work.
It is supposed to echo the "Database created successfully" or the error message.
`<?php
$servername = "localhost";
$usrname = "root";
$pwd = "";
//$db = "test_db";
// connect to the database
$conn= mysqli_connect($servername, $usrname, $pwd);
if (!$conn){
die('connection failed' . mysqli_connect_error());
}
// Create database
$sql = "CREATE DATABASE IF NOT EXISTS test_db";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully<br>";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
?>`
Your code is fine, You just have to remove $db from mysqli_connect().
Because you are requesting to connect "test_db" to this database which already not exists.
<?php
// Connect to MySQL
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// Make my_db the current database
$db_selected = mysql_select_db('my_db', $link);
if (!$db_selected) {
// If we couldn't, then it either doesn't exist, or we can't see it.
$sql = 'CREATE DATABASE my_db';
if (mysql_query($sql, $link)) {
echo "Database my_db created successfully\n";
} else {
echo 'Error creating database: ' . mysql_error() . "\n";
}
}
mysql_close($link);
?>
Reference
How do I create a database if it doesn't exist, using PHP?
$conn= mysqli_connect($servername, $usrname, $pwd,$db);
You are trying to connect to a database that did not initially exist, so you get a fatal error.
Try creating new database using try catch blocks
try{
$conn= mysqli_connect($servername, $usrname, $pwd,$db);
}cath(Exception $e){
//your code
}
I am trying to get my PHP pages to connection to a remote server hosting the MySQL database. I can connect via the command line just fine with the same username and password. Below is a simple test file I created. The only thing I've done to the code is remove the password, but I know that's not the issue. Like I said I can connect via the CLI. This is a Linux install. When viewed from the web browser, all I get is an error 500 page. If I comment out the mysqli statement, the page displays the Connected successfully.
<?php
$servername = "12.0.1.170";
$username = "jason";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
/ die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Per the php documentation for mysqli_connect()
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
Your code appears to be lacking all four parameters required.
You have:
$conn = mysqli_connect($servername, $username, $password);
Perhaps try:
$conn = mysqli_connect($servername, $username, $password, $database);
with a $database='db_name'; value added into your variables section.
There are two possible issues with remote mysql connection:
1. Firewall of the server: You must enable incoming connection on port (for. e.g. 3306).
2. User in mysql: You must have a user created (for e.g. jason in this case)
Try connecting this way
$con = mysqli_connect('localhost','root','','db_name');
if($con)
echo "hell yeah its connected";
else
echo "i m boned"
I'm trying to connect to a database via MySQL but it does not work. I'm getting the following error:
"Connection failed: No connection could be made because the target machine actively refused it. "
Here's the basic code:
<?php
$servername = "server";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
I can connect to the database from Excel and also via an ODBC connection with the code below but not via MySQL:
<?php
$user = 'username';
$pass = 'password';
$server = 'server';
$database = 'database';
// No changes needed from now on
$connection_string = "DRIVER={SQL Server};SERVER=$server; DATABASE=$database";
$conn = odbc_connect($connection_string,$user,$pass);
if (!$conn){
exit("Connection to the database Failed: " . $conn);
}
echo "Connected successfully";
?>
Any ideas?
You say you can connect with odbc which means you have an SQL Server database, not a Mysql database. If you don't like odbc, pdo is your other option. Mysqli is not an option as its mysql only.
Try this code:
<?php
$link = mysqli_connect('server','username','password','database');
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo("success");
?>
I am having trouble connecting to my localhost database with php. It feels like I have followed every tutorial there is.
current php code:
<?php
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="localhost";
$database="webutvshop";
$username="dbconnect";
$password="password";
//DO NOT EDIT BELOW THIS LINE
$link = mysql_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysql_error());
}
else{
echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}
$db_selected = mysql_select_db($database, $link);
if (!$db_selected) {
die ('Can\'t select database: ' . mysql_error());
}
else {
echo 'Database ' . $database . ' successfully selected!';
}
mysql_close($link);
?>
The issue stands, when I acess the file locally on my computer I do not get any answer at all from it. I've tried with many other, yet I do not get any answers from them!
I need help in order to keep working on my schoolproject, thanks.
Stop using mysql_*, because they are deprecated officially. Use mysqli_* or PDO for this purpose. An example:-
<?php
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="localhost";
$database="stackquestion";
$username="root";
$password=""; // check once with empty password and once with some password that you tried already
//DO NOT EDIT BELOW THIS LINE
$link = mysqli_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysqli_connect_error());
}
else{
echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}
$db_selected = mysqli_select_db($link,$database);
if (!$db_selected) {
die ('Can\'t select database: ' . mysqli_error($link));
}
else {
echo 'Database ' . $database . ' successfully selected!';
}
mysqli_close($link);
?>
Output:- http://prntscr.com/7cbr5j
Can you also verify you can connect to the database from a command line:
mysql -u dbconnect -ppassword -h localhost webutvshop
I am trying to insert some data from the client browser to a MySQL database on AWS server.
The code worked on godaddy so it looks like a permissions/security issue.
$servername = "xx.xx.xx.xx";
$username = "user";
$password = "mypass";
$dbname = "dbName";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO mytable ('email')
VALUES ('" . $email . "')";
echo $sql;
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
I am getting the error saying "Connection failed: Host x.x.x.x is not allowed to connect to this MySQL server "
How do I allow connections to this from a client browser?
I have already commented out the line bind-address = 127.0.0.1
If Mysql is a remote host, than use private IP address of source system to be allowed as AWS would use private IP address to communicate by default.