0

I have made a basic database for a grocery store using MySQL.

I would like to make a simple website that uses the data from my database.

I am trying to to use php within a html file to establish a connection to the database.

<!DOCTYPE html>

<?php
    $host="127.0.0.1";
    $port=3306;
    $socket="";
    $user="root";
    $password="";
    $dbname="greenwichgrocers";

    try {
    $dbh = new PDO("mysql:host={$host};port={$port};dbname={$dbname}", $user, $password));
    } catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    }
?>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <h1>PHP connect to MySQL</h1>


</body>
</html>

The connection code was automatically generated by MySQL Workbench. I am not sure how it is supposed to be formatted as I am completely new to php.

The page will only show this:

enter image description here

Any help would much appreciated

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Gary
  • 19
  • 6
  • You don't seem to be closing the PHP code block – apokryfos Jul 17 '20 at 08:23
  • 1
    Also if you're seeing the PHP code in the page, then maybe PHP is not enabled or you're not using it correctly. Have you installed PHP in your webserver? Have you named your file with a .php extension? Have you accessed your file over a proper URL (e.g. `http://localhost/yourfile.php` or similar)? – ADyson Jul 17 '20 at 08:24
  • @ADyson Hi, The file is called index.html, i have created it on my desktop using notepad and am viewing it in chrome from my local drive. – Gary Jul 17 '20 at 08:28
  • 1
    Ok well all of those things are a problem. Rename it to .php, and load it through a proper local webserver, so that the code can be executed by the PHP interpreter. And close your PHP code block before the HTML as mentioned in the first comment. And find a proper code editor / IDE, there are tons of free ones available which understand PHP syntax. Using Notepad for programming is a form of masochism. – ADyson Jul 17 '20 at 08:29
  • @ADyson Ok i have apache running via XAMPP and i have renamed my file to index.php, this is probably really stupid but how would i run the webserver? – Gary Jul 17 '20 at 08:34
  • @Gary you dont know how run the webserver? – KUMAR Jul 17 '20 at 08:37
  • well hopefully if you installed XAMPP then it's set up and running already as an automated service. I don't know XAMPP specifically but I imagine it has a control panel where you can check, or at least you might see it in the Services tool in Windows. But to make your code work through the server you have to put your .php file in the root folder of the webserver (again the configuration app ought to tell you where that is, hopefully), and then load it in your browser via a suitable URL as I mentioned e.g. `http://localhost/something.php` – ADyson Jul 17 '20 at 08:37
  • 1
    @Gary if you dont know how to run php code with HTML with.php extension and not installed , running a proper local webserver than please go to the internet and search how run first .php program. – KUMAR Jul 17 '20 at 08:43
  • @Gary I suspect you might find a tutorial such as this one helpful: https://www.ionos.co.uk/digitalguide/server/tools/xampp-tutorial-create-your-own-local-test-server/ . Obviously you seem to have done all the installation steps already, but the sections from "The XAMPP Control Panel" onwards might be helpful for you in ensuring everything is up and running, and that you put your test code in the right folder, and use the right URL to run it. – ADyson Jul 17 '20 at 08:51
  • @Gary Also this list of code editors and IDEs might be valuable as well: https://codingcyber.org/best-free-php-ide-for-web-developers-674/ . From personal experience, Notepad++ is fine for some quick-and-dirty bits and pieces, and Visual Studio Code is pretty decent as a proper IDE, once you've installed some PHP-related plugins. – ADyson Jul 17 '20 at 08:53

3 Answers3

1

To run php code:

  1. you will need php environment installed
  2. php file should ends in .php in most case
  3. a server is needed if you want to see result in browser (php command line server / nginx / apache etc.)
Gavin Kwok
  • 98
  • 6
  • Hi Gavin, I have php version 7.4.4 installed via apache and xampp, apache is running and i have changed my file extensions to .php, however it still displays as plain text when i open it in chrome. – Gary Jul 17 '20 at 08:43
  • @Gary how you running your `.php` file in browser? – KUMAR Jul 17 '20 at 08:44
  • @Gary check these one by one: 1. where is your root of server, does it match your php file location 2. does your url location matches your php file location – Gavin Kwok Jul 17 '20 at 08:47
  • 1
    to make things simple, just `echo 'hi';` in php section to see if php works – Gavin Kwok Jul 17 '20 at 08:51
1

If you already have setup your server to run PHP script such as Apache.

  1. Rename your file with .php extension

  2. Try this connection

    try { $dbh = new PDO("mysql:host=$host;port=$port;dbname=$dbname", $user, $password); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); }

  3. You have an excess ) in your connection string.

Bevin NIno
  • 75
  • 7
0

Try:

<?php echo "Works"; ?>

See: How do I get PHP errors to display?

In PDO line you have got an error

$dbh = new PDO("mysql:host=".$host.";port=".$port.";dbname=".$dbname, $user, $password);