在php中连接到mysql数据库
我想从here的AJAX
建议示例教程。我在database.php
文件中发现了一个问题。它包含这样的代码段。在php中连接到mysql数据库
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());
写
mysql_connect($server, $username, $password) or die(mysql_error());
而努力对于DB
mysql_select_db($database) or die(mysql_error());
,并为您查询
mysql_query($q) or die(mysql_error());
我找不到比这个没什么更好的解决方案。
mysql_connect
可以有四个参数。
这样
mysql_connect($server,$user,$pwd,$newlink,$clientflag)
在我的我的前三个你已经知道了。您可能会对$ newlink和$ clientfla*生困惑
$newlink
=返回布尔值意味着TRUE或FALSE。
用法:如果在脚本中再次调用连接。现在将创建新的连接。如果其设置为TRUE
。
clientflag
client_flags参数可以是以下常量的组合:128(启用LOAD DATA LOCAL handling), MYSQL_CLIENT_SSL, MYSQL_CLIENT_COMPRESS, MYSQL_CLIENT_IGNORE_SPACE or MYSQL_CLIENT_INTERACTIVE
。有关详细信息,请参阅有关MySQL客户端常量的部分。在SQL安全模式下,忽略此参数。**according to the php manual
**
检查这些链接了解详细信息
http://php.net/manual/en/function.mysql-connect.php
http://php.net/manual/en/mysql.constants.php#mysql.client-flags
重要提示:请学会mysqli
或PDO
$$链接=> $ { 'db_link'} => $ db_link'在php手册中寻找'variable variables' – 2014-10-27 06:59:55
你的问题是'$ link ='db_link''在那里做什么?这只是一个参数,当没有提供,'db_link'被使用 – Ghost 2014-10-27 07:01:13
请停止使用'mysql_ *'功能,这些都已过时! – Azrael 2014-10-27 07:01:16