简体   繁体   中英

connecting to phpMyAdmin database with PHP/MySQL

我已经使用 phpMyAdmin 创建了一个数据库,现在我想为我的网站制作一个注册表单,人们可以在其中注册。我知道如何使用 HTML 中的输入标签,我知道如何将数据插入数据库,但我的问题是我不知道如何连接到已经在 phpMyAdmin 中创建的数据库。

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

mysql_connect('localhost', 'username', 'password') or die (mysql_error());
mysql_select_database('db_name') or die (mysql_error());

// now you are connected

Connect to MySQL

<?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();
    }
?>

Also mysqli_connect() function to open 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();
  }
?> 

Set up a user, a host the user is allowed to talk to MySQL by using (eg localhost), grant that user adequate permissions to do what they need with the database .. and presto.

The user will need basic CRUD privileges to start, that's sufficient to store data received from a form. The rest of the permissions are self explanatory, ie permission to alter tables, etc. Give the user no more, no less power than it needs to do its work.

This (mysql_connect, mysql_...) extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.
(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");
$db = new mysqli('Server_Name', 'Name', 'password', 'database_name');

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM