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.
Related
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 updated my server & moved both server itself & website on the same machine.
After doing that, my website won't connect to DB anymore.
I'm using Ubuntu 16.04 on aws
I'll include connection script, because a friend said my code might be too old for php 7. Other then that I'm looking for any suggestions which might result in fixing my problem.
include "../config.php";
$link = #mysql_connect($db_host, $db_user, $db_pass);
if (!$link)
{
$error = "Cannot access MYSQL, please contact admin!<br />";
$error .= mysql_errno() . ": " . mysql_error();
die($error);
}
$db = #mysql_select_db($db_name);
if (!$db)
{
$error = "Failed to select database.<br />";
$error .= mysql_errno() . ": " . mysql_error();
die($error);
}
$lang = #mysql_query("SET NAMES utf8");
# is an error control operator. It means to php "if this call fails, let's log nothing and let's continue the trip"
So remove those # symbols before your mysql calls then see your logs.
Anyway those mysql calls won't be accepted by php7.
You have now to use PDO or MySQLi
http://php.net/manual/en/function.mysql-connect.php
I have a script that connects to a MySQL database in localhost, when I run it from the console it runs just fine, but when I run it from the web, I get this:
Warning: mysqli_connect(): (HY000/2002): No such file or directory in /var/www/html/tests/mysql.php on line 3
Error: Unable to connect to MySQL. Debugging errno: 2002 Debugging error: No such file or directory
I checked my php.ini file and it has the correct location of mysql.sock, the file exist, but php won't connect via web.
Also, I have Fedora Linux with Firewall disabled as well as SELinux, so I have no idea what can be causing this.
This is the test code I'm using:
<?php
$link = mysqli_connect("localhost", "root", "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);
?>
Any ideas?
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