Skip to main content
Participating Frequently
March 7, 2012
Question

undefined variables

  • March 7, 2012
  • 1 reply
  • 6336 views

I am new to PHP & MySQL and am having a little difficulty. I am using Dreamweaver CS5.5 for website building. I just recently completed a tutorial by David Powers titled "Setting up a PHP developement environment for Dreamweaver. I downloaded and installed the latest version of XAMPP and found that things have changed a little since he made the tutorial making some of the screen shots and directions obsolete, but I managed to make my way through it with success until I got to the end. Everything was working and I successfully made a connection to my database, but when I clicked on "Live View" in dreamweaver I got error messages instead of the expected list of information stored in the database. Those error messages:


Notice: Undefined variable: database_connTest in C:\xampp\htdocs\php_test\comments.php on line 34Notice: Undefined variable: connTest in C:\xampp\htdocs\php_test\comments.php on line 34

Warning: mysql_select_db() expects parameter 2 to be resource, null given in C:\xampp\htdocs\php_test\comments.php on line 34

Notice: Undefined variable: connTest in C:\xampp\htdocs\php_test\comments.php on line 36

Warning: mysql_query() expects parameter 2 to be resource, null given in C:\xampp\htdocs\php_test\comments.php on line 36


Any Idea what is causing this and how to repair it. I desire to continue learning more about building a dinamic website but feel I need to discover and correct my mistakes before I go on. Is it possible that the sample file was created in a PHP or MySQL language that isn't recognized by the newest version of the XAMPP software package?


Thanks so much for your help.

This topic has been closed for replies.

1 reply

Community Expert
March 8, 2012

Basically an undefined variable means you have code where a variable is referenced but at that point the variable doesn't exist.  This is typically resolved by using an "if" statement prior to calling the variable along the lines of:

if ( isset($var)) {  }

But knowing David (he does browse these forums), he would normally take precautions to make sure that did not exist in his code.  Can you post some of the code from that page?And just remember to "x" out any connection or other personal details for security reasons.

Participating Frequently
March 9, 2012

Yea I'm just really confused because I tested the server by making the "Time" php page that David had in his tutorial and it worked great. But when I installed the sample files as he instructed I was able to open the comments PHP file in dreamweaver and view it in design view or code view, but when I try viewing it in Live view, or on a browser all I get is the error message I pasted to my first post. Here is the code for the SQL file (I inserted the line numbers in hopes they might help):

1  -- phpMyAdmin SQL Dump
2  -- version 3.2.4
3  -- http://www.phpmyadmin.net
4  --
5  -- Host: localhost
6  -- Generation Time: Feb 05, 2010 at 02:41 PM
7  -- Server version: 5.1.41
8  -- PHP Version: 5.3.1
9
10 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
11
12
13 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
14 /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
15 /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
16 /*!40101 SET NAMES utf8 */;
17
18 --
19 -- Database: `php_test`
20 --
21
22 -- --------------------------------------------------------
23
24 --
25 -- Table structure for table `comments`
26 --
27
28 DROP TABLE IF EXISTS `comments`;
29 CREATE TABLE IF NOT EXISTS `comments` (
30   `comment_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
31   `first_name` varchar(20) NOT NULL,
32   `last_name` varchar(30) NOT NULL,
33   `comment` text NOT NULL,
34   PRIMARY KEY (`comment_id`)
35 ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
36
37 --
38 -- Dumping data for table `comments`
39 --
40
41 INSERT INTO `comments` (`comment_id`, `first_name`, `last_name`, `comment`) VALUES
42 (1, 'Ben', 'Morin', 'I would like more seafood choices on the menu. Thanks.'),
43 (2, 'Dieter', 'Dietrich', 'Is your London restaurant open on Saturdays?'),
44 (3, 'Sachiko', 'Matsuda', 'Unbelievably excellent service received at your New Tokyo

restaurant.'),
45 (4, 'Letitia', 'Riley', 'Are there any plans to open a restaurant in Anchorage, Alaska?');
46
47 /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
48 /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
49 /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
50

And this is the code for the Comments.php file (I added the Line #s for lines 34 - 39 in case the error message was refering to this code):

<?php require_once('Connections/connTest.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

Line 34- mysql_select_db($database_connTest, $connTest);
Line 35- $query_getComments = "SELECT first_name, last_name, `comment` FROM comments ORDER BY comment_id DESC";
line 36- $getComments = mysql_query($query_getComments, $connTest) or die(mysql_error());
Line 37- $row_getComments = mysql_fetch_assoc($getComments);
Line 38- $totalRows_getComments = mysql_num_rows($getComments);
Line 39- ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cafe Townsend - Customer Comments</title>
<link href="assets/styles.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="wrapper">
  <div id="header"><img src="assets/header.jpg" width="700" height="92" alt="Cafe Townsend" /></div>
  <h1 id="main_header">Cafe Townsend Intranet</h1>
  <div id="main">
    <h2>Customer comments</h2>
    <div id="comments">
      <?php do { ?>
        <p><?php echo $row_getComments['comment']; ?> &#8212; <?php echo $row_getComments['first_name']; ?> <?php echo $row_getComments['last_name']; ?></p>
        <?php } while ($row_getComments = mysql_fetch_assoc($getComments)); ?>
    </div>
  </div>
  <div id="footer"></div>
</div>
</body>
</html>
<?php
mysql_free_result($getComments);
?>

Community Expert
March 10, 2012

Check your conntest.php file and makes ure your connection details are correct.  Unfortunately this is one aspect that we will not be able to help with because this is set up by you via your web hosting provider.  The rest of the errors appear to be a result of that information not being either filled or correct.