Up until defining redirects and adding constants ...
all my controller code and my functions were working and testing fine including my database connection and routes.
Everything was being called just fine within my MVC.
NOW I HAVE ...
Moved working function db_connect() and function result_to_array($result)
FROM
models/post.php
/**
* connects to database server and selects database
* @return (bool) resource
function db_connect()
{
$connection = @mysql_connect('localhost','root','Nogard');
if (!$connection)
{
return false;
}
if(!mysql_select_db('jobBoard_vjr', $connection))
{
return false;
}
return $connection;
}
TO
new file
/database.php
function db_connect() using params <--- Changed
function result_to_array($result) <--- Not Changed
<?php
/**
* connects to database server and selects database
* @return (bool) resource
*/
echo 'this is the database.php';
function db_connect()
{
$connection = @mysql_connect(HOST,USERNAME,PASSWORD);
if (!$connection)
{
return false;
}
if(!mysql_select_db(DATABASE, $connection))
{
return false;
}
return $connection;
}
/**
* turns mysql resource into array
* @param resource $result
* @return array
*/
function result_to_array($result)
{
$result_array = array();
for ($i=0; $row = mysql_fetch_array($result) ; $i++)
{
$result_array[$i] = $row;
}
return $result_array;
}
?>
new file
/config.php
Change routes, database params and App directory here.
<?php
// routes url to controller and view
$routes = array(
array('url' => '/^posts\/(?P<id>\d+)$/', 'controller' => 'posts', 'view' => 'show'),
array('url' => '/^posts\/(?P<id>\d+)\/edit$/', 'controller' => 'posts', 'view' => 'edit')
);
// Database connection params
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', 'Nogard');
define('DATABASE', 'jobBoard_vjr');
// The server root
define('SERVER_ROOT', $_SERVER['DOCUMENT_ROOT']);
// Directory structure
define('DS', '/');
// Application Directory - need to change this to your app folder.
// define('APP_ROOT', '/videojobresume/jobBoardTest/');
define('APP_ROOT', 'videojobresume/jobBoardTest');
//echo APP_ROOT ;
// address of website.
define('WEBSITE', 'http://localhost/');
// echo WEBSITE ;
// MVC paths
// IF YOU RUN THIS ON LINUX YOU MAY NEED TO ADD .DS after the SERVER_ROOT.
// example : define('MODEL_PATH', SERVER_ROOT.DS.APP_ROOT.DS.'models'.DS);
// MVC paths
define('MODEL_PATH', SERVER_ROOT.APP_ROOT.DS.'models'.DS);
define('VIEW_PATH', SERVER_ROOT.APP_ROOT.DS.'views'.DS);
define('CONTROLLER_PATH', SERVER_ROOT.APP_ROOT.DS.'controller'.DS);
//echo MODEL_PATH;
// lib includes
include('database.php');
?>
When I place include('database.php'); in config.php I get no echo from database.php along with the following error.
This is the index.php file
Fatal error: Call to undefined function db_connect() in C:\xampp\htdocs\videojobresume\jobBoardTest\models\post.php on line 149
error connectingUnknown MySQL server host 'HOST' (11001)
When I place include('database.php'); in models/post.php ... which was working just fine with index.php and controller/posts.php ... till I made the params adjustments ... I get the following echo and error.
This is the index.php file
this is the database.php
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\videojobresume\jobBoardTest\models\post.php on line 157
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\xampp\htdocs\videojobresume\jobBoardTest\models\post.php on line 157
Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\videojobresume\jobBoardTest\models\post.php on line 161
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\videojobresume\jobBoardTest\models\post.php on line 161
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\videojobresume\jobBoardTest\models\post.php on line 163
SO I believe I am having issues with the params when I changed ...
FROM
function db_connect()
{
$connection = @mysql_connect('localhost','root','Nogard');
TO
function db_connect()
{
$connection = @mysql_connect(HOST,USERNAME,PASSWORD);
and issues with my routes when I moved them to config.php file.
// routes url to controller and view
$routes = array(
array('url' => '/^posts\/(?P<id>\d+)$/', 'controller' => 'posts', 'view' => 'show'),
array('url' => '/^posts\/(?P<id>\d+)\/edit$/', 'controller' => 'posts', 'view' => 'edit') );
index.php
<?php
echo '<p>This is the index.php file</p>';
include('config.php');
//include($_SERVER['DOCUMENT_ROOT'].APP_ROOT.'/config.php');
function dispatcher($routes)
{
// Requested URL
$url = $_SERVER['REQUEST_URI'];
//echo $url;
// Removes Apllication root from url
$url = str_replace('/'.APP_ROOT.'/', '', $url);
//echo APP_ROOT;
// holds the named captures
$params = array();
// becomes true if $route ['url'] = $url
$route_match = false;
// loops over $routes looking for a match
foreach($routes as $urls => $route)
{
//echo 'looking for match.. <br />';
// if match found appends $matches to $params
// sets $route_match to true and exits loop
if(preg_match($route['url'], $url, $matches))
print_r ($matches);
{
$params = array_merge($params, $matches);
//echo "match found <br />";
$route_match = true;
break;
}
}
// if no route matched displays error
if(!$route_match) { exit('no route found'); }
//include($_SERVER['DOCUMENT_ROOT'].'/videojobresume/jobBoardTest/controller/'.$route['controller'].'.php');
include(CONTROLLER_PATH.$route['controller'].'.php');
}
dispatcher($routes);
?>
controller/posts.php
<?php
include($_SERVER['DOCUMENT_ROOT'].'/videojobresume/jobBoardTest/models/post.php');
switch ($route['view']) {
case "show":
$post = find_post($params['id']);
include($_SERVER['DOCUMENT_ROOT'].'/videojobresume/jobBoardTest/views/posts/show.php');
break;
case "new":
echo "this is the new action";
break;
}
?>