Wrox Programmer Forums
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 31st, 2004, 06:50 AM
Registered User
 
Join Date: Jul 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default mySQL problem

Hi,
I have Apache, PHP and mySQL successfully installed on my computer ('if this works we really did it') but can't seem to connect mySQL with a program.

I have a guest book running on my remote server (my web site) using PHP and mySQL and there are no problems with it, it works as it should, but I can't make the same program work locally. On launching, instead of it loading the previous guest book entries into the 'write' window, or the text ("there are no entries as yet...") all it says is 'undefined'

There would appear to be some communication between the PHP script and mySQL though because if I have the script in an editor and change a connection variable deliberately such as the name of the table to a phony one, when I launch it in a browser I get an error, but if I use the correct table name I just get a blank screen which I think is as it should be.

Can anyone help please.

Thank-you in advance, Cheers, Henry



 
Old July 31st, 2004, 08:34 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you've copied everything into the database, and set up the user account that the script uses to log into MySQL, (in effect, if you're duplicated everything from your live box onto your local one) then it must be a local settings problem. It may well be that your local machine defaults to being much less tolerant of things like unquoted array indeces, or implicit calls to members of PHP's global arrays, the live server is very likely set up to recover from things like this, which are strictly speaking coding errors, but ones which can be catered for within PHP's setup, at the expense of a small amount of performance.

What does the actual code look like?
 
Old July 31st, 2004, 11:07 AM
Registered User
 
Join Date: Jul 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Daniel,
You will note that the code is part of a tutorial from Flash-db.
I just have a feeling that the problem may be in the settings of the mySQL or PHP ini files or permissions. But then I'm a complete novice. Thanks, Henry

<?php
/*
-----
Application: Flash-dB GuestBook Version 2.0
Details: mySQL and PHP powered GuestBook
Author: Mohsin Sumar
Website: http://www.flash-db.com
Support: http://www.flash-db.com/Board
Notes: Coments are marked by using comment entries symbols. Eg: // Comment
-----
*/

// Part One - Initiate a mySQL Database Connection
// Database Connectivity Variables and other Variables
   $DBhost = "localhost"; // Database Server
   $DBuser = "root"; // Database User
   $DBpass = "8526802"; // Database Pass
   $DBName = "guestbooktwo"; // Database Name
   $table = "guestbook"; // Database Table
   $numComments = 10; // Number of Comments per page

   // Connect to mySQL Server
   $DBConn = mysql_connect($DBhost,$DBuser,$DBpass) or die("Error in GuestBook Application: " . mysql_error());
   // Select mySQL Database
   mysql_select_db($DBName, $DBConn) or die("Error in GuestBook Application: " . mysql_error());

// Part Two - Choose what action to perform
   $action = $_GET['action'];

   switch($action) {
      case 'read' :
         // Fetch all comments from database table
         $sql = 'SELECT * FROM `' . $table . '`';
         $allComments = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());
         $numallComments = mysql_num_rows($allComments);
         // Fetch page-wise comments from database table
         $sql .= ' ORDER BY `time` DESC LIMIT ' . $_GET['NumLow'] . ', ' . $numComments;
         $fewComments = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());
         $numfewComments = mysql_num_rows($fewComments);
         // Generate Output for Flash to Read
         print '&totalEntries=' . $numallComments . '&';
         print "<br>&entries=";

         if($numallComments == 0) {
            print "No entries in the guestbook, as yet..";
         } else {
            while ($array = mysql_fetch_array($fewComments)) {
               $name = mysql_result($fewComments, $i, 'name');
               $email = mysql_result($fewComments, $i, 'email');
               $comments = mysql_result($fewComments, $i, 'comments');
               $time = mysql_result($fewComments, $i, 'time');

               print '<b>Name: </b>' . $name . '<br><b>Email: </b>' . $email . '<br><b>Comments: </b>' . $comments . '<br><i>Date: ' . $time . '</i><br><br>';
               $i++;
            }
        }
        // Print this only when there aren't any more entries..
        if($_GET['NumLow'] > $numallComments) {
           print 'No More Entries!&';
        }
        break;

      case 'write' :
         // Recieve Variables From Flash
         $name = ereg_replace("&", "%26", $_POST['yourname']);
         $email = ereg_replace("&", "%26", $_POST['youremail']);
         $comments = ereg_replace("&", "%26", $_POST['yourcomments']);
         $submit = $_POST['submit'];

         // Current system date in yyyy-mm-dd format
         $submitted_on = date ("Y-m-d H:i:s",time());

         // Check if its submitted from Flash
         if($submit == 'Yes'){
         // Insert the data into the mysql table
         $sql = 'INSERT INTO ' . $table .
                ' (`ID`,
                   `name`,
                   `email`,
                   `comments`,
                   `time`
                  )
                  VALUES
                  (\'\','
                   . '\'' . $name . '\','
                   . '\'' . $email . '\','
                   . '\'' . $comments . '\','
                   . '\'' . $submitted_on . '\'
                   )';
         $insert = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());

         // If you want your script to send email to both you and the guest, uncomment the following lines of code
         // Email Script Begin

         /* <-- Remove this line
         $MyName = "Mohsin Sumar";
         $MyEmail = "[email protected]";
         $Subject = "$name has just signed your guestbook.";
         $EmailBody = "Hello Mohsin,\n$name has just signed your guestbook available at http://www.mohsinsumar.com. THe following were the details submitted into your guestbook:\n\nName: $name\nEmail: $email\nComment:\n$comments\n";

         $EmailFooter = "~~~~~~~~~~~~~~~\nThe guestbook was signed by $name and thus this email got activated by $name from $REMOTE_ADDR from http://www.mohsinsumar.com\n~~~~~~~~~~~~~~~\nThanking you,\nMohsin Sumar";

         $Message = $EmailBody.$EmailFooter;

         mail($MyName." <".$MyEmail.">",$Subject, $Message, "From: ".$name." <".$email.">");
         --> Remove this line */

         // Email Script End

         print "&gb_status=Thank you for signing my guestbook.&done=yes&";
         return;
         }
         print "&_root.write.gb_status=Error!&";
         break;
   }
?>

 
Old August 2nd, 2004, 09:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Clearly you need to pass somethig as "action" in the querystring before it'll do anything at al (namely action=write or action=read). I'm not sure what that "undefined" business is about, though. Have you checked in your HTML source, to see if there's actually a more explanatory error message being sent, of which the word "undefined" is the only part visible in the browser? Long shot, but its always worth a try ;).
Dan
 
Old August 2nd, 2004, 12:32 PM
Registered User
 
Join Date: Jul 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
Well as I say the script works perfectly at the remote site. It reads first and presents the entries as you load the page then when you add to it, it displayes that too,

Can you think of any settings that I may have missed in both PHP and mySQL. It seems to attempt to work then faulter as though it had started the script then found that the password was wrong say and stopped. (I checked the password)






Similar Threads
Thread Thread Starter Forum Replies Last Post
problem conecting to MySql - please help daemyenn MySQL 2 October 29th, 2006 09:26 PM
Test MYSQL PROBLEM jhan316 MySQL 2 June 26th, 2006 03:46 AM
mysql connect problem protozoa JSP Basics 3 August 17th, 2004 12:21 AM
MySQL queries problem hosefo81 PHP Databases 1 January 22nd, 2004 07:15 AM
MySQL Query Problem cmiller Beginning PHP 4 August 26th, 2003 12:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.