I'm trying to connect to a remote WAMP server using a simple php script. I'm able to successfully execute scripts like this one:
<?php
$counter = 0;
while($counter < 20) {
$counter++;
echo $counter;
echo "\n";
}
echo "Done! Counter: ";
echo $counter;
echo "\n";
?>
But, unable to execute basic scripts on my laptop that try to connect to my remote server, such as below. The following line just freezes and the php script never stops running. I am executing it by cd'ing into the directory with the file and running php test.php.
$link = mysqli_connect($db_host, $db_user, $db_pass, $db_database, $db_port) or die(mysqli_error($link));
This prints bool(true):
var_dump(function_exists('mysqli_connect'));
Do I need to install anything to get this to work? I've done all the usual fixes, changed the .conf files on my local server and have an Android app that connects successfully, but now I just want to do operations on a MySQL table from Terminal but am unable to.
The correct call to see any errors generated by a mysqli_connect() is mysqli_connect_error() and not mysqli_error($link));
So change the connection call to
$link = mysqli_connect($db_host, $db_user,
$db_pass, $db_database,
$db_port)
or die(mysqli_connect_error());
Or as suggested in the manual
$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;
}
You will also need to be using a Userid/password that is configured to allow access from a remote connection. The default root will not allow remote connections, and should not be amended to allow remote connections.
You may find this page in the manual useful to create a new user account in MYSQL
Related
This question already has answers here:
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES) (SQL: select * from `table`)
(2 answers)
Closed 2 years ago.
I tried connecting my PHP file to MySQli database but when i ran the code it displays
But when i logon to phpmyadmin it works fine no errors i can login to my account with no problems
Credentials in the database:
Connection Code:
<?php
$con = mysqli_connect('localhost','sotomango', '1234', 'mysql');
if(!$con) {
echo 'noooo';
}
?>
I double checked the database usernames, passwords, and privileges. Everything seems in place and correct, but the php file wont allow me to connect to the database.
#Dharman had a nice post but I couldnt find it.
Try this for debug :
$con = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$con) {
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($con) . PHP_EOL;
mysqli_close($con);
mysqli_connect
if its a new instalation or new upgrade, You need to include your port into connection, mariadb comes as default port so, when you try to connect mysql its redirecting to mariadb.
you need to include your correct port into connection it might be 3306, 3307 or 3308, and use ip instead of localhost.
your final codes should look like this :
$host = '127.0.0.1';
$db = 'test';
$user = 'root';
$pass = '';
$port = '3308';
$charset = 'utf8mb4';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
$mysqli = mysqli_connect($host, $user, $pass, $port, $db);
mysqli_set_charset($mysqli, $charset);
} catch (\mysqli_sql_exception $e) {
throw new \mysqli_sql_exception($e->getMessage(), $e->getCode());
}
For The People Wondering
I solved this by just adding a port to the host name like localhost:3308
I have MySQL database, it contain a table with a column called "downloads"
I want to update this column to 0 every 24h, but it seems my code doesn't work!
I have folder on the server named cron.
Inside it there is two files, one to connect to the database, and the other contain the php code to reset the column downloads to 0
This is my connection code:
<?php
$link = mysqli_connect("localhost", "test", "root", "test1");
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;
?>
And my php code that I want to use it in cronjob is this one:
<?php
require_once('/home/cron/connect.php'); // connect to db
$mysqli = ("update users set downloads = 0");
$mysqli->close();
?>
I fired the file directly from the browser but it doesn't reset the column downloads to zero! what I'm doing wrong?
Note: of course there is .htaccess file to protect direct access to the connection file
EDIT: There is no error at connection if I run the code of connection, but the second code from cronjob doesn't work!
You do not need $mysqli->close(); at all. Your connection object is called $link. The second line should be:
$link->query("update users set downloads = 0");
You should probably also check if it executed properly and if not do something about it.
Your full code in second file could look like this (assuming the connection is successful):
<?php
require_once('/home/cron/connect.php'); // connect to db
if( $link->query("update users set downloads = 0") ){
// succesful
} else {
// fail
}
I have Apache (with PHP) and MySQL installed and running on my Raspberry Pi. I've done some simple tests with PHP, and it seems to be working. And MySQL is working perfectly from the terminal, and even another computer.
I made this PHP file:
<?php
echo("Connecting");
$connection = new mysqli("127.0.0.1", "admin", "password", "test");
if ($connection->connect_error) {
die("Connection error");
}
echo("Connection successful");
?>
Yet when I go to this page in my web browser, all I see is "Connecting". If I comment out the connection command, I see "Connecting Connection successful" in the browser.
It seems as if the PHP code stops running or hangs at the connection command.
Any ideas why I'm having this strange behavior?
Try adding these lines at the top: error_reporting(E_ALL);
ini_set('display_errors',1);
I hope You should do following
if ($connection->connect_error) {
echo "Connection error";
}else{
echo "Connection successful";
}
Try this instead
<?php
$link = mysqli_connect("127.0.0.1", "admin", "password", "test");
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 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"
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I connected successfully to my database and the I tried to print my data but I can only see the "Connected succesfully" line in the browser. Here is my code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$db=mysqli_connect("host","root","pass","dbase");
if (!$db) {
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 "Connected successfully";
$sql = "SELECT username FROM students";
$result = $db->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "username: " . $row["username"]. "<br>";
}
} else {
echo "0 results";
}
$db->close();
?>
So the issue was that SELinux was blocking remote connections from PHP scripts executed by Apache web server.
The setsebool -P httpd_can_network_connect=1 in the terminal did the trick.
Edit: Note to future readers. The OP edited their question with the corrected syntax after answers were posted and this answer was based on their original post:
https://stackoverflow.com/revisions/36189996/2
and added:
"So the issue was that SELinux was blocking remote connections from PHP scripts executed by Apache web server. The setsebool -P httpd_can_network_connect=1 in the terminal did the trick."
Firstly, you didn't select a database.
and if (!db) needs to be changed to if (!conn) and if ($db->connect_error) to if ($conn->connect_error). The same variable needs to be used throughout your code.
You're also using the wrong variable $db it's $conn (or whatever you want to use, but as I said above, you need to use the same variable for your connection and for the query.
Error reporting http://php.net/manual/en/function.error-reporting.php would have told you about an undefined variable.
Therefore:
$conn = #mysqli_connect("host","root","pass", "your_database");
Sidenote: host I take it is only representative of the host you're using. If you're on your own PC, you would use localhost and accessing that file as http://localhost/file.php instead of file///file.php if that is what you're doing and will not work. The web browser will not parse PHP directives like that and you need to install a webserver/PHP in order for it to work, including MySQL.
Read the manual on connecting:
http://php.net/manual/en/function.mysqli-connect.php
and remove the # symbols while testing. They are error suppressors.
Check for errors with http://php.net/manual/en/mysqli.error.php
Example from the manual:
Sidenote: 127.0.0.1 or localhost or if hosted, use the setting they gave you to use.
<?php
$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);
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
You use another variable than you connected to the database!
You connected to $db but perform the query with $conn.
Also, in the check if the database connection could be established you do not have the $ before db.
It always helps a lot when you have in a terminal window the PHP error log open. 99% of errors can be fixed by your self.