Categories > Coding > PHP >

How can I connect php to mysql linux database?

Posts: 29

Threads: 18

Joined: Oct, 2021

Reputation: 0

Posted

This is the story I got a linux server  that runs on my rasbarri pi and I installed on an ubuntu server .I installed mysql using sudo apt install mysql-server.I created a basic database and I tried to connect to it using this code.I installed php using sudo apt install php

 

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if (mysqli_connect_error()) {
  die("Database connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

I thinked the probelm is becaus of sudo permission so I disabled sudo because I tought is because of it.It didn't work.It doesn't display that it connected to the databse.What is the problem in my code or where did I go wrong?.All the credentials are corect but it doesn't confirm that I am connected.Do not tell me to change to Javscript or download some weird files.I am not dumb.Is a problem with the php code?If yes where?Or is a problem with how I configured the server?

  • 0

blueless

Failed to fetch.

vip

Posts: 435

Threads: 41

Joined: Dec, 2021

Reputation: 17

Replied

https://www.youtube.com/watch?v=VidqoMsdySs maybe this can help you

  • 0

Failed to fetch.

Posts: 10

Threads: 2

Joined: Jan, 2022

Reputation: -31

Replied

this code doesn't have database included

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database = "";


// Create connection
$conn = new mysqli_connect($servername, $username, $password, $database);

// Check connection
if (mysqli_connect_error()) {
  die("Database connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

Be sure you set all permissions

  • 0

<script>alert("get good get monelite.cf")</script>

Posts: 190

Threads: 19

Joined: Jul, 2021

Reputation: 6

Replied

better to just go to stackoverflow

  • 0

10 years ago

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )