Mysql_connect error

In the following code, I see the echo1 instruction, after which I do not see anything printed in the user interface. The username and password are correct. But PHP does not seem to connect to MySQL. I don’t even see the statement about death, what I am doing wrong. After mysql_connect is encountered, the rest of the code does not work:

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

The problem was that the ph5-mysql driver was not installed. Installs and works. The mysql_connect () function now works. thanks for the help

0
source

You must fix the error. Add:

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

at the beginning of your script

+3
source

If you use your code in the same way as this, then it is vulnerable to SQL Injection. I highly recommend using mysql_real_escape_string when pasting data into your database to prevent SQL injection as a quick fix or it is better to use PDO or MySQLi .

If you intend to use mysql_* , I would recommend reading the chapter of the PHP manual on mysql_* functions where they indicate that this extension is not recommended for writing new code. Instead, they say that you should use either MySQLi or PDO_MySQL .

I also checked mysql_connect and found a strange regularity, which is - if you use the arguments on mysql_connect , then it cannot connect, and in my case, when I tested it, it was just described, try instead:

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

Replace " with ' , as shown in the PHP Manual examples, and it might work!

EDITED

For those who do downvote - TRY first! I tested it on my server using " and it gave me an error: Warning: mysql_connect(): Access denied for user . I am using PHP version 5.4.6!

Log in to your server with SSH and run php --modules - if you do not see mysql in the list then this is the cause of your fatal error.

+1
source

No conclusion - this is a fatal error. The only possible fatal error is the "undefined function mysql_connect (unless something really goes wrong). This means that the mysql library is not installed or it simply cannot be included in the php.ini .

Check the specified file, and while you are on it, turn error_reporting on.

+1
source

For Tomcat 7, the default dir for php.ini is actually C: \ windows. Thus, modified (including modules, extensions, etc.) Php.ini should be copied to C: \ windows. Then run phpinfo () ;. It should work fine even on Tomcat 7 next to PHP 5.x. I had this problem and was scared a lot when I wanted the mysql statements to work.

Even after following all the instructions from the answers in [779246] ( Run php application using tomcat? ), Mysql statements did not work (Tomcat 7, PHP 5.2.16, PECL 5.2.5, MySql 5.6 on Windows 7). So I started learning "phpinfo ()"; withdraw carefully and use the solution as indicated in the previous paragraph. Be sure to include the mysql dll extension as well as the PDO extensions in php.ini if ​​you want to use PDO (this is the future of simple mysql_ * methods in PHP).

0
source

Source: https://habr.com/ru/post/1442481/


All Articles