-5

I have copied the PDO connection script directly from the php.net documentation, however, it fails to work, because it gets this error.

<?php
  global $pdo = new PDO('mysql:host=localhost;dbname=pionear', "root", "");
?>

http://php.net/manual/en/pdo.connections.php

Jack M
  • 43
  • 8
  • 3
    Please cite the link so the docs can be reviewed. – Spechal May 21 '17 at 06:08
  • `PDO('dblib:host=your_hostname;dbname=your_db;charset=UTF-8', $user, $pass);` – LF00 May 21 '17 at 06:09
  • Doubtful this is actually a copy/paste from the PHP documentation, since a Google search can't find your code. https://www.google.com/search?num=20&safe=active&q="global+%24pdo+%3D+new+PDO"&oq="global+%24pdo+%3D+new+PDO" – Brad May 21 '17 at 06:11
  • Just cited the docs, despite changing some of the variables to string literals, it is more or less the same. – Jack M May 21 '17 at 06:12
  • 2
    Oh, great, now you edit your question to remove the syntax error? Fantastic... – Brad May 21 '17 at 06:12
  • It's not a doc problem; you replaced $user and $pass with your own data and caused a parse error. – Spechal May 21 '17 at 06:13
  • I typed it in incorrectly, and that was not the issue. – Jack M May 21 '17 at 06:13
  • @Brad the error would have been different if that was the actual mistake. – Tom Udding May 21 '17 at 06:14
  • @TomUdding We don't know what was on other lines. Either the mistake was caused by the broken quotes, or the mistake is elsewhere. Either way, this question is a waste of everyone's time. – Brad May 21 '17 at 06:15
  • Possible duplicate of [PHP Syntax Error in Setting Global Variable](https://stackoverflow.com/q/1145970/5914775) – Tom Udding May 21 '17 at 06:18
  • Possible duplicate of [PHP Parse/Syntax Errors; and How to solve them?](http://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Qirel May 21 '17 at 06:46

1 Answers1

1

You cannot declare a variable in the global scope like that. You have to declare it as a normal variable and than access it through global (e.g. in a function):

$pdo = new PDO('mysql:host=localhost;dbname=pionear', "root", "");

function something() {
    global $pdo;
    $pdo->doSometing();
}

something();

You can check the documentation on the global keyword for more information. If you do not want to use the global keyword you can instead use $GLOBALS (which is a 'superglobal', thus no need to do global $pdo;).

Tom Udding
  • 2,264
  • 3
  • 20
  • 30
  • 2
    Good catch. I'm surprised this threw a syntax error, but it makes sense. – Brad May 21 '17 at 06:17
  • 1
    @Brad but a different one than the title, which only goes to show it was an edit to correct the obviousness of not a doc problem. It would have caused ```Parse error: parse error, expecting `','' or `';'' in Command line code on line 1``` – Spechal May 21 '17 at 06:18
  • 1
    @Spechal It's definitely not a documentation problem as Jack's code was not copied/pasted from the docs as he asserted. – Brad May 21 '17 at 06:19
  • @Brad I saw the edit, he replaced $user and $pass with "root, "" and forgot a quote. – Spechal May 21 '17 at 06:20
  • 1
    @Spechal He also added `global`. – Brad May 21 '17 at 06:21
  • 1
    Generally speaking, it's not a good idea to use global variables in the first place - they should be passed as arguments to the function, that way you have more control over which variables are in what scope. – Qirel May 21 '17 at 06:48