简体   繁体   English

在PHP中连接到MySQL数据库

[英]Connecting to mysql database in php

I am trying an example tutorial for AJAX suggest from here . 我正在尝试从此处建议AJAX的示例教程。 I found an issue in the database.php file. 我在database.php文件中发现了一个问题。 it containts a code segment like this. 它包含这样的代码段。

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;
  }

My question is what is $link = 'db_link' in the code. 我的问题是代码中的$link = 'db_link'是什么。 I have worked with php before, but not a lot, I have not met a situation like this. 我以前使用过php ,但工作不多,我还没有遇到过这样的情况。 All needed were $servername, $username, $password. 所有需要的是$ servername,$ username,$ password。

I am a java person more than PHP . 我比PHP更是java人。 In java I can use jdbc:mysql://localhost:3306/db_ajax_suggest . 在Java中,我可以使用jdbc:mysql://localhost:3306/db_ajax_suggest But in this case, Hoe should set the database url? 但是在这种情况下,Ho头应该设置数据库URL吗? Thank you!! 谢谢!!

I think Only this much you needed to connect to your databases and work with your tables 我认为只有这么多,您才需要连接到数据库并使用表

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

and for fetching data from table 并从表中获取数据

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

Write

mysql_connect($server, $username, $password) or die(mysql_error());

and for DB 和DB

mysql_select_db($database) or die(mysql_error());

and for your queries 和您的查询

mysql_query($q) or die(mysql_error());

I find nothing better solution than this. 我发现没有比这更好的解决方案了。

mysql_connect can have four paramter. mysql_connect可以有四个参数。

like this 像这样

mysql_connect($server,$user,$pwd,$newlink,$clientflag)

In my my first three you already know. 在我的前三个中,您已经知道。 You might have confusiong about $newlink and $clientflag 您可能对$ newlink和$ clientflag感到困惑

$newlink = Return bool value means TRUE or FALSE. $newlink =返回布尔值表示TRUE或FALSE。

Usage: If the connection is called again in the script. 用法:如果在脚本中再次调用连接。 now new conection will be created. 现在将创建新的连接。 if its set to TRUE . 如果将其设置为TRUE

clientflag The client_flags parameter can be a combination of the following constants: 128 (enable LOAD DATA LOCAL handling), MYSQL_CLIENT_SSL, MYSQL_CLIENT_COMPRESS, MYSQL_CLIENT_IGNORE_SPACE or MYSQL_CLIENT_INTERACTIVE . clientflag client_flags参数可以是以下常量的组合:128(启用LOAD DATA LOCAL handling), MYSQL_CLIENT_SSL, MYSQL_CLIENT_COMPRESS, MYSQL_CLIENT_IGNORE_SPACE or MYSQL_CLIENT_INTERACTIVE Read the section about MySQL client constants for further information. 阅读有关MySQL客户端常量的部分,以获取更多信息。 In SQL safe mode, this parameter is ignored. 在SQL安全模式下,将忽略此参数。 **according to the php manual ** **according to the php manual **

Check these link for further info 检查这些链接以获取更多信息

http://php.net/manual/en/function.mysql-connect.php http://php.net/manual/zh/function.mysql-connect.php

http://php.net/manual/en/mysql.constants.php#mysql.client-flags http://php.net/manual/zh/mysql.constants.php#mysql.client-flags

Important Note: Please learn mysqli or PDO 重要说明:请学习mysqliPDO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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