-1

I am trying to use the following to use database via a function. This is not working as expected. This works outside of the function but not in. The page returns blank when the PDO line is used.

function db($inputStatement){
        $link = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
        $statement = $link->prepare($inputStatement);
      echo $inputStatement.'-XX';
  }
  db("SELECT * FROM users");
echo "<br />END OF test.php";
Derple
  • 865
  • 11
  • 28

1 Answers1

1

One way is to define $db** as contants instead, then you can use them inside functions

<?php

  define( 'DB_HOST', 'host' );
  define( 'DB_NAME', 'name' );
  define( 'DB_USER', 'user' );
  define( 'DB_PASS', 'pass' );

  function db( $inputStatement )
  {
      $link = new PDO( 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS );
      $statement = $link->prepare( $inputStatement );
      echo $inputStatement . '-XX';
  }

?>
Tom
  • 691
  • 4
  • 7