MySQL PDO trouble - Working with my PHP/MySQL scrit & DB

Codes here !

Moderators: gesf, Michalio

Post Reply
User avatar
NorseMan
php-forum Fan User
php-forum Fan User
Posts: 65
Joined: Mon Jul 04, 2022 5:25 pm
Location: Near Oslo (Norway)

Ok, the connection gives no error now. I paste in on the line below:

<?php
try {
$pdo = new PDO('mysql:host=localhost; dbname=kjegri1_***;
charset=utf8', 'kjegri1_***', '**********');
$pdo->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$output = 'Unable to connect to the datase server: ' ;
$e->getMessage() . ' in ' .
$e->getFile() . ':' . $e->getLine();
}
#include _DIR_.'../templates/output.html.php';

Now i get problems when i try to connect the file who contains the connection script (db_inc.php) to the ..... argh.... I start over.

When i open the register.php file in the browser i get a ERROR 500. The db_inc.php file is included in this with a included. I paste the script in below. Hope you can help me to find the error, because i dont. Nice if you could help me out with the session_start below also. What is intended to be inserted where I have marked with the 3 question marks between the single quotes? Or should this be emty without the single quotes?

<?php
include $ROOT.'/includes/db_inc.php';
session_start('???');
$ROOT = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']) . '/';
$HOST = 'http://' . $_SERVER['HTTP_HOST'] . '/';
________________
Leon (Norway)
________________

-------
(some of)My configuration is:
Laragon full 5.0 web server with PHP and MySQL installation
(Apache 2.4.47 / PHP 7.4.19 / MySQL 5.7.33)
-------
My toolbox and workbench:
Navicat for MySQL 16.0.13
FileZilla 3.57.1
Among others
-------
Development toolbox:
Adobe DreamWeaver 21.2
Adobe Photoshop 23.1.0
Adobe Illustrator 26.0.2
Adobe InDesign 17.0.1
Adobe XD
Visual Studio Code 1.63.2 (with some extensions enabled)
User avatar
Michalio
Moderator
Moderator
Posts: 337
Joined: Sun Jul 18, 2021 1:33 pm
Location: Poland

You are trying to use the $ROOT variable before it is defined, because the root will never change while script is executing the it should be a definad as constant.
The session_start() can be use without session name.

Offtopic question, did you consider to use the routing (one file will decide what to include based on the url) instead of requesting each file? You can also hide the database connection by interfaces and use the dependency injection. That is modern way to build applications and websites
Free coding lessons: https://php-forum.com/phpforum/viewtopic.php?t=29852
User avatar
NorseMan
php-forum Fan User
php-forum Fan User
Posts: 65
Joined: Mon Jul 04, 2022 5:25 pm
Location: Near Oslo (Norway)

Okey, thanks for your help.
So, then i can leave the session start empty, but when it comes to the root..... Ok, do you wanr me to put it together like this:

<?php
$HOST = 'http://' . $_SERVER['HTTP_HOST'] . '/';
$ROOT = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']) . '/';
include $ROOT.'/includes/db_inc.php';
session_start();
________________
Leon (Norway)
________________

-------
(some of)My configuration is:
Laragon full 5.0 web server with PHP and MySQL installation
(Apache 2.4.47 / PHP 7.4.19 / MySQL 5.7.33)
-------
My toolbox and workbench:
Navicat for MySQL 16.0.13
FileZilla 3.57.1
Among others
-------
Development toolbox:
Adobe DreamWeaver 21.2
Adobe Photoshop 23.1.0
Adobe Illustrator 26.0.2
Adobe InDesign 17.0.1
Adobe XD
Visual Studio Code 1.63.2 (with some extensions enabled)
User avatar
NorseMan
php-forum Fan User
php-forum Fan User
Posts: 65
Joined: Mon Jul 04, 2022 5:25 pm
Location: Near Oslo (Norway)

Now I have tried to reshuffle so __ROOT__ is started in the correct order, but keg gets the error messages anyway. Can you Michalio look at it and tell me what's wrong again, so I do not misunderstand you in any way. It is possible I misunderstand you due to my poor English. Here is the script referred to in the error message. Look at the attached image.

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();
$ROOT = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']) . '/';
$HOST = 'http://' . $_SERVER['HTTP_HOST'] . '/';
require_once(__ROOT__.'/includes/db_inc.php');
const PAGETITLE = 'Båtplasskart og Venteliste';
const HELPBUTTON = "<span id='info_btn' class='w3-badge w3-small w3-white w3-border w3-border-white w3-right' title='Hjelp'>?</span>";

And here is the connection script:

<?php
try {
$pdo = new PDO('mysql:host=localhost; dbname=122387_***ina;
charset=utf8', '122387_***ina', '********');
$pdo->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
$output = 'Unable to connect to the datase server: ' ;
$e->getMessage() . ' in ' .
$e->getFile() . ':' . $e->getLine();
}
#include _DIR_.'../templates/output.html.php';
7.png
7.png (129.03 KiB) Viewed 508 times
________________
Leon (Norway)
________________

-------
(some of)My configuration is:
Laragon full 5.0 web server with PHP and MySQL installation
(Apache 2.4.47 / PHP 7.4.19 / MySQL 5.7.33)
-------
My toolbox and workbench:
Navicat for MySQL 16.0.13
FileZilla 3.57.1
Among others
-------
Development toolbox:
Adobe DreamWeaver 21.2
Adobe Photoshop 23.1.0
Adobe Illustrator 26.0.2
Adobe InDesign 17.0.1
Adobe XD
Visual Studio Code 1.63.2 (with some extensions enabled)
User avatar
Michalio
Moderator
Moderator
Posts: 337
Joined: Sun Jul 18, 2021 1:33 pm
Location: Poland

The __ROOT__ is not defined so the php is trying (by default) to use its name as its value and to include file from path: __ROOT__/includes/db_inc.php, but that path/file doesn't exists.

And the session should started early, if you put session_start after some code then the session will be not available for the code. Additionally the session_start needs access to the http header (to set cookies), so when you print any output like html then the header will be closed and the body opened, so the session_start will throw an error about closed header
Free coding lessons: https://php-forum.com/phpforum/viewtopic.php?t=29852
User avatar
NorseMan
php-forum Fan User
php-forum Fan User
Posts: 65
Joined: Mon Jul 04, 2022 5:25 pm
Location: Near Oslo (Norway)

Ok, I see👍😊
I understand what you say and mean, but I do not quite understand how to do this now. I've messed up my script here now. I have debugged back and forth, it's a totaly mess, so I have come a long way off the road that should lead me further. I can just sit down and try until it's right. I guess it' not the last word you heare from me.
________________
Leon (Norway)
________________

-------
(some of)My configuration is:
Laragon full 5.0 web server with PHP and MySQL installation
(Apache 2.4.47 / PHP 7.4.19 / MySQL 5.7.33)
-------
My toolbox and workbench:
Navicat for MySQL 16.0.13
FileZilla 3.57.1
Among others
-------
Development toolbox:
Adobe DreamWeaver 21.2
Adobe Photoshop 23.1.0
Adobe Illustrator 26.0.2
Adobe InDesign 17.0.1
Adobe XD
Visual Studio Code 1.63.2 (with some extensions enabled)
Post Reply