-- fixed --
I fixed the issue that I was having. I'm not 100% sure how it was fixed, but I think it was because I changed the connection port to 3306 then connected using localhost rather than my ip. Thank you for everyone that took their time to help, you all were very helpful.
I'm having a really frustrating issue that I've been trying to fix for (literally) the past 4 hours. I cannot connect to MySQLi through my php code. I checked and the username, password, database, and host are correct. Here's my code:
$mysqli = new mysqli("104.236.***.57", "Josh", "********", "******");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
All this does is echo Connected. The query doesn't return anything (it's like an empty string). When I try to echo $connection, it does the same. I think it's an issue with the connection because it was empty but you guys know best (I'm new to MySQL). I've researched and tried stuff for 4+ hours and nothing has worked.
Thank you so much,
Josh
edit: I've updated the code. Now it returns the error: Connection refused (mysql error).
I think I have fixed the connection issue, but I've ran into a new issue which I think may be related to the other issue. I have this code here:
$servername = "104.***.***.57";
$username = "Josh";
$password = "*******";
$dbname = "*****";
$conn = new mysqli($servername, $username, $password, $dbname, '/var/run/mysqld/mysqld.sock');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"];
}
} else {
echo "0 results";
}
$conn->close();
And it seems to work, except for that it prints '0 results' when there is 1.
<?php
$servername = "localhost"; //replace your servername
$username = "root"; //replace your username
$password = ""; //replace your password
$dbname = "test"; //replace your database name
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
//Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
echo ' not connected';
}
else
echo 'connected';
?>
execute this and tell me the output.
For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli::query (or mysqli_query()) will return a mysqli_result object (see this article mysqli.query).
So in your case you can't just echo this, you can see rows data like this:
$mysqli = new mysqli("104.236.***.57", "Josh", "********", "******");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = $mysqli->query("SELECT * FROM users", MYSQLI_USE_RESULT)) {
/* prints number of result rows */
printf("Select returned %d rows.\n", $result->num_rows);
/* Cycle through results */
echo "Table data:\n";
while ($row = $result->fetch_object()){
var_dump($row);
}
/* free result set */
$result->close();
}
/* close connection */
$mysqli->close();
Also, check your database server (104.236.***.57) made available for remote access. Check with your hosting company if you can access the database server from outside.
Can you please check whether Mysql is running or not. It usually happens when your droplet runs out of memory. In that case you can add a swap file to your droplet.
$sql = "SELECT * FROM users";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result))
{
echo "id: " . $row["id"];
}
Try this instead of your code.
//just fill your own settings...
$server = "localhost";
$user = "root";
$password = "*****";
$database = "uyelik";
$db= mysqli_connect($server ,$user, $password);
if(!$db) { echo 'Mysqli conn. error: ' . mysqli_connect_errno(); exit;}
mysqli_select_db($db,$database);
$query = mysqli_query($db,"SELECT * FROM lists") or die (mysql_error());