Connecting to mysql from php fails silently - php

I'm trying to connect to a server running on linux from a php file using the following file:
<?php
$db_host = "127.0.0.1";
$db_user = "userk";
$db_pss = "pass";
$tag = $_POST['tag'];
echo $tag . " Trying to connect with credentials: "
. $db_host."\n". $db_user . " " .$db_pss;
$link = mysql_connect($db_host, $db_user,$db_pss);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
the output page is:
register Trying to connect with credentials: 127.0.0.1 userk pass
I've checked previous questions looking for useful information. But it still doesn't work.
I'm able to connect to mysql with this user from terminal using putty. I don't know why I don't receive any messages after the connection attempt. Should I check the firewall configuration on the server, how can I check that?
Thanks!

As we know mysql_* is depreciated now, so better use it using MYSQLI_*:
<?php
error_reporting(E_ALL | E_NOTICE | E_STRICT );
$db_host = "localhost";
$db_user = "userk";
$db_pss = "pass";
$database="abc";
$tag = $_POST['tag'];
echo $tag . " Trying to connect with credentials: " . $db_host."\n". $db_user . " " .$db_pss" " .$database;
$link = mysqli_connect($db_host, $db_user,$db_pss,$database);
if (!$link) {
die('Could not connect: ' . mysqli_error($link));
}
echo 'Connected successfully';
mysqli_close($link);
?>
It can be a case that your errors are suppressed. So its not showing an error. try it out. It will give the error now. That will help figuring out real problem.:)

<?php
$db_host = "localhost";
$db_user = "userk";
$db_pss = "pass";
$tag = $_POST['tag'];
echo $tag . " Trying to connect with credentials: " . $db_host."\n". $db_user . " " .$db_pss;
$link = mysql_connect($db_host, $db_user,$db_pss);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
try this :)

Related

Cannot Connect mysql from php with specific port

I have mysql 8.0.15 for windows and I'm trying to connect from remote php webSite but always getting
2002: Connection refused
The code I use to test the connection is very simple
$dbHost = "HOST";
$dbUser = "root";
$dbPass = "password";
$dbDB = "DBNAME";
$dbPort = "9999";
echo "Conectando a $dbHost,$dbUser,$dbPass,$dbDB,$dbPort ";
$link = mysqli_connect($dbHost,$dbUser,$dbPass,$dbDB,$dbPort);
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);
I also try host:port and p:host:port and always getting the same error message.
If you want to change the MySQL port, you have to edit the my.ini config-file on the MySQL server by setting port=<YOUR PREFERRED PORT>, then check if the port is forwarded and finally restart the MySQL server.

Could not connect to MySQL in PHP

I have newly purchased server, on that server database connections are not working.
<?php
error_reporting(E_ALL);
$server = "server name";
$user = "username";
$password = "password";
$db = "test";
echo "Before";
$con = mysql_connect($server, $user, $password);
echo "After";
if (!$con){
die('Could not connect:' . mysql_error());
}
mysql_select_db($db, $con);
?>
When run this file its print Before text but not print After text.
Currently you can use the following code:
ini_set("error_reporting", E_ALL & ~E_DEPRECATED);
Using this you can get either deprecated or not.
FYI: The mysql_* functions have been removed in PHP7.x. There are two modules you can use.
The first is MySQLi, just use the code as follows:
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
You can also use PDO using code :
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>

remote mysql database via php

Hello I have problem with my local computer. I'm install fedora and i have problem to connect with my remote mysql server via php.
php code:
$servername = "my_own_ip";
$username = "test";
$password = "test";
$dbname = "test";
$link = mysql_connect($servername, $username, $password);
if (!$link) {
die('Could sdf connect: ' . mysql_error());
}
echo 'Connected successfully';
or
$servername = "my_own_ip";
$username = "test";
$password = "test";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
I'm sure that my server is correct configured, because I'm tested it before on ubuntu, and now I'm test this script on:
http://phpfiddle.org/
And work normally. I think it is a problem with Fedora system, Can you help me ?
It's can be couple of issues:
Maybe you have a network issue- try to send ping to the server which you want to connect to.
Maybe the firewall of your fedora is on. Turn it off:
$ systemctl disable firewalld
Which error do you get when you run this script?

Php MySQL Connecting a database via MySQL does not work but ODBC does

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");
?>

Cannot connect to local database using php

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

Categories