使用 PHP/MySQL 连接 phpMyAdmin 数据库

发布于 2024-08-27 02:33:06 字数 133 浏览 5 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

画尸师 2024-09-03 02:33:06

该数据库是 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

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
苍暮颜 2024-09-03 02:33:06

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

还可以使用 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();
  }
?> 

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();
  }
?> 
温柔戏命师 2024-09-03 02:33:06

设置一个用户,一个允许用户使用(例如本地主机)与 MySQL 对话的主机,授予该用户足够的权限来执行他们需要的数据库操作..然后就完成了。

用户需要基本的 CRUD 权限才能启动,这足以存储收到的数据从一个表格。其余的权限是不言自明的,即更改表的权限等。给予用户完成其工作所需的权力。

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.

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, i.e. permission to alter tables, etc. Give the user no more, no less power than it needs to do its work.

你的往事 2024-09-03 02:33:06

此 (mysql_connect, mysql_...) 扩展自 PHP 5.5.0 起已弃用,并将在将来删除。相反,应使用 MySQLiPDO_MySQL 扩展。
(参考:http://php.net/manual/en/function .mysql-connect.php)

  • 面向对象:

    $mysqli = new mysqli("主机", "用户", "密码");
    $mysqli->select_db("db");
    
  • 过程:

    $link = mysqli_connect("主机","用户","密码") 或 die(mysqli_error($link)); 
    mysqli_select_db($link, "db");
    

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");
    
铜锣湾横着走 2024-09-03 02:33:06
$db = new mysqli('Server_Name', 'Name', 'password', 'database_name');
$db = new mysqli('Server_Name', 'Name', 'password', 'database_name');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文