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

NorseMan

Member
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'] . '/';
 
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
 
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();
 
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
 
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
 
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.
 
Back
Top