为什么连接使用包含一次失败与pdo失败?

Why Connecting using include once is failing with pdo?
When I connect to the database including a connection page it gave me an error, but when I put the connecting code in and I remove include I do not get errors. What might be the problem?

Error Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\tish\A\view.php on line 167

Connecting page:

<?php
  function connected_Db(){
    try {
      $con = new PDO('mysql:host=localhost;dbname=tish_database;charset=utf8','root','');
    } catch(PDOException $e){
      echo 'Connection failed'.$e->getMessage();
    }
  }
  connected_Db();
?>

the way I include it in other pages:

include_once('pdo.inc.php');
connected_Db();
global $con;

You can't make a function variable global from the global scope. The variable $con has long been GC'ed: it's deallocated once the function has returned. Try this:

function connected_Db()
{
   return new PDO('mysql:host=localhost;dbname=tish_database;charset=utf8','root','');
}
//global scope:
$con = connect_Db();

If you insist on using the global keyword, which is not a great idea, you should've used it like this:

function connected_Db()
{
    global $con;//use the global var, set at top of function
    $con = new PDO('mysql:host=localhost;dbname=tish_database;charset=utf8','root','');
}
$con = null;// declare global var (optional, but a notice will be issued if not declared)
connect_Db();
var_dump($con);

As you can see, I have -like deceze and YourCommonSense suggested- removed the try-catch block (which you don't need here). It also goes to show that you don't really need the function call either. Your script would work perfectly fine (or marginally faster even) if you were to write this:

//global scope (or whatever scope you need it to be)
$con = new PDO('mysql:host=localhost;dbname=tish_database;charset=utf8','root','');

That way, you're not calling a function that needn't be called.

You never make $con available outside of the function connected_Db. Learn about variable scope. You should return it from the function and use it like so:

require_once 'pdo.inc.php';

$con = connected_Db();

Which also means you should not catch connection errors (at least not there), since if the function can't return a PDO connection your script doesn't need to continue.

You're using global incorrectly. It should go in the function, to specify that the variable is from outside the function, i.e. in the global scope.

e.g.

function connected_Db()
{
    global $con;

    $con = null;
    try
    {
       $con = new PDO(...);
    }
    catch(PDOException $e)
    {
        die('Could not connect: '.$e->getMessage());
    }
}

But it's better to return the connection rather than pull it in from global scope.

i.e.

function connected_Db()
{
    try
    {
       return new PDO(...);
    }
    catch(PDOException $e)
    {
        die('Could not connect: '.$e->getMessage());
    }
}

First, you need to tell PDO to throw exceptions on connect errors.
Next, you shouldn't catch them, at least not using try .. catch.

So, the code have to be

pdo.inc.php:

<?php
function connected_Db(){
    $dsn  = 'mysql:host=localhost;dbname=tish_database;charset=utf8';
    $opt  = array(
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    );
    return new PDO($dsn,'root','', $opt);
}

(you may also wish to pass connect options via function parameters)

other file

include_once('pdo.inc.php');
$con = connected_Db();