Php... can you test something for me?

Discussion in 'Web Design and Development' started by Steve C, Feb 19, 2009.

  1. Steve C macrumors newbie

    Joined:
    Feb 19, 2009
    #1
    I am relatively new to php and I have a problem that I can't figure out regarding some scripts I found online that I wish to use to study OOP. Here is the URL:

    http://www.hosting.vt.edu/tutorials/phpmysql/#about

    The scripts are located under the section "Putting it all together in web-based personal address book" (at least 3/4 of the way down the page).

    The scripts are entitled:

    dbconnect.php
    listcontacts.php
    addcontact.php
    editcontact.php
    deletecontact.php

    I reconfigged the dbconnect.php (I am not running PEAR, couldn't get it configged on my Leopard box) and am making the connection fine (I pulled data out of it to an altered version of the dbconnect.php script as a test)... but when I try to run listcontacts.php (or any of the others) I get the following error message from Safari:

    "Parse error: syntax error, unexpected T_STRING in /Users/steve/Sites/address book/listcontacts.php on line 5"

    Here is the code for line 5:

    if ( isset($_GET['sort']) && $_GET['sort']=='firstname' ) { $_SESSION['sort'] = 'firstname'; } else { $_SESSION['sort'] = 'lastname'; }

    What I'm asking is if someone would take the time to copy the scripts and run them to see if they can get them working.

    Thanks
    Steve C
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Can you post a few more lines from that file where the error comes up, like the first 7 lines? Sometimes the source of the error comes from another line nearby. I was having issues bringing up the link, probably an issue on my end. The code you posted looks structured correctly.
     
  3. Steve C thread starter macrumors newbie

    Joined:
    Feb 19, 2009
    #3
    Yes, and thank you very much for looking!

    I don't know how to post the code correctly so that it looks like php code on this forum but here is the entire listcontacts.php file:


    <?php

    // if the user hasn't specified any "sort" the default is "lastname"

    if ( isset($_GET['sort']) && $_GET['sort']=='firstname' ) { $_SESSION['sort'] = 'firstname'; }

    else { $_SESSION['sort'] = 'lastname'; }



    include('dbconnect.php');

    ?>

    <html>

    <head>

    <title>List Contacts</title>

    </head>

    <script language="JavaScript">

    <!--

    function confirmdelete(id)

    {

    var c = confirm("Do you really want to delete this contact?");

    if (c) {

    window.location="deletecontact.php?id="+id;

    }

    }

    //-->

    </script>

    <body>

    <a href="addcontact.php">Add a Contact</a> | My Contacts

    <h1>My Contacts</h1>

    <table border="0" cellspacing="0" cellpadding="6">

    <tr bgcolor="#CCCCCC">

    <td><strong><?php if ($_SESSION['sort']!='firstname') { echo '<a href="',$_SERVER['PHP_SELF'],'?sort=firstname">First name</a>'; } else { echo 'First name ↓'; } ?></strong></td>

    <td><strong><?php if ($_SESSION['sort']!='lastname') { echo '<a href="',$_SERVER['PHP_SELF'],'?sort=lastname">Last name</a>'; } else { echo 'Last name ↓'; } ?></strong></td>

    <td><strong>Phone</strong></td>

    <td> </td>

    </tr>

    <?php

    $query = "SELECT * FROM contacts ORDER BY ".$_SESSION['sort'];

    $result = $database->query($query);

    for ($i=0; $i<$result->numRows(); $i++) {

    $contact = $result->fetchRow(DB_FETCHMODE_ASSOC,$i);

    ?>

    <tr>

    <td><?php echo $contact['firstname']; ?></td>

    <td><?php echo $contact['lastname']; ?></td>

    <td><?php

    if ( $contact['private'] == 0 ) {

    echo $contact['phone'];

    }

    else {

    echo "not public";

    }

    ?></td>

    <td>

    <a href="editcontact.php?id=<?php echo $contact['id']; ?>">edit</a>

    <a href="deletecontact.php?id=<?php echo $contact['id']; ?>" onClick="confirmdelete('<?php echo $contact["id"]; ?>'); return false;">delete</a>

    </td>

    </tr>

    <?php

    } // end: for

    ?>

    </table>

    </body>

    </html>

    <?php

    $database->disconnect();

    ?>
     
  4. frankblundt macrumors 65816

    frankblundt

    Joined:
    Sep 19, 2005
    Location:
    South of the border
    #4
    it might sound odd, but I've had frequent issues of this sort with running code copy and pasted off sites - usually turns out to be something stupid like a line ending or a space that's not quite right - screwed up somehow in the copy process.

    If you retype the line by hand (including the line break above it and after it) does the error move to the next line down?
     
  5. Steve C thread starter macrumors newbie

    Joined:
    Feb 19, 2009
    #5
    I just tried... no luck... and I typed it in VERY carefully;)

    Thanks for reading
    Steve C
     
  6. frankblundt macrumors 65816

    frankblundt

    Joined:
    Sep 19, 2005
    Location:
    South of the border
    #6
    hmmm. ok. i'll just finish getting the dinner on and try it out in a bit
     
  7. frankblundt macrumors 65816

    frankblundt

    Joined:
    Sep 19, 2005
    Location:
    South of the border
    #7
    I just copied what you posted into a new file and it seems to run just fine:
    http://test.zero.co.nz/test.php

    maybe you should try the same ;)

    I used TextWrangler, set the line endings to LF and the enciodiong to UTF-8 (No BOM)
     
  8. Steve C thread starter macrumors newbie

    Joined:
    Feb 19, 2009
    #8
    I just did... it worked;)

    Wtf? LOL!

    But I did get this
    Fatal error: Call to a member function query() on a non-object in /Users/steve/Sites/address book/listcontacts.php on line 69
     
  9. frankblundt macrumors 65816

    frankblundt

    Joined:
    Sep 19, 2005
    Location:
    South of the border
    #9
    I presume that's somewhere else in the code - probably the dbconnect one, that sets up the $database as an object
     
  10. frankblundt macrumors 65816

    frankblundt

    Joined:
    Sep 19, 2005
    Location:
    South of the border
    #10
    you have that dbconnect.php include code running?... do a var_dump($database) after it and see what you get.
     
  11. Steve C thread starter macrumors newbie

    Joined:
    Feb 19, 2009
    #11
    In this case I'm in trouble cuz I did not use the dbconnect.php file that the author did... as I don't have PEAR running (well, it's running but not configged correctly). Apparently what you said is the case... this is from the site:

    (begin quote)
    As you can see in Figure 16, a variable called $database is initialized which later will be used to "represent the database"
    (end quote)

    I guess I have to get PEAR working (probably should anyway).


    Hey... with your help I've gotten further here in the last few minutes than I have in a week;)

    Thanks... I now feel if I can just get PEAR happenin' I can make this thing work... I WAS ready to give up completely!
     
  12. frankblundt macrumors 65816

    frankblundt

    Joined:
    Sep 19, 2005
    Location:
    South of the border
    #12
    you just need to rewrite the $database bit so that it's not treating it as an object (which it probably isn't cos you rewrote that bit )

    I'm guessing you've done something like this to get the connection:
    //connect and retrieve results
    @ $db = mysql_pconnect($db_server, $db_username, $db_password);
    mysql_select_db($db_name);
    if (!$db) {
    echo "There was a problem connecting to the database, please try again later!";
    }

    ...so instead of treating it as an object at line 69:
    $result = $database->query($query);

    go old school:
    $result = mysql_query($query);

    it's not as beautiful.. but at this stage that's probably not what you're worried about
     
  13. Steve C thread starter macrumors newbie

    Joined:
    Feb 19, 2009
    #13

    I was wondering if I could bypass PEAR (hoping, actually;)

    I just tried this... I now get past that line but I still choke on line 71:

    "Fatal error: Call to a member function numRows() on a non-object in /Users/steve/Sites/address book/listcontacts.php on line 71"


    Makes no sense!

    Here is the pertinent code section:

    $query = "SELECT * FROM contacts ORDER BY ".$_SESSION['sort']; // line 67

    $result = mysql_query($query); // line 69 (with your change)

    for ($i=0; $i<$result->numRows(); $i++) { // line 71

    $contact = $result->fetchRow(DB_FETCHMODE_ASSOC,$i); // line 73
     
  14. frankblundt macrumors 65816

    frankblundt

    Joined:
    Sep 19, 2005
    Location:
    South of the border
    #14
    objects objects and more objects ... :p

    The $result variable was an object, but now it's not (we changed that bit), it's a plain array... I suspect you're going to get deeper and deeper into the morass by trying to non-ojectify this code..

    but, it non-object world, this bit:
    for ($i=0; $i<$result->numRows(); $i++) { // line 71

    needs to now change to:
    $numRows = count($result);
    for ($i=0; $i<$numRows; $i++) { // line 71

    but then you're going to run into the same issue at line 73 - there are ways of coding around all this, but it gets to the point where you either need to re-write the whole thing, or leave it as is, and just work out how to get the original $database appearing as an object.

    Personally... if you're not comfortable with objects (I tolerate them myself, but we are not good friends yet), you are probably better off looking for a more friendly script to work with.

    Now, in any case, it is time to EAT!
     
  15. Steve C thread starter macrumors newbie

    Joined:
    Feb 19, 2009
    #15
    You're probably right. I chose this one because it wasn't loaded down with objects but I obviously need to get a handle on how they work if I'm going to be looking at code that has them. Someone told me I was better off learning php with OOP in mind from the start... now I'm not so sure;)

    Yes, I did run into line 73 and now I sort of understand why it's choking (I was just going through a tutorial on objects). Too bad... cuz that's the last bit of code that references objects in this script. Do you have PEAR running on Leopard?

    Thanks for all your help
    Steve C
     
  16. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #16
    Find:
    PHP:
    $query "SELECT * FROM contacts ORDER BY ".$_SESSION['sort']; // line 67
    $result mysql_query($query); // line 69 (with your change)
    for ($i=0$i<$result->numRows(); $i++) { // line 71
    $contact $result->fetchRow(DB_FETCHMODE_ASSOC,$i); // line 73
    Change to:
    PHP:
    $query "SELECT * FROM contacts ORDER BY ".$_SESSION['sort']; // line 67
    $result mysql_query($query); // line 69 (with your change)
    while ($row mysql_fetch_assoc($result)) {

         
    // field names returned in your query are now keys in a very convenient associative array, i.e.:
        
    echo $row['fieldname']; 

        
    // Additional processing here
        
    }
    Simple as that, you just had some Pear functions in there. Above is native PHP code. Note: It is good habit to define the list of field names in your query, not always use *, so you only pull what you need - makes for a faster web site sometimes. In the example above "fieldname" could be "contactID" or whatever.

    Not odd at all. Copy/paste from sites often picks up embedded format codes in the clipboard. I copy then paste to a text editor (Notepad or Wordpad in Windows, TextWrangler in Mac as was suggested earlier, although you do not have to code the whole page in it) then copy/paste it back to my development software (i.e. Rapidweaver, Dreamweaver, etc.) - the text editor acts as a filter. You can usually tell when to do this if the font is bold or colorized and the developer who posted the code did not wrap a <pre> tag which is easy to spot as the font and color abruptly changes.

    -jim
     
  17. Steve C thread starter macrumors newbie

    Joined:
    Feb 19, 2009
    #17
    Thanks Jim... as Frank said, some other things did break but this script is so basic I'm having fun trying to figure it out and fix it. Thanks to this post I am now moving data in an out... now to get the delete and edit pages working and have the edit/delete sequence appear on each entry and actually DO something;)

    Thanks to both of you
    Steve C
     

Share This Page