Is the connect() function depecrated or something? When I try to connect to a mysql database using that function, my page stops working? i'm doing jeffrey ways 30daysjquery course lesson 25
here's the code in my index.php file
<?php
require 'functions.php'
if ( isset($_POST['q'])) {
connect();
}
include 'views/index.tmpl.php'
?>
This is my code in my functions.php file
<?php
function connect(){
global $pdo; // set it as global so other functions can access it
$pdo = new PDO("mysql:host=localhost;dbname=sakila", "root", "root");
}
function get_actors (){
}
?>
Might it be that I do not have access to the content that's in functions.php? if it is how do I create access?
Your function should maybe return a connection:
function connect(){
$pdo = new PDO("mysql:host=localhost;dbname=sakila", "root", "root");
return $pdo;
}
Then, your function get_actors()
could be
$db_connection = connect();
get_actors( $db_connection );
function get_actors( $pdo ) {
//
// You use the database connection $pdo to get actors
//
}
You have to add a parameter to your function get_actors
. No need to modify the code into the function.
When I checked MAMP this is the information I have if I want to connect to a MySQL server using a script.
Host localhost
Port 8889
User root
Password root
Socket /Applications/MAMP/tmp/mysql/mysql.sock
thrown in /Applications/MAMP/htdocs/lesson-25/functions.php on line 5
[09-Nov-2014 23:01:47 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in /Applications/MAMP/htdocs/lesson-25/functions.php:5
Stack trace:
#0 /Applications/MAMP/htdocs/lesson-25/functions.php(5): PDO->__construct('mysql:host=loca...', 'root', 'root')
#1 /Applications/MAMP/htdocs/lesson-25/functions.php(9): connect()
#2 /Applications/MAMP/htdocs/lesson-25/index.php(3): require('/Applications/M...')
#3 {main}