简体   繁体   English

mysql_connect错误

[英]mysql_connect error

In the following code I see echo1 statement, after which I do not see anything printed on the UI.The username and password is correct. 在下面的代码中,我看到了echo1语句,此后我看不到UI上打印的任何内容。用户名和密码正确。 But PHP doesn't seem to connect to MySQL. 但是PHP似乎没有连接到MySQL。 Don't even see the die statement what am I doing wrong. 甚至看不到死者声明我在做什么错。 After mysql_connect is encountered the rest of the code doesn't work: 遇到mysql_connect之后,其余代码将不起作用:

<?php    
echo "echo1==========";
$con = mysql_connect("localhost","root", "xxxx123") or die("Error connecting to database");
echo "+++++++++ echo2";
echo $con;
mysql_close($con);
  ?>          

You should be mising an error. 您应该误会一个错误。 Add : 添加:

ini_set('display_errors', 1);
error_reporting(E_ALL);

at the beggining of your script 在您的脚本开始时

If you use your code just like this then it's vulnerable for SQL Injection. 如果像这样使用代码,则很容易受到SQL注入的攻击。 I would strongly recommend using mysql_real_escape_string as you insert data into your database to prevent SQL injections , as a quick solution or better use PDO or MySQLi . 我强烈建议您在将数据插入数据库时​​使用mysql_real_escape_string来防止SQL注入 ,这是一种快速解决方案,或者更好地使用PDOMySQLi

If you are going to use mysql_* then I'd recommend reading the PHP manual chapter on the mysql_* functions, where they point out, that this extension is not recommended for writing new code. 如果要使用mysql_*则建议阅读有关mysql_*函数的PHP手册,其中指出,不建议使用此扩展名编写新代码。 Instead, they say, you should use either the MySQLi or PDO_MySQL extension. 他们说,相反,您应该使用MySQLiPDO_MySQL扩展。

I also checked mysql_connect and found a weird regularity which is - if you use " on mysql_connect arguments, then it fails to connect and in my case, when I was testing it, it happened just described way, so, please try this instead: 我还检查了mysql_connect ,发现了一个怪异的规律性-如果在mysql_connect参数上使用" ,那么它将无法连接,在我的情况下,当我对其进行测试时,它只是按照上述方式发生,因此,请尝试以下方法:

$con = mysql_connect('localhost','username','password');

Replace " to ' as it's shown in the PHP Manual examples and it might work! PHP手册示例中所示,将"替换为' ,它可能会起作用!

EDITED 已编辑

For those who downvote - TRY first! 对于那些不赞成投票的人-首先尝试 I tested it on my server with " and it gave me an error: Warning: mysql_connect(): Access denied for user . I use PHP version 5.4.6! 我在服务器上使用"对其进行了测试,它给了我一个错误: Warning: mysql_connect(): Access denied for user 。我使用PHP 5.4.6版!

Login to your server with SSH and run php --modules - if you don't see mysql in the list - then it's the reason of your fatal error. 使用SSH登录到服务器并运行php --modules如果列表中没有mysql ,则这是致命错误的原因。

No output means a fatal error. 没有输出表示致命错误。 The only possible fatal error is "undefined function mysql_connect (unless something's really messed up somewhere). This means the mysql library is not installed, or it might just not be enabled in the php.ini file. 唯一可能发生的致命错误是“未定义的函数mysql_connect (除非某处确实被弄乱了)。这意味着未安装mysql库,或者可能未在php.ini文件中启用它。

Check said file, and while you're at it turn error_reporting on. 检查所述文件,然后在打开时打开error_reporting

问题是没有安装ph5-mysql驱动程序。安装并使其运行..现在mysql_connect()函数可以运行了。.感谢所有帮助

For Tomcat 7, the default dir for php.ini is actually C:\\windows. 对于Tomcat 7,php.ini的默认目录实际上是C:\\ windows。 So modified (enabling modules, extensions etc.) php.ini should be copied to C:\\windows. 修改后的(启用模块,扩展等)应该将php.ini复制到C:\\ windows。 Then run phpinfo();. 然后运行phpinfo();。 It should work fine even on Tomcat 7 along side PHP 5.x. 即使在附带PHP 5.x的Tomcat 7上,它也可以正常工作。 I had this problem and struggled a lot when I wanted the mysql statements to work. 当我想要mysql语句工作时,我遇到了这个问题,并且挣扎了很多。

Even after following all instructions from answers in [779246] ( Run a php app using tomcat? ), mysql statements didn't work (Tomcat 7, PHP 5.2.16, PECL 5.2.5, MySql 5.6 on Windows 7). 即使按照[779246]中的答案进行了所有说明( 使用tomcat运行php应用程序? ),mysql语句也不起作用(Tomcat 7,PHP 5.2.16,PECL 5.2.5,Windows 7上的MySql 5.6)。 So I started looking into "phpinfo();" 所以我开始研究“ phpinfo();”。 output carefully and used solution as mentioned in earlier paragraph. 仔细输出并使用前面段落中提到的解决方案。 Please be sure to enable mysql dll extension and also PDO extensions in php.ini if you would like to use PDO (which is future of plain mysql_* methods in PHP). 如果您想使用PDO(这是PHP中普通mysql_ *方法的未来),请确保在php.ini中启用mysql dll扩展以及PDO扩展。

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

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