I have an issue with PHP script not connecting to MySQL database. Below code always generates error:
$dbconn = #mysql_connect('SFDC1', '<username>', '<password>');
if (!$dbconn)
{
die('Error connecting to DB!');
}
There is no issue if I connect to the database using MySQL workbench with same credentials. Issue only occurs during the communication between PHP and MySQL.
Any help on debug of this issue?
Try this one;
$dbhost = "localhost";
$dbuser = "username";
$dbpass = "pass";
$dbname = "DBname";
if(!#mysql_connect($dbhost,$dbuser,$dbpass)) {
echo "Cannot connect";
die();
} else
mysql_select_db($dbname);// or report();
Use this code to connect mysql.
<?php
$dbconn = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$dbconn ) {
die('Could not connect: ' . mysql_error());
}
?>
Related
here is the code that I am using:
$dbhost = "localhost:8080";
$dbuser = "root";
$dbpass = "";
$dbname = "test";
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(! $conn){
die('Could not connect:'. mysql_error());
}
else{
echo "Connected Successfully";
}
?>
when I use include or require_once, I never get the echo "Connected successfully" I always receive the error.
This is for a university project, and in uni the windows computers connect right away, using the same code.
since i am using MacOSX is there anything i need to do?
My server is also running on bitnami MAMP
Here is my connection code, I have made mysql database in 000webhost and I have also uploaded file in 000webhost but every time I am getting error like this "
PHP Error Message
Warning: mysql_connect() [function.mysql-connect]: Access denied for
user 'a1346052'#'10.1.1.39' (using password: YES) in
/home/a1346052/public_html/connection.php on line 7
Free Web Hosting
Could not connect database
please help me on this, Thanks
<?php
$mysql_hostname = "mysql13.000webhost.com";
$mysql_user = "a1346052";
$mysql_password = "*****";
$mysql_database = "a1346052_simple";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?
I dont think your username will only be that. There will certainly be a username along with the random number. Ex: a7238414_username. Try running following code and see what result it gives.
$dbhost = 'mysql13.000webhost.com';
$dbuser = 'a7238414_username';
$dbpass = '***';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($conn);
Just a headsup, mysql is deprecated. You should start using mysqli or PDO :)
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.
i'm new here and doing some project,
how can i connect to my database in db4free.net from php,
i have tried this
<?php
$host = "db4free.net/mainbookstore";
$user = "***";
$pass = "****";
$database = "mainbookstore";
$conn = mysql_connect($host, $user, $pass);
if ($conn) {
$buka = mysql_select_db ($database);
if (!$buka) {
die ("Database tidak dapat dibuka");
}
} else {
die ("Server MySQL tidak terhubung");
}
?>
but get error Such no host, but in my java use :
dataSource.setUrl("jdbc:mysql://db4free.net:3306/mainbookstore");
i can connect to my database.
Thanks before :)
Most likely, it is the missing port. Try
$host = "db4free.net:3306/mainbookstore";
and it should work.
I already have the mysql database made, I just need to connect php to mysql locally but I don't know the php commands for that.
cheers
$server = 'localhost';
$user = 'myUser';
$pass = 'myPass';
$dbname = 'MyDatabase';
$con = mysql_connect($server, $user, $pass) or die("Can't connect");
mysql_select_db($dbname);
The following contains a pretty good example: http://ca3.php.net/manual/en/mysql.examples-basic.php
This is a simple connect to the mysql server. After Connecting to the server it selects a DB
<?php
mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("test") or die(mysql_error());
echo "Connected to Database";
?>