0

I am trying to do some coursework, where I have to create a webpage that interacts with a mySQL database using PHP.

I am unfamiliar with PHP and I don't know how to connect an SQL database to it.

I have access to a piece of software called "laragon", which seems to be a virtual server program.

So far, I have managed to get a "hello world program" to run.

<HTML><BODY>Test.<br>

<?php echo "PHP test"?>

</BODY></HTML>

Result:

<code><HTML><BODY>Test.<br>PHP test</BODY></HTML></code>

I have been trying to find out how to "connect" PHP to a mySql database through Google, but I could not find anything that clearly shows how to do so. I tried adapting some code I found from https://www.thegeekstuff.com/2017/05/php-mysql-connect/.

<HTML><BODY>Test.<br>

<?php
$conn = new mysqli("localhost", "", "", "DDL.sql");
if ($conn->connect_error)
{die("ERROR: Unable to connect: " . $conn->connect_error);} 
echo 'Connected to the database.<br>';
$result = $conn->query("SELECT id FROM user");
echo "Number of rows: $result->num_rows";
$result->close();
$conn->close();
?>

</BODY></HTML>

Result:

A screenshot, showing an error message: "Access denied for user ''@'localhost' to database 'ddl.sql'"

DDL.sql is a file created using notepad. It contains 5 DDL create statements:

CREATE TABLE if not exists USER (id VARCHAR PRIMARY KEY,
...)

CREATE TABLE if not exists COMMUNITY (id VARCHAR PRIMARY KEY,
style TEXT)

etc.

I opened the largon terminal and opened mySQL in it. The error seems to be a problem with user access rights, so I tried adapting some code that I found in a YouTube video, and entering it into the laragon terminal.

C:\largon\www>cd ..\bin\mysql\mysql-5.1.72-win32\bin

C:\largon\bin\mysql\mysql-5.1.72-win32\bin>mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.72-community MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> GRANT * ON DDL.sql
    -> TO 'USR'@'localhost'
    -> IDENTIFIED BY 'pass';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your 
MySQL server version for the right syntax to use near '* ON DDL.sql
TO 'USR'@'localhost'
IDENTIFIED BY 'pass'' at line 1
mysql> GRANT INSERT, SELECT, DELETE, UPDATE ON DDL.sql
   -> TO 'USR'@'localhost'
   -> IDENTIFIED BY 'pass';
ERROR 1146 (42S02): Table 'ddl.sql' doesn't exist
mysql> GRANT INSERT, SELECT, DELETE, UPDATE ON DDL
    -> TO 'USR'@'localhost'
    -> IDENTIFIED BY 'pass';
ERROR 1046 (3D000): No database selected
mysql> GRANT INSERT, SELECT, DELETE, UPDATE ON DDL.*
    -> TO 'USR'@'localhost'
    -> IDENTIFIED BY 'pass';
Query OK, 0 rows affected (0.01 sec)

mysql>

It seems like I have to create a username and password. I then added the name and password to my adapted PHP test code.

<HTML><BODY>Test.<br>

<?php
$conn = new mysqli("localhost", "USR", "pass", "DDL.sql");
if ($conn->connect_error)
{die("ERROR: Unable to connect: " . $conn->connect_error);} 
echo 'Connected to the database.<br>';
$result = $conn->query("SELECT id FROM user");
echo "Number of rows: $result->num_rows";
$result->close();
$conn->close();
?>

</BODY></HTML>

Result:

A screenshot, showing an error message: Unable to connect: Access denied for user 'USR'@'localhost' to database 'ddl.sql'

I am currently trying to get a test PHP page to run, so that I can work out how to create a PHP page that accesses a mySQL database.

How do I access a mySQL database using PHP?

Programmer S
  • 429
  • 7
  • 21

0 Answers0