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?
Related
I use the hosting company aPlus.net, and I can't seem to get past a connection error I'm getting when trying to process some php to write database content to a webpage, and I am curious as to if this is because my database appears to not be on the same server as the entire rest of my hosting account, and if there is a way to resolve this in my code? This is my first attempt at writing PHP, and it would be good to know if my code is wrong, or if my hosting company is messing me up. (and either way, how to fix it)
Here's the code that's failing to pull from the database:
{
$con = mysql_connect("localhost","2p5dq9vxmy240651","MY_PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("felineasthma_2p5dq9vxmy240651", $con);
$users_name = $_POST['name'];
$users_comment = $_POST['requests'];
$users_name = mysql_real_escape_string($users_name);
$users_comment = mysql_real_escape_string($users_comment);
$inputid = $_GET['id'];
$query = "
INSERT INTO `felineasthma_2p5dq9vxmy240651`.`submissions` (`id`,
`name`, `requests`, `inputid`) VALUES (NULL,
'$users_name', '$users_comment', '$inputid');";
mysql_query($query);
echo "<h2>Your request has been processed, reload page.</h2>";
mysql_close($con);
}
and here's some screen captures from inside my hosting account (links because I don't have enough posts here yet to upload images, sorry):
felineasthma_2p5dq9vxmy240651 doesn't appear in my hosting account
yet it clearly exists in MySQL Manager, but on a different server
I was even more confused while making the user for this database, as the control panel didn't allow me to make a username, it just randomly assigned one. Help? Advice?
I found a more modern tutorial to learn PHP with, and now everything works, I just need to add security measures now. Here's the working code snippets, if anyone ever comes here asking the same questions.
here's the form action that places the entries into the database:
<?php
$servername = "sql5c40n.carrierzone.com";
$username = "my_username";
$password = "my_password";
$dbname = "my_database";
$users_name = $_POST['name'];
$users_request = $_POST['requests'];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO submissions (name, requests)
VALUES ('$users_name', '$users_request')";
if (mysqli_query($conn, $sql)) {
header("Location: clv.php");
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
here's the include that puts the database entries onto the page:
<?php
$servername = "sql5c40n.carrierzone.com";
$username = "my_username";
$password = "my_password";
$dbname = "my_database";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, requests, name FROM submissions";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "" . $row["requests"]. " - by " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
I'm new to PHP and MYSQL, and I have what I think is going to be a relatively easy question.
My objective is to show one image (a green flashing light) when the database is connected, and display another image (a red flashing light) when there is no database connection.
I imagine it should be a simple variation on this:
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
But if I attempt to add an image to where it echos "Connected successfully" I receive an error.
I'm attempting to add the image like this:
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("<img src="Red_Light.gif" style="width:10px;height:10px;"> " . $conn->connect_error);
}
echo "<img src="Green_Light.gif" style="width:10px;height:10px;">";
?>
I probably have the completely wrong syntax but any help is appreciated.
Many thanks,
Leif
You can use Janno answer or you may use this (by changing " to ':-
if ($conn->connect_error) {
die("<img src='Red_Light.gif' style='width:10px;height:10px;'> " . $conn->connect_error);
}
echo "<img src='Green_Light.gif' style='width:10px;height:10px;'>";
if ($conn->connect_error) {
die("<img src=\"Red_Light.gif\" style=\"width:10px;height:10px;\"> " . $conn->connect_error);
}
echo "<img src=\"Green_Light.gif\" style=\"width:10px;height:10px;\">";
Whole problem seems to be related to not escaping the quote marks.
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 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.
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 :)