1

I need to create a website which allows a user to create a job which is saved to a database and then all the jobs can be viewed from a different page on the website. I've managed to get this to work but I've had to duplicate all of the MySQL log in set up in PHP. I'd like to avoid this duplication and tried using an autoloader but with no success. Basically the first 5 variables are repeated from a separate page where I'm adding the data into the database and I'd like to avoid this if possible. The code itself works I just feel like there must be a way to avoid repetition here.

    <?php
        $server = '192.168.56.2';
        $username = 'student';
        $password = 'student';



        $schema = 'assignment';
        $pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);

                $file ='section3.php';
                echo 'Last Edited: ' .date('d/m/Y', filemtime($file));
                $results = $pdo->query('SELECT * FROM jobs');
                foreach ($results as $row) {
                echo '<p>' . $row['title']."   "  . $row['salary'] ."  ".  $row['location'] ." ". $row['description'].'</p>';
                } 
                ?>
2
  • put the code in a single file and use include? Commented Dec 10, 2015 at 23:35
  • or put the code in a single file and add the filepath under auto_prepend_file in the php.ini file and restart you php Commented Dec 10, 2015 at 23:37

2 Answers 2

1

Put your DB credentials into separate php-file and require_once it from each script where needed

db.php:

$server = '192.168.56.2';
$username = 'student';
$password = 'student';

index.php:

require_once('db.php');
$schema = 'assignment';
$pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);
// ...

Also RTM on difference between

  • include
  • include_once
  • require
  • require_once
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help really appreciated!
0

The usual approach is to place the database connection data (and usually the code that creates the connection) into a separate file and then require_once() that file in each page that needs to connect to the db. eg:

db_connect.php:

<?php
    $server = '192.168.56.2';
    $username = 'student';
    $password = 'student';

    $schema = 'assignment';
    $pdo = new PDO('mysql:dbname=' . $schema . ';host=' . $server, $username, $password);

otherpage.php:

<?php
    require_once 'db_connect.php';
    $file ='section3.php';
    echo 'Last Edited: ' .date('d/m/Y', filemtime($file));
    $results = $pdo->query('SELECT * FROM jobs');
    foreach ($results as $row) {
    echo '<p>' . $row['title']."   "  . $row['salary'] ."  ".  $row['location'] ." ". $row['description'].'</p>';
    } 
    ?>

As usual, I leave error handling as an exercise for the reader.

1 Comment

That's great thanks for that, I was hoping for a simple answer and got one!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.