Connecting to phpMyAdmin database using PHP / MySQL

I created a database using phpMyAdmin, now I want to create a registration form for my site where a file can be registered. I know how to work with input tags in HTML, and I know how to embed data in a database, but my problem is that I do not know how I can connect to a database that is already created in phpMyAdmin.

+4
source share
4 answers

A database is a MySQL database, not a phpMyAdmin database. phpMyAdmin is only PHP code that connects to the database.

mysql_connect('localhost', 'username', 'password') or die (mysql_error()); mysql_select_database('db_name') or die (mysql_error()); // now you are connected 
+6
source

MySQL connection

 <?php /*** mysql hostname ***/ $hostname = 'localhost'; /*** mysql username ***/ $username = 'username'; /*** mysql password ***/ $password = 'password'; try { $dbh = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password); /*** echo a message saying we have connected ***/ echo 'Connected to database'; } catch(PDOException $e) { echo $e->getMessage(); } ?> 

The mysqli_connect () function also opens a new connection to the MySQL server.

 <?php // Create connection $con=mysqli_connect(host,username,password,dbname); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } ?> 
+3
source

Configure the user, the host, which the user is allowed to talk to MySQL with (for example, localhost), provide this user with sufficient permissions to perform the necessary actions with the database .. and presto.

The user will need basic CRUD privileges that are sufficient to store data received from the form. Other permissions are self-evident, that is, they allow changing tables, etc. Give the user more, not less power, than you need to do your job.

+1
source

This extension (mysql_connect, mysql _...) has been deprecated from PHP 5.5.0 and will be removed in the future. Instead, use the MySQLi extension or PDO_MySQL .
(ref: http://php.net/manual/en/function.mysql-connect.php )

  • Object Oriented:

     $mysqli = new mysqli("host", "user", "password"); $mysqli->select_db("db"); 
  • Procedural:

     $link = mysqli_connect("host","user","password") or die(mysqli_error($link)); mysqli_select_db($link, "db"); 
0
source

Source: https://habr.com/ru/post/1305418/


All Articles