-1

While I am trying to connect with mysql in PHP.

It is showing its code in the browser instead of running the code.

please help I am new to PHP.

<?php
    $con=mysqli_connect('localhost','root','','cki');
    if(!$con)
    {
        echo "not connected";
    }else{
        echo "connected";
    }
?>
Naveed Ramzan
  • 3,565
  • 3
  • 25
  • 30

1 Answers1

0

this is OS specific, (if the user works in MYSQL shell that is) AS A SECURITY RESTRAINT you also may not connect from php with root / no password so run $ sudo mysql_secure_installation first

EG:

$ sudo mysql_secure_installation

follow the prompts set a root password

then login to mysql as that root user and do this

mysql> GRANT ALL PRIVILEGES ON cki.* TO 'new_user'@'localhost' IDENTIFIED BY 'new_password';

if you are using centos you do

  $ sudo yum install php php-mysql mysql-server
  $ sudo service httpd restart

if you are using debian you do

  $ sudo apt-get install php php-mysql mysql-server
  $ sudo service apache2 restart

then you ensure that you have turned off selinux in /etc/sysconfig/selinux

finally

<?php
    $con=mysqli_connect('localhost','new_user','new_password','cki');
    if(!$con)
    {
        echo "not connected";
    }else{
        echo "connected";
    }
?>
Mr Heelis
  • 2,370
  • 4
  • 24
  • 34