Re: [lug-nuts] PHP whoohs

From: Scott Tyson (tysons@deepwell.com)
Date: Thu Nov 18 1999 - 13:30:59 PST


What part of the script is bombing? I have some php3 running against a
mysql db over at http://rand.deepwell.com/news.php3 and my mysql
statements are arranged a bit different but it doesn't mean these won't
work.

Here is a snippet that grabs all of my news entries and spews them onto
the page: (http://rand.deepwell.com/archive.php3
has more entries so you can see how they repeat. I hope the formatting
is OK.

<? mysql_connect("localhost", "SOMEUSERGOESHERE", "");
    $query = "SELECT * FROM entry where timedt > '1999-10-31 00:00:00'
ORDER BY timedt DESC";
    $result = mysql_db_query("news", $query);
    if ($result)
        {
        while ($r = mysql_fetch_array($result))
            {
            $timestamp = $r["timedt"];
            $title = $r["title"];
            $item = $r["item"];
            echo "<TABLE BORDER='0' CELLSPACING='0' CELLPADDING='0'>";
            echo "<TR VALIGN='top'>";
            echo "<TD WIDTH='548' VALIGN='top'>";
            echo "<FONT FACE='Tahoma,Arial' COLOR='#E9B74B' SIZE='3'><B>
            $title -- </B></FONT><FONT FACE='Tahoma,Arial' SIZE='2'
COLOR='#C7C7C7'><B>$timestamp</B></FONT>";
            echo "<BR><FONT FACE='Tahoma,Arial' SIZE='2'>$item";
            echo "</TD>";
            echo "</TR>";
            echo "</TABLE>";
            echo "<BR>";
            echo "<!-- end news acticle -->";
            }
          echo "<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR></TD>";
    } else {
      echo "No data.";
    }
 mysql_free_result($result);
?>

BTW. http://www.devshed.com is a good place for php/mysql stuff.

----- Original Message -----
From: Brian Lavender <brian@brie.com>
To: Lug Nuts <lug-nuts@saclug.org>
Sent: Thursday, November 18, 1999 12:57 PM
Subject: [lug-nuts] PHP whoohs

> I just started playing with PHP and I found an example from web
techniques magazine
>
> http://www.webtechniques.com/archives/1998/01/note/
>
> Thing is, it seems the script was written for PHP2 and I have
installed
> PHP3. So, I am wondering if anyone can recognize the changes I need to
> make, so I can get a working prototype and get started with testing
php.
> Enough rambling, here is the sample code. What changes do I need to
make?
>
> Here is the url (error message of course)
>
> http://www.brie.com:8080/contact.php3
>
> brian
>
> <HTML>
> <?
> /* Script Name: contact.php3
> Script Purpose: Simple Contact Database Script Version: 1.1
> Script Author: Michael J. Miller Jr. (mke@netcom.com)
> */
> /* First let's set some variables. */
> $hostname = "localhost";
> $password = "grok";
> $user = "contact";
> /* Now to define functions. */
> /* The main screen. Printed when $state is empty. */
> Function Main_Menu (
> echo "<H1><CENTER> Welcome To The Contact Database </CENTER></H1>";
> echo "You have the following choices:";
> echo "<FORM METHOD = \"POST\" ACTION=\"contact.php3\">";
> echo "<INPUT CHECKED TYPE=\"radio\" NAME=\"state\" VALUE=\"List\"><B>
List
> Contacts.</B><BR>";
> echo "<INPUT TYPE=\"radio\" NAME=\"state\" VALUE=\"Create\"><B>Create
a new
> Contact.</B><P>";
> echo "<INPUT TYPE=\"submit\" VALUE=\"Make Contact\">";
> echo "</FORM>";
> );
> /* List the first and last name of people in the contact database plus
their
> phone number. Make the last name clickable for the full record. */
> Function List (
> echo "<CENTER><H2>Contact List</CENTER></H2><P>";
> mysql_connect($hosthame,$user,$password);
> $result = mysql("ContactDB","SELECT uid, last_name, first_name, phone
FROM
> contact ORDER BY last_name");
> echo "<TABLE BORDER = 10 CELLPADDING = 2>";
> $total_rows = mysql_numrows($result); $counter = 0;
> echo "<TR><TD><B>Last Name</B></TD><TD><B>First
Name</B></TD><TD><B>Work
> Phone</B></TD></TR>";
> while($counter < $total_rows);
> $uid = mysql_result($result,$counter,"uid"); echo "<TR><TD>\n";
> echo "<A HREF=contact.php3?uid=$uid&state=Print_Contact>";
> echo mysql_result($result,$counter,"last_name");
> echo "</A>\n";
> echo "</TD><TD>\n";
> echo mysql_result($result,$counter,"first_name"); echo "</TD><TD>\n";
> echo mysql_result($result,$counter,"phone"); echo "</TD></TR>\n";
> $counter = $counter + 1;
> endwhile;
> echo "</TABLE>";
> );
> /* Save updates to database. Call Print_Contact to list new
information
> for updated contact entry. */
> Function Commit_Update $uid $fn $mi $ln $ph $fx $em (
> mysql_connect($hosthame,$user,$password);
> $result = mysql("ContactDB","UPDATE contact SET first_name = '$fn',
> middle_initial = '$mi', last_name = '$ln', phone = '$ph', fax = '$fx',
> email = '$em' WHERE uid = $uid");
> $state = "";
> Print_Contact($uid);
> );
> /* Save new contact information to database. Call Print_Contact to
display
> new contact entry. */
> Function Commit_Contact $fn $mi $ln $ph $fx $em (
> mysql_connect($hostname,$user,$password);
> $result = mysql("ContactDB","SELECT now()"); /* Get the Current
Datetime */
> /* Note that it would be
> better to use a field
> of type TIMESTAMP here,
> since it would automatically
> assign the current date and
> time. I haven't done that
> here since I wanted to
> demonstrate using functions
> in MySQL. */
> $now = mysql_result($result,0,"now()");
> $result = mysql("ContactDB","INSERT INTO contact
> (first_name, middle_initial,last_name, phone, fax, email)
> VALUES ('$fn', '$mi','$ln', '$ph', '$fx', '$em')");
> $new_uid = mysql_insert_id();
> $result = mysql("ContactDB","INSERT INTO comment (uid, contact_date,
> contact_comment) VALUES ($new_uid, '$now' , 'Contact Record
Created')");
> $state = "";
> Print_Contact($new_uid);
> );
> /* This function displays the contact nformation in a form. It is
used
> for both the create and update contact options. */
> Function Contact_Form $state,$uid (
> if($state == "Update");
> $state = "Commit_Update";
> $result = mysql("ContactDB","SELECT * FROM contact WHERE uid = $uid");
> $last_name = mysql_result($result,0,"last_name");
> $first_name = mysql_result($result,0,"first_name");
> $middle_initial = mysql_result($result,0,"middle_initial");
> $phone = mysql_result($result,0,"phone");
> $fax = mysql_result($result,0,"fax"); $email =
mysql_result($result,0,"email");
> else;
> $state = "Commit_Contact";
> endif;
> echo "<CENTER><H2>Contact Form</H2></CENTER><P>\n";
> echo "<FORM METHOD = \"POST\" ACTION=\"contact.php3\">\n";
> echo " <INPUT TYPE = \"HIDDEN\" NAME = \"uid\" VALUE = \"$uid\">\n";
echo "
> <INPUT TYPE = \"HIDDEN\" NAME = \"state\" VALUE = \"$state\">\n";
> echo " First Name: <BR><INPUT TYPE = \"TEXT\" NAME=\"first_name\"
\n"; echo "
> VALUE = \"$first_name\" MAXLENGTH = 20><BR>\n";
> echo " Middle Initial: <BR><INPUT TYPE = \"TEXT\"
NAME=\"middle_initial\"
> \n";
> echo " VALUE = \"$middle_initial\" MAXLENGTH = 1 SIZE = 2><BR>\n";
> echo " Last Name: <BR><INPUT TYPE = \"TEXT\" NAME=\"last_name\" \n";
echo "
> VALUE = \"$last_name\" MAXLENGTH = 30><BR><P>\n";
> echo " Phone: <BR><INPUT TYPE = \"TEXT\" NAME=\"phone\" \n"; echo "
> VALUE = \"$phone\" MAXLENGTH = 30><BR>\n";
> echo " Fax: <BR><INPUT TYPE = \"TEXT\" NAME=\"fax\" \n"; echo "
> VALUE = \"$fax\" MAXLENGTH = 30><BR>\n";
> echo " Email: <BR><INPUT TYPE = \"TEXT\" NAME=\"email\" \n"; echo "
> VALUE = \"$email\" MAXLENGTH = 70 SIZE = 40><P>\n";
> echo " <CENTER><INPUT TYPE=\"submit\" VALUE=\"Submit
> Contact\"></CENTER>\n";
> echo "</FORM>\n";
> );
> /* Add a comment to the database. Call Print_Contact to display
changes. */
> Function Add_Comment $uid $contact_comment (
> mysql_connect($hosthame,$user,$password);
> $result = mysql("ContactDB","SELECT now()"); /* Get the Current
Datetime */
> $now = mysql_result($result,0,"now()");
> $result = mysql("ContactDB","INSERT INTO comment (uid, contact_date,
> contact_comment) VALUES ($uid, '$now', '$contact_comment')");
> Print_Contact($uid);
> );
> /* Print address and comments for a contact. Also give option to
update
> or add comments. */
> Function Print_Contact $uid (
> mysql_connect($hosthame,$user,$password);
> $result = mysql("ContactDB","SELECT * FROM contact WHERE uid = $uid");
> $name = mysql_result($result,0,"first_name");
> $name = $name + " " + mysql_result($result,0,"middle_initial");
> $name = $name + " " + mysql_result($result,0,"last_name");
> echo "<TITLE>Contact Information For $name</TITLE>"; echo "</BODY>";
> echo "<HTML>";
> echo "<CENTER><H2>Contact Information For $name</H1></CENTER>";
> echo "<TABLE BORDER = 10 CELLPADDING = 2>";
> echo "<TR><TD>Name</TD>";
> echo "<TD>$name</TD></TR>";
> $result = mysql("ContactDB","SELECT * FROM contact WHERE uid = $uid");
> echo "<TR><TD>Phone</TD>";
> echo "<TD>";
> echo mysql_result($result,0,"phone"); echo "</TD></TR>\n";
> echo "<TR><TD>Fax</TD>";
> echo "<TD>";
> echo mysql_result($result,0,"fax");
> echo "</TD></TR>\n";
> $email = mysql_result($result,0,"email");
> echo "<TR><TD>Email Address</TD>";
> echo "<TD><A HREF=\"MAILTO:";
> echo "$email \"> $email";
> echo "</TD></TR>\n";
> echo "</TABLE>";
> echo "<FORM METHOD = \"POST\" ACTION=\"contact.php3\">";
> echo " <INPUT TYPE = \"HIDDEN\" NAME = \"uid\" VALUE = \"$uid\">\n";
echo "
> <INPUT TYPE = \"HIDDEN\" NAME = \"state\" VALUE = \"Update\">\n";
> echo "<CENTER><INPUT TYPE=\"submit\" VALUE=\"Update Contact
> Information\"></CENTER>";
> echo "<P><HR WIDTH=\"100%\">";
> echo "<CENTER><H2>Comments For This Contact</H2></CENTER>";
> echo "</FORM>";
> $result = mysql("ContactDB","SELECT * FROM comment WHERE uid = $uid
ORDER BY
> contact_date");
> $total_rows = mysql_numrows($result); $counter = 0;
> echo "<TABLE BORDER = 10 CELLPADDING = 2>"; echo
"<TR><TD>Date</TD><TD>Comment</TD>";
> while($counter < $total_rows);
> echo "<TR><TD>";
> echo mysql_result($result,$counter,"contact_date"); echo "</TD><TD>";
> echo mysql_result($result,$counter,"contact_comment"); echo
"</TD></TR>";
> $counter = $counter + 1;
> endwhile;
> echo "</TABLE>";
> echo "<P>Add new comment below.";
> echo "<FORM METHOD = \"POST\" ACTION=\"contact.php3\">";
> echo "<P><INPUT TYPE = \"TEXT\" NAME = \"contact_comment\" ";
> echo "MAXLENGTH = 60 SIZE = 60><BR>";
> echo "<INPUT TYPE = \"HIDDEN\" NAME = \"uid\" VALUE = \"$uid\"><P>";
> echo "<INPUT TYPE = \"HIDDEN\" NAME = \"state\" VALUE =
\"Add_Comment\"><P>";
> echo "<CENTER><INPUT TYPE=\"submit\" VALUE = \"Submit
Comment\"></CENTER>";
> echo "</FORM><P>";
> echo "<FORM METHOD = \"POST\" ACTION=\"contact.php3\">";
> echo "<CENTER><INPUT TYPE=\"submit\" VALUE=\"Return To Main
> Menu\"></CENTER>";
> echo "</FORM>\n";
> );
> /* The main loop. Call functions based on the value of $state, which
> gets set via a hidden INPUT TYPE. */
> switch($state) {
> case "";
> Main_Menu();
> break;
> case "List";
> List();
> break;
> case "Create";
> Contact_Form($state, $uid);
> break;
> case "Update";
> Contact_Form($state, $uid);
> break;
> case "Commit_Update";
>
Commit_Update($uid,$first_name,$middle_initial,$last_name,$phone,$fax,$e
ma
> il);
> break;
> case "Commit_Contact";
>
Commit_Contact($first_name,$middle_initial,$last_name,$phone,$fax,$email
); break;
> case "Add_Comment";
> Add_Comment($uid,$contact_comment);
> break;
> case "Print_Contact";
> Print_Contact($uid);
> endswitch;
> >
> </BODY>
> </HTML>
>
>
> --
> Brian Lavender
> http://www.brie.com/brian/
>
************************************************************************
****
> * To UNSUBSCRIBE from the list, send a message with "unsubscribe
lug-nuts"
> * in the message body to majordomo@saclug.org. Please direct other
> * questions, comments, or problems to lug-nuts-owner@saclug.org.
>

****************************************************************************
* To UNSUBSCRIBE from the list, send a message with "unsubscribe lug-nuts"
* in the message body to majordomo@saclug.org. Please direct other
* questions, comments, or problems to lug-nuts-owner@saclug.org.



This archive was generated by hypermail 2b29 : Fri Feb 25 2000 - 14:29:08 PST