使用 PHP/MySQL 连接到 phpMyAdmin 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2535962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:52:46  来源:igfitidea点击:

connecting to phpMyAdmin database with PHP/MySQL

phpmysqlphpmyadmin

提问by The Worst Shady

I've made a database using phpMyAdmin , now I want to make a register form for my site where peaple can register .I know how to work with input tags in HTML and I know how to insert data into a database but my problem is that I don't know how I can connect to the database that is already made in phpMyAdmin.

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

回答by Amy B

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

该数据库是MySQL数据库,而不是phpMyAdmin数据库。phpMyAdmin 只是连接到数据库的 PHP 代码。

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

// now you are connected

回答by railsbox

Connect to MySQL

连接到 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.

还有 mysqli_connect() 函数来打开一个到 MySQL 服务器的新连接。

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

回答by Tim Post

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

设置一个用户,一个允许用户通过使用(例如 localhost)与 MySQL 对话的主机,授予该用户足够的权限以使用数据库执行他们需要的操作……然后快速执行。

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

用户需要基本的CRUD权限才能启动,这足以存储从表单接收到的数据。其余的权限是不言自明的,即修改表等的权限。给予用户不超过其工作所需的权力。

回答by Snowman

This (mysql_connect, mysql_...) extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLior PDO_MySQLextension should be used.
(ref: http://php.net/manual/en/function.mysql-connect.php)

这个 (mysql_connect, mysql_...) 扩展从 PHP 5.5.0 开始被弃用,将来会被删除。相反,应使用MySQLiPDO_MySQL扩展。
(参考: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");
    
  • 面向对象:

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

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