0

Guys i'm having common issue. I want to display data from MySQL database into HTML page using PHP.

Using this code:

<html>
<head>

<title>Pulse Sensor Data </title>

</head>
<body>

<?php

$servername = 'localhost'; 
$username = 'root';  
$password = '';

// Connect to database server
mysql_connect('192.168.1.106','root','','database') or die (mysql_error ());

// Select database
mysql_select_db('database') or die(mysql_error());

// SQL query
$strSQL = "SELECT * FROM pulsesensor";

// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);

// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {

   // Write the value of the column id and value
  echo $row['id'] . " " . $row['value'] . "<br />";

  }

// Close the database connection
mysql_close();
?>

</body>
</html>

but i got

mysql_connect(): Access denied for user 'root'@'XXX' (using password: NO) in C:\xampp\htdocs\html.php on line 16 Access denied for user 'root'@'Dell' (using password: NO)

i changed the password the same error appear

mysql_connect(): Access denied for user 'root'@'XXX' (using password: YES) in C:\xampp\htdocs\html.php on line 16 Access denied for user 'root'@'Dell' (using password: YES)

i don't know what to do

chris85
  • 23,846
  • 7
  • 34
  • 51
Tabarek Ghassan
  • 33
  • 1
  • 10
  • 4
    so why did you assign this `$servername = 'localhost';` and using `192.168.1.106` after? – Funk Forty Niner Feb 20 '17 at 21:20
  • 7
    You need to stop using `mysql_*` functions. They have been deprecated for years and don't even exist in current PHP releases. Please study about [PHP Data Objects](http://php.net/manual/en/intro.pdo.php), commonly referred as PDO for short. – sidyll Feb 20 '17 at 21:22
  • possible duplicateof http://stackoverflow.com/questions/8537531/access-denied-for-user-rootlocalhost-using-password-no – Jay Blanchard Feb 20 '17 at 21:26
  • 1
    Is `192.168.1.106` the local computer's IP address? If not, you need to grant permission to connect from your IP on the MySQL server. You should also create a user to use to connect with. Using root/administrator accounts should only be done when you actually need to. – Mike Feb 20 '17 at 21:27
  • You are connecting to `Dell`, `localhost`, or `192.168.1.106`? – chris85 Feb 20 '17 at 21:27
  • ops never notice that :/ i tried to make it access remotely so i used my IP address instead of localhost >> its worked when i changed the IP into localhost but i want it to access remotely – Tabarek Ghassan Feb 20 '17 at 21:30
  • The PHP connecting is remote? – chris85 Feb 20 '17 at 21:31
  • Possible duplicate of [How to allow remote connection to mysql](http://stackoverflow.com/questions/14779104/how-to-allow-remote-connection-to-mysql) – Mike Feb 20 '17 at 21:33
  • thanks for you suggestion i'll read it @sidyll – Tabarek Ghassan Feb 20 '17 at 21:41
  • yes it is .. so i have to create another user instead the root and use it @Mike – Tabarek Ghassan Feb 20 '17 at 21:43
  • i make static IP for further step cause i want to access remotely that's the why i used 192.168.1.106 but it still local @chris85 – Tabarek Ghassan Feb 20 '17 at 21:46
  • @TabarekGhassan You need to forward port 3360 from your router to the static IP of your server in order to allow remote connections. – Mike Feb 20 '17 at 22:02

2 Answers2

1

Your connection string is using the ip address and root is not configured to access via the ip address you are using for the host. You will have to change it to localhost or add that permission to your mysql server for the root user.

I would suggest that you not do that, but create a new mysql user for your development.

Also, from @sidyll you will not want to use the mysql_* functions and use PDO functions instead.

Ray Hunter
  • 15,137
  • 5
  • 53
  • 51
  • thanks a lot its worked but in localhost environment but if i want to make it remotely is the new user will help me in this case?.. sorry for these questions its my first experiment @srayhunter – Tabarek Ghassan Feb 20 '17 at 21:51
  • That is correct. I would recommend creating a new user that is for this project's database and tables. Then you can restrict the new user's access to the database and tables for the project. I would recommend researching mysql users and how access and permissions work. – Ray Hunter Feb 20 '17 at 21:53
-1

Try this:

mysql_connect($servername,'root','','database') or die (mysql_error ());
chris85
  • 23,846
  • 7
  • 34
  • 51
Ubi Gurung
  • 20
  • 2