Mit CPC Mit CPC - 2 months ago 7
Bash Question

Can't connect MySQLfrom a PHP script

I need to learn a transactional database, but I can't connect to MySQL:

#!/usr/bin/php

<?php
$link = new mysql_connect('localhost', 'root', '123456789');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>


I get the output:


PHP Fatal error: Class 'mysql_connect' not found in /root/test01.php on line 4


What is the problem, and how can I solve it?

Answer

removing 'new' as below; because mysql_connect is a function.

<?php
$link = mysql_connect('localhost', 'root', '123456789');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>