Jump to content
Larry Ullman's Book Forums

Undefined Variable: Dbc


Recommended Posts

I am getting this error coming up when i try to run my script, it is showing on all scripts that require the database dbc connection.

 

Here is the error that is showing on the knowledge_is_power index page, does Larry or anyone here know how to fix this?

 

An error occurred in script 'C:\xampp\htdocs\knowledge_is_power\www\index.php' on line 35:

Undefined variable: dbc

Array

 

(

 

[0] => Array

 

(

 

[file] => C:\xampp\htdocs\knowledge_is_power\www\index.php

 

[line] => 35

 

[function] => my_error_handler

 

[args] => Array

 

(

 

[0] => 8

 

[1] => Undefined variable: dbc

 

[2] => C:\xampp\htdocs\knowledge_is_power\www\index.php

 

[3] => 35

 

[4] => Array

 

(

 

[GLOBALS] => Array

 

*RECURSION*

 

[__utma] => 111872281.92522752.1309196757.1309196757.1309196757.1

 

[Drupal_toolbar_collapsed] => 0

 

[Drupal_tableDrag_showWeight] => 0

 

[sESSbce8863eabfd478fab0d5a5077b9e512] => RPzBUmLIJ_x2rxRSAkhhveaBQcp8skL0l4hpKmbNbtg

 

[phpSESSID] => 2631qhcn4o4i6u50d6h4sdrtm0

 

[MIBDIRS] => C:/xampp/php/extras/mibs

 

[MYSQL_HOME] => \xampp\mysql\bin

 

[OPENSSL_CONF] => C:/xampp/apache/bin/openssl.cnf

 

[php_PEAR_SYSCONF_DIR] => \xampp\php

 

[phpRC] => \xampp\php

 

[TMP] => \xampp\tmp

 

[HTTP_HOST] => localhost

 

[HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1

 

[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

 

[HTTP_ACCEPT_LANGUAGE] => en-us,en;q=0.5

 

[HTTP_ACCEPT_ENCODING] => gzip, deflate

 

[HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.7

 

[HTTP_CONNECTION] => keep-alive

 

[HTTP_REFERER] => http://localhost/knowledge_is_power/www/register.php

 

[HTTP_COOKIE] => __utma=111872281.92522752.1309196757.1309196757.1309196757.1; Drupal.toolbar.collapsed=0; Drupal.tableDrag.showWeight=0; SESSbce8863eabfd478fab0d5a5077b9e512=RPzBUmLIJ_x2rxRSAkhhveaBQcp8skL0l4hpKmbNbtg; PHPSESSID=2631qhcn4o4i6u50d6h4sdrtm0

 

[PATH] => C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\QuickTime\QTSystem\

 

[systemRoot] => C:\Windows

 

[COMSPEC] => C:\Windows\system32\cmd.exe

 

[PATHEXT] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

 

[WINDIR] => C:\Windows

 

[sERVER_SIGNATURE] => Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1 Server at localhost Port 80

 

 

 

[sERVER_SOFTWARE] => Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1

 

[sERVER_NAME] => localhost

 

[sERVER_ADDR] => 127.0.0.1

 

[sERVER_PORT] => 80

 

[REMOTE_ADDR] => 127.0.0.1

 

[DOCUMENT_ROOT] => C:/xampp/htdocs

 

[sERVER_ADMIN] => postmaster@localhost

 

[sCRIPT_FILENAME] => C:/xampp/htdocs/knowledge_is_power/www/index.php

 

[REMOTE_PORT] => 49840

 

[GATEWAY_INTERFACE] => CGI/1.1

 

[sERVER_PROTOCOL] => HTTP/1.1

 

[REQUEST_METHOD] => GET

 

[QUERY_STRING] =>

 

[REQUEST_URI] => /knowledge_is_power/www/index.php

 

[sCRIPT_NAME] => /knowledge_is_power/www/index.php

 

[php_SELF] => /knowledge_is_power/www/index.php

 

[REQUEST_TIME] => 1327298686

 

[_POST] => Array

 

(

 

)

 

 

 

[_GET] => Array

 

(

 

)

 

 

 

[_COOKIE] => Array

 

(

 

[__utma] => 111872281.92522752.1309196757.1309196757.1309196757.1

 

[Drupal_toolbar_collapsed] => 0

 

[Drupal_tableDrag_showWeight] => 0

 

[sESSbce8863eabfd478fab0d5a5077b9e512] => RPzBUmLIJ_x2rxRSAkhhveaBQcp8skL0l4hpKmbNbtg

 

[phpSESSID] => 2631qhcn4o4i6u50d6h4sdrtm0

 

)

 

 

 

[_SERVER] => Array

 

(

 

[MIBDIRS] => C:/xampp/php/extras/mibs

 

[MYSQL_HOME] => \xampp\mysql\bin

 

[OPENSSL_CONF] => C:/xampp/apache/bin/openssl.cnf

 

[php_PEAR_SYSCONF_DIR] => \xampp\php

 

[phpRC] => \xampp\php

 

[TMP] => \xampp\tmp

 

[HTTP_HOST] => localhost

 

[HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1

 

[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

 

[HTTP_ACCEPT_LANGUAGE] => en-us,en;q=0.5

 

[HTTP_ACCEPT_ENCODING] => gzip, deflate

 

[HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.7

 

[HTTP_CONNECTION] => keep-alive

 

[HTTP_REFERER] => http://localhost/knowledge_is_power/www/register.php

 

[HTTP_COOKIE] => __utma=111872281.92522752.1309196757.1309196757.1309196757.1; Drupal.toolbar.collapsed=0; Drupal.tableDrag.showWeight=0; SESSbce8863eabfd478fab0d5a5077b9e512=RPzBUmLIJ_x2rxRSAkhhveaBQcp8skL0l4hpKmbNbtg; PHPSESSID=2631qhcn4o4i6u50d6h4sdrtm0

 

[PATH] => C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\QuickTime\QTSystem\

 

[systemRoot] => C:\Windows

 

[COMSPEC] => C:\Windows\system32\cmd.exe

 

[PATHEXT] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

 

[WINDIR] => C:\Windows

 

[sERVER_SIGNATURE] => Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1 Server at localhost Port 80

 

 

 

[sERVER_SOFTWARE] => Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1

 

[sERVER_NAME] => localhost

 

[sERVER_ADDR] => 127.0.0.1

 

[sERVER_PORT] => 80

 

[REMOTE_ADDR] => 127.0.0.1

 

[DOCUMENT_ROOT] => C:/xampp/htdocs

 

[sERVER_ADMIN] => postmaster@localhost

 

[sCRIPT_FILENAME] => C:/xampp/htdocs/knowledge_is_power/www/index.php

 

[REMOTE_PORT] => 49840

 

[GATEWAY_INTERFACE] => CGI/1.1

 

[sERVER_PROTOCOL] => HTTP/1.1

 

[REQUEST_METHOD] => GET

 

[QUERY_STRING] =>

 

[REQUEST_URI] => /knowledge_is_power/www/index.php

 

[sCRIPT_NAME] => /knowledge_is_power/www/index.php

 

[php_SELF] => /knowledge_is_power/www/index.php

 

[REQUEST_TIME] => 1327298686

 

)

 

 

 

[_ENV] => Array

 

(

 

)

 

 

 

[_FILES] => Array

 

(

 

)

 

 

 

[_REQUEST] => Array

 

(

 

)

 

 

 

[live] =>

 

[contact_email] => you@example.com

 

[_SESSION] => Array

 

(

 

)

 

 

 

[http_response_header] => Array

 

(

 

[0] => HTTP/1.1 200 OK

 

[1] => Date: Mon, 23 Jan 2012 06:04:47 GMT

 

[2] => Server: Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1

 

[3] => X-Powered-By: PHP/5.3.5

 

[4] => Content-Length: 0

 

[5] => Connection: close

 

[6] => Content-Type: text/html

 

)

 

 

 

[pages] => Array

 

(

 

[Home] => index.php

 

[About] => #

 

[Contact] => #

 

[Register] => register.php

 

)

 

 

 

[this_page] => index.php

 

[v] => register.php

 

[k] => Register

 

[q] => SELECT COUNT(history.id) AS num, pages.id, pages.title FROM pages, history WHERE pages.id=history.page_id AND history.type='page' GROUP BY (history.page_id) ORDER BY num DESC LIMIT 10

 

)

 

 

 

)

 

 

 

)

 

 

 

)

Link to comment
Share on other sites

$dbc is your database connection variable. That means you don't have access to it, perhaps because you haven't included the MySQL connection script.

 

Also, just to be clear, the book does assume complete comfort with PHP and MySQL.

Link to comment
Share on other sites

I included the MySql connection script, here is my index.php page up till the part when $dbc is required,

 

<?php

 

// This file is the home page.

// This script is begun in Chapter 3.

 

// Require the configuration before any PHP code as the configuration controls error reporting:

require ('./includes/config.inc.php');

// The config file also starts the session.

 

// To test the sidebars:

// $_SESSION['user_id'] = 1;

// $_SESSION['user_admin'] = true;

 

// Require the database connection:

require (MYSQL);

 

// Next block added in Chapter 4:

// If it's a POST request, handle the login attempt:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

include ('./includes/login.inc.php');

}

 

// Include the header file:

include ('./includes/header.html');

 

/* PAGE CONTENT STARTS HERE! */

?><h3>Welcome</h3>

<p>Welcome to Knowledge is Power, a site dedicated to keeping you up to date on the Web security and programming information you need to know. Blah, blah, blah. Yadda, yadda, yadda.</p>

 

<?php // Bonus material! Referenced in Chapter 5.

// Display the most popular pages:

echo '<h3>Most Popular Pages</h3><p>';

 

$q = "SELECT COUNT(history.id) AS num, pages.id, pages.title FROM pages, history WHERE pages.id=history.page_id AND history.type='page' GROUP BY (history.page_id) ORDER BY num DESC LIMIT 10";

$r = mysqli_query($dbc, $q);

 

I even put an echo statement in the mysql.inc.php file to make sure that the file was running, and it showed up. I am guessing i have to check if the database is connection is actually connecting or not which may be leading to it being undefined.

Link to comment
Share on other sites

Ive checked this again i can't see what the problem is? I have used mysql connections before when connecting to the database, this was the first time for me using mysql extension with objects. What test could i do for checking the mysqli connection. I have the code copied in my apache server exactly as from your book, i have just been making important notes as i read through.

Link to comment
Share on other sites

I don't know what you mean about "using the mysql extension with objects", because that's not what's happening here. Also, even if no connection were to be made, that wouldn't cause an undefined variable error.

 

Edward, I do worry you're going to have difficult following the rest of the code in this book if you're having trouble with this.

Link to comment
Share on other sites

I don't want to give up that easy, with your explaining i am sure i will make it through.

 

I tried to add in the

echo mysql_error ();

But no reading comes up, i also have this error showing on the same page:

 

An error occurred in script 'C:\xampp\htdocs\knowledge_is_power\www\index.php' on line 35:

mysqli_query() expects parameter 1 to be mysqli, null given

Link to comment
Share on other sites

Update, i tried running this code in my mysql.inc.php file

 

// Make the connection:

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

 

// Set the character set:

mysqli_set_charset($dbc, 'utf8');

 

$q = "INSERT INTO users (username, email, pass, first_name, last_name, date_expires) VALUES ('Admin', 'admin', 'password', 'Admin', 'Admin', SUBDATE(NOW(), INTERVAL 1 DAY) )";

 

$r = mysqli_query ($dbc, $q);

 

Now when i run the index.php file it run the require(MYSQL); and this part of the code would then run. Well the record was successfully written to the users table. Does this give anyone an idea of what the issue could be?

Link to comment
Share on other sites

I tried this also i added to mysql.inc.php

 

<?php

 

// This file contains the database access information.

// This file establishes a connection to MySQL and selects the database.

// This file defines a function for making data safe to use in queries.

// This file defines a function for hashing passwords.

// This script is begun in Chapter 3.

 

// Set the database access information as constants:

DEFINE ('DB_USER', 'root');

DEFINE ('DB_PASSWORD', 'edward');

DEFINE ('DB_HOST', 'localhost');

DEFINE ('DB_NAME', 'knowledge_is_power');

 

// Make the connection:

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

 

// Set the character set:

mysqli_set_charset($dbc, 'utf8');

 

$q = "INSERT INTO users (username, email, pass, first_name, last_name, date_expires) VALUES ('1', '1', '1', '1', '1', SUBDATE(NOW(), INTERVAL 1 DAY) )";

 

$r = mysqli_query ($dbc, $q);

 

etc.....

 

and added to index.php

 

<?php

 

// This file is the home page.

// This script is begun in Chapter 3.

 

// Require the configuration before any PHP code as the configuration controls error reporting:

require ('./includes/config.inc.php');

// The config file also starts the session.

 

// To test the sidebars:

// $_SESSION['user_id'] = 1;

// $_SESSION['user_admin'] = true;

 

// Require the database connection:

require (MYSQL);

$q = "INSERT INTO users (username, email, pass, first_name, last_name, date_expires) VALUES ('2', '2', '2', '2', '2', SUBDATE(NOW(), INTERVAL 1 DAY) )";

 

$r = mysqli_query ($dbc, $q);

 

// Next block added in Chapter 4:

// If it's a POST request, handle the login attempt:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

include ('./includes/login.inc.php');

}

 

etc....

 

Then i run the index.php script well the values from mysql.inc.php went into the database, but again on index.php i got the error below and the values didn't make it into the database:

 

An error occurred in script 'C:\xampp\htdocs\knowledge_is_power\www\index.php' on line 18:

Undefined variable: dbc

 

Why is dbc undefined when we already required the mysql.inc.php script in index.php i don't understand?

Link to comment
Share on other sites

Are you sure that the new record is being inserted (i.e., are you always using new values)? As for why $dbc would be undefined, perhaps PHP isn't including the MySQL connect file, or it's not including the one you think it is.

 

Also, per the forum guidelines, it'd be good to know the versions involved here.

Link to comment
Share on other sites

Finally i figured it out. It was my mistake i had misunderstood what you had written Larry, I put the mysql.inc.php file below the root directory and not in the /includes/ directory above the root folder. The index.php file was running the mysql.inc.php script but was not saving the variable $dbc for global use. That is something now i don't even understand why it doesn't work. But now its all fine while in the includes.

 

Haha i have a new error now, more detective work for me today, eh!

 

An error occurred in script 'C:\xampp\htdocs\knowledge_is_power\www\index.php' on line 38:

mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given

Link to comment
Share on other sites

An error occurred in script 'C:\xampp\htdocs\knowledge_is_power\www\index.php' on line 38:

mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given

 

"Well this is not working because i have no data in my pages table yet, okay all done proceeding on..."

 

Extract from index.php to show line 38 in red:

 

<?php // Bonus material! Referenced in Chapter 5.

// Display the most popular pages:

echo '<h3>Most Popular Pages</h3><p>';

 

$q = "SELECT COUNT(history.id) AS num, pages.id, pages.title FROM pages, history WHERE pages.id=history.page_id AND history.type='page' GROUP BY (history.page_id) ORDER BY num DESC LIMIT 10";

$r = mysqli_query($dbc, $q);

 

$n = 1; // Counter

while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {

 

// Display each record:

echo "<h4>$n. <a href=\"page.php?id=$row[1]\">$row[2]</a></h4>\n";

 

// Increment the counter:

$n++;

 

} // End of WHILE loop.

echo '</p>';

?>

Link to comment
Share on other sites

I am not struggling with this Jonathon, i simply placed the mysql.inc.php below the root directory. When i run the index.php, it was running the mysql.inc.php code but was not storing the variable $dbc in memory so that it was unavialable globally. I am using Windows 7, Larry is using a Mac, well i got confused with Larry's book Page.54 Figure 3.2, it shows the apple operating system there. It seems to show in the book the mysql.inc.php below the root directory there??

 

Everything with regards to the PHP server scripting language i am happy with.

 

I have two other books here on E-commerce development both are in OOP, but the first one uses the three tier architecture which is not the same as MVC and the second is a OOP MVC framework. I have to admit the second book was too much for me, as at the moment i am not completely familar with what MVC is, so thats why Larry's book is perfect for me, it is helping me to see the structure while we are coding basically with procedural. The reason for me doing these books is because i don't want to be writing how i feel spaghetti type code which will be impossible to fix later on, i want to fully understand the MVC approach and use this method.

Link to comment
Share on other sites

 Share

×
×
  • Create New...