I have made an sql database in cpanel and adding some data to a table. I am now trying to access it and print out the data on my website, but I am not convinced I have the php correct. A tutorial I found to explain it is using xampp and not a real online website. so far I have a database handler php file with,
<?php
$dbServername = "localhost";
$dbUsername = "username";
$dbPassword = "password";
$dbName = "databasename";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
In my index.php file I have,
<?php
include_once('php/db_handler.php');
?>
//some html code
<?php
//Choose all fields in table
$sql ="SELECT * users;";
//var to put in all the results
$results = mysqli_query($conn, $sql);
//var to get the number of rows returned.
$resultcheck = mysqli_num_rows($result);
//if the number of rows returned is not empty, then cycle through all results and print the stated ones to the screen.
if($resultcheck > 0){
while($row = mysqli_fetch_assoc($result)) {
echo $row['$email, $username'];
}
}
?>
//more html code
I am not totally sure what I should be using for database server name, In cpanel there is a section with server info and it gives me the servername webhosting2004, I have tried this also which does not work.
The username and password are as per the user details in mysql databases (using my cpanel accountname_name)
I get the following on the error log
[05-Nov-2019 12:31:51 UTC] PHP Warning: mysqli_connect(): (HY000/1045): Access denied for user 'dannyjeb_admin'@'old' (using password: YES) in /home/dannyjeb/public_html/php/db_handler.php on line 8
[05-Nov-2019 12:31:51 UTC] PHP Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /home/dannyjeb/public_html/test.php on line 48
[05-Nov-2019 12:31:51 UTC] PHP Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/dannyjeb/public_html/test.php on line 51
You can start by displaying connection errors to determine if the connection works
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
if (!$conn) {
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;
}
Second. Make sure you have remote access to your database allowed. If not, allow it. To do this, go to RemoteMysql from cPanel and add %.%
to allow users from any origin.
Third. If you are not sure about the user for the database connection, you can always create a new MySQL user to use for your connection.
Check your error log what kind of error is showing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.