Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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']; ?> — <?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);
?>
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
yea, this actually has nothing to do with my webhosting provider. It is all on a local host test server that I set up using the XAMPP software that David suggested in his tutorial. The tutorial was actually teaching how to install the software and set up a local test sever. And as I said it all worked fine until the end. At one point he had us make a php file that gave the current time, that worked fine. Then he had us download some example files, insert them into our test site root. Then set up a database using phpadmin in MySQL and import a sql file in that database. This is where I ran into trouble. The page in DW shows up fine 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. So I am at a loss, as I said, I'm new to php & mysql and just trying to learn, I thought David's tutorial would be a good place to start.
Thanks for your help
Copy link to clipboard
Copied
I no longer remember the actual error message(s) I received when first trying to connect to my live site after following the same tutorial but after starting from scratch (new web site in DW5.5) several times I narrowed it down to something in the connections file. Low and behold, my live site host finally disclosed that I had to prefix the database name and database user name with by cPanel user ID and an underscore. I also was advised by the host that (for some reason) the live database username(s) was limited to 6 (not 7 as shown on the live phpMyAdmin database manage form). After going back and re-creating the connections file, all worked perfectly.
The connections file ultimately looked as follows:
<?php
# FileName="Connection-php-mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_check_mag = 'localhost";
$database_check_mag = "abcde_phptest";
$username_check_mag = "abcde_tester";
$password_check_mag = "XyZ321";
$check_mag = mysql_pconnect($hostname_check_mag, $username_check_mag, $password_check_mag) or trigger_error(mysql_error(), E_USER_ERROR);
?>
(Note: the 'abcde' is my cPanel (Host) login ID and the underscore ("_") is required.)
Since I've been using only one host for quite a while, I don't know if this applies other site hosts; but, it's worth a call/message to yours.
Copy link to clipboard
Copied
yea, this actually has nothing to do with my webhosting provider.
I said what I said because it has to do with your setup regardless of whether it is hosted somewhere else or localhost. For security reasons we will never recommend posting login details to a mySQL server but that is where the issue lies for you. The details you have entered are not correct or are not going into the variable $connTest. The first error you are receiving on line 34 is the root cause. The other errors you are receiving after that are subsequently caused by that initial error. I would recommend going back through the tutorial and verifying the connection you are making to the database in that "conntest.php" file. The SQL query and the PHP files you posted have no errors in them at all and they appear to be ok.
If you want to PM me the details you have in the conntest.php file I would be more than willing to see if there's anything I can do to verify the file is correct. Short of going through the tutorial myself that's about the most I can do. At that point I might recommend that you consider reaching out to David via the forums.
Copy link to clipboard
Copied
thank you SnakEyez02 - though your answer wasn't exactly it was close enough to make me find the answer. What was wrong was that I had set up the user account in the wrong section of phpMyAdmin. I think I set it up at the sql file level instead of the home/welcome level. For some reason when I set up my connection and hit the "test" button, DW was telling me that the connection was made just fine. But I'm guessing that since the user information I had typed into the connection setup was entered at the wrong level the though I was connected I wasn't given permission to retreave the data. After going into phpMyAdmin and deleting the user ID and resubmitting it at the correct level everything started working.
Thanks again!
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more