gpt4 book ai didi

php - 在php中连接mysql数据库

转载 作者:行者123 更新时间:2023-11-29 23:30:56 26 4
gpt4 key购买 nike

我正在尝试 here 建议的 AJAX 示例教程。我在 database.php 文件中发现了一个问题。它包含这样的代码段。

function db_connect($server = 'localhost', $username = 'root', $password = '123', $database = 'db_ajax_suggest', $link = 'db_link') {
global $$link;

$$link = mysql_connect($server, $username, $password);

if ($$link) mysql_select_db($database);

return $$link;
}

我的问题是代码中的$link = 'db_link' 是什么。我以前使用过php,但不是很多,我没有遇到过这样的情况。所需要的只是 $servername、$username、$password。

我是一个java人,而不是PHP。在java中我可以使用jdbc:mysql://localhost:3306/db_ajax_suggest。但在这种情况下,Hoe 应该设置数据库 url 吗?谢谢!!

最佳答案

我认为只需这么多就可以连接到数据库并使用表

$connect = mysql_connect('localhost','root','123');
if (!$connect) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_select_db('db_ajax_suggest',$connect);

以及从表中获取数据

$query = " your query ";
mysql_query($query) or die('SQL ERROR:'.mysql_error());

关于php - 在php中连接mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26582439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com