-5

I have been working on learning PDO. Basically what I am trying to do is display names that are off work on any given day of the year.

I created a prepared statement that gives me the names, based on a date set in a variable. It's working as expected.

The problem I am having is displaying that on a seperate page. Lets say my page with the PDO statement is called vacationnames.php

Depending on the date I need to display those names on 1 of 4 different pages called calendar1.php or calendar2.php...etc.

Here is the code I have: vacationnames.php

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES   => false,
];
$db = new PDO($dsn, $user, $pass, $opt);

$sth = $db->prepare("
SELECT (employee.name)Name
, vacation_picks._date1 as Date
FROM employee
LEFT OUTER JOIN
vacation_picks on
employee.employee_id = vacation_picks.employee_id
WHERE vacation_picks._date1 = :day
");

This is on one of the calendar.php pages

$myVariable = ('2017-1-1');
// This executes the SQL query, binding the parameter as we go.
$sth->execute(array(
':day' => $myVariable
));

// echo $myVariable;
foreach($sth as $row) { 
 echo $row['Name'];
} 

Even putting the 'Day One' php on calendar.php with vacationnames.php required_once gives me no result. I would prefer to have it referenced with even less code on calendar.php if possible.

Thanks for looking.

  • *"The problem I am having is displaying that on a seperate page."* - you can use sessions for this. However, this sounds more like an out of scope issue. – Funk Forty Niner Oct 16 '17 at 11:43
  • [Sometimes you can use prepared statements for the multiple execution of a prepared query. This feature would have been more useful if it was possible to execute a statement prepared in another PHP instance. But alas - it is not](https://phpdelusions.net/pdo#multiexec) – Your Common Sense Oct 16 '17 at 11:45
  • 1
    Basically, your code should emit an "Undefined variable $sth" error, because, as you could guess, nowhere $sth can be seen defined in calendar.php – Your Common Sense Oct 16 '17 at 11:47
  • 1
    The name of the calendar pages sound like you should just have 1 page, and have the number passed in dynamically. Where is the `require_once` called? – chris85 Oct 16 '17 at 11:53

1 Answers1

-1

You could use SESSIONS for this. Store your post values to a SESSION variable and you can then call on that variable anywhere in your site if the page contains session_start() at the top

Here is a link to the manual - http://php.net/manual/en/reserved.variables.session.php

example

$_SESSION['name'] = $_POST['firstname'];

I hope this gets you going

Sean Konig
  • 124
  • 1
  • 12