Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New Coder
    Join Date
    Aug 2012
    Posts
    65
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Catchable fatal error:

    Hello everyone. I have goten this code to work great except for 1 thing so far. Trying to get the dates to output from a DB. All other information will output, but when I add the:
    PHP Code:
    <td>".$result['DateEntered']."</td>
    <
    td>".$result['DateUpdated']."</td
    to the output the program errors out:
    Catchable fatal error: Object of class DateTime could not be converted to string in C:\Inetpub\wwwroot\msag\index.php on line 78

    The error seems pretty straight forward, however, I am not sure how to handle it.

    This is my code thusfar, any suggestions or help would be greatly appreciated.
    PHP Code:
    <center><h1><u> MSAG Database </u></h1>
    <br>            
    <br>
            
    <?php include 'includes/menu.php'?>
                
    <form method="post" action="index.php">
    <input type="hidden" name="submitted" value="true" />

    <label>Search Category:

    <select name="category">
        <option value="StreetName">Street Name</option>
        <option value="LOWNUMBER">Low Range</option>
        <option value="HighNumber">High Range</option>
        <option value="EOB">OEB</option>
        <option value="ESN">ESN Number</option>
        <option value="COMMUNITY">Community</option>
        <option value="TELCO">Telco</option>
        <option value="MAP">Map Number</option>
        <option value="DateEntered">Entry Date</option>
        <option value="DateUpdated">Last Modified</option>
    </select>
    </label>

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label>Search Criteria: <input type="text" name="criteria" /></label>

    <input type="submit" name="search" value="Search"/>

    </form>

    </center>
    <?php

    include 'includes/db/connect.php';

    if(isset(
    $_POST['search']) || isset($_POST['submit']))
    {     
        
    $category $_POST['category'];
        
    $criteria $_POST['criteria'];
        
        
    $query "SELECT * FROM MSAG WHERE $category LIKE '%$criteria%' ORDER BY StreetName ASC";
        
    $resource=sqlsrv_query($conn$query) or die(print_r(sqlsrv_errors(), true));
    }    
    ?>
    <form action="" method="post">
            <table align="center" border="1" cellpadding="2" cellspacing="2" width="100%" >
            <tr align = 'center'>
                <td><b>Prefix</b></td>
                <td><b>Street Name</b></td>
                <td><b>Low Range</b></td>
                <td><b>High Range</b></td>
                <td><b>OEB</b></td>
                <td><b>ESN Number</b></td>
                <td><b>Community</b></td>
                <td><b>Telco</b></td>
                <td><b>Map Number</b></td>
                <td><b>Entry Date</b></td>
                <td><b>Last Modified</b></td>
                <td><b>Details</b></td>
            </tr>
    <?php


    while($result=sqlsrv_fetch_array($resource))

        echo 
    "
    <tr align = center>
    <td>"
    .$result['StreetPrefix']."</td>
    <td>"
    .$result['StreetName']."</td>
    <td>"
    .$result['LOWNUMBER']."</td>
    <td>"
    .$result['HighNumber']."</td>
    <td>"
    .$result['EOB']."</td>
    <td>"
    .$result['ESN']."</td>
    <td>"
    .$result['COMMUNITY']."</td>
    <td>"
    .$result['TELCO']."</td>
    <td>"
    .$result['MAP']."</td>
    <td>"
    .$result['DateEntered']."</td>
    <td>"
    .$result['DateUpdated']."</td>
    <td><a href=\"modify2.php?id="
    .$result['StreetID']."\"><input type=\"button\" name = \"details\" value = \"Details\" /></a></td>
    </tr>
    "
    ;         
    }
    ?>
    </table>
    </form>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    That's interesting; it maps a datetime object to the results of the fetch? I'm not sure if I like that, let me verify in the api.
    The API doesn't indicate that it does this, but what you have here looks that it does. The solution is simple, you need to give it a format: $result['DateEntered']->format('F j Y, h:i:s') for example.
    Also, make sure you move the end } from the isset check to after the while loop. Otherwise it will throw an error on the fetch since $resource won't be defined as a valid sql resource.

  • #3
    New Coder
    Join Date
    Aug 2012
    Posts
    65
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ok, fixed the " bracket on the isset 'Thank you for that, by the way'. changed the Dateentered to this:
    PHP Code:
    <td>".$result['DateEntered'] ->format('Y-m-d H:i:s') ." </td
    and it will now output the date but it is still throwing this error:
    Fatal error: Call to a member function format() on a non-object in C:\Inetpub\wwwroot\msag\index.php on line 77

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    The only way to really do that is to use a check now. Seems like you can have null entries in there (which I assume map to null not an empty datetime). This is why I hate it when external libraries seem to think they should be responsible for constructing objects.
    You can use a ternary to check them:
    PHP Code:
    ($result['DateEntered'] instanceof DateTime) ? $result['DateEntered']->format('Y-m-d H:i:s') : $result['DateEntered'
    And do the same with the date updated. Not sure what it is otherwise, it could technically be an integer or simply null. So I just left it raw.

  • #5
    New Coder
    Join Date
    Aug 2012
    Posts
    65
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Okay. I tried the code like that and this is what I got.:

    Code:
    2000-06-27 00:00:002004-06-09 00:00:002004-06-09 00:00:002005-03-11 00:00:002004-06-10 00:00:002004-05-11 00:00:002005-03-22 00:00:002005-03-10 00:00:002005-04-26 00:00:002005-03-03 00:00:002012-11-21 00:00:002005-03-11 00:00:002005-03-11 00:00:002009-03-24 00:00:002005-04-28 00:00:002005-03-08 00:00:002005-04-25 00:00:002004-09-22 00:00:002005-05-11 00:00:002005-04-28 00:00:002005-03-21 00:00:002005-04-28 00:00:002005-03-10 00:00:002005-03-10 00:00:002005-04-22 00:00:002004-05-11 00:00:002005-03-24 00:00:002009-03-24 00:00:002012-11-21 00:00:002005-05-11 00:00:002005-03-08 00:00:002005-03-21 00:00:002004-03-11 00:00:002005-03-24 00:00:002005-03-18 00:00:002005-10-19 00:00:002004-09-17 00:00:002005-03-16 00:00:002005-03-10 00:00:002007-12-04 00:00:002005-03-10 00:00:002004-10-21 00:00:002004-03-10 00:00:002008-06-05 00:00:002005-04-28 00:00:002005-03-29 00:00:002004-03-05 00:00:002005-03-08 00:00:002012-11-21 00:00:002005-04-28 00:00:002005-04-25 00:00:002005-03-29 00:00:002005-03-23 00:00:002005-04-25 00:00:002005-06-15 00:00:002005-04-25 00:00:002005-06-15 00:00:002005-03-22 00:00:002005-04-04 00:00:002005-03-23 00:00:002004-09-17 00:00:002004-09-17 00:00:002005-03-08 00:00:002004-10-21 00:00:002012-11-21 00:00:002012-11-21 00:00:002004-03-04 00:00:002005-04-26 00:00:002004-09-20 00:00:002005-04-22 00:00:002004-03-05 00:00:002005-03-23 00:00:002004-05-07 00:00:002005-03-09 00:00:002005-03-08 00:00:002005-03-16 00:00:002005-03-23 00:00:002005-03-17 00:00:002005-04-28 00:00:002005-04-28 00:00:002009-03-24 00:00:002004-03-11 00:00:002005-03-16 00:00:002005-06-15 00:00:002004-06-11 00:00:002005-04-28 00:00:002005-03-04 00:00:002008-04-17 00:00:002005-03-09 00:00:002005-04-04 00:00:002005-03-04 00:00:002005-04-26 00:00:002005-04-04 00:00:002005-03-28 00:00:002004-05-07 00:00:002005-03-03 00:00:002004-09-20 00:00:002004-09-22 00:00:002005-06-15 00:00:002005-06-15 00:00:002005-06-15 00:00:002004-03-12 00:00:002004-03-12 00:00:002005-04-26 00:00:002005-03-16 00:00:002004-03-05 00:00:002006-02-20 00:00:002005-03-22 00:00:002005-04-26 00:00:002005-04-26 00:00:002005-04-28 00:00:002005-06-15 00:00:00 Fatal error: Call to a member function format() on a non-object in C:\Inetpub\wwwroot\msag\index.php on line 78

    This is line 78:
    PHP Code:
    <td>".($result['DateEntered'] instanceof DateTime) ? $result['DateEntered']->format('Y-m-d H:i:s') : $result['DateEntered'] ." </td

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    That doesn't appear to match the lines you have here.
    You can't trigger an error on the format there since it must be an instance of DateTime in order to issue the format call. If $result['DateEntered'] is not an instanceof DateTime (null isn't an instance, nor is any scalar or non-DateTime object), then it will drop to the ternary else. You can't have a DateTime datatype without having a valid DateTime object; so null's or any scalar (ie: non-object) type cannot pass an instanceof check. Unless there's a bug of course, but I don't believe I've seen one like this.

    Post more code for this; I suspect that you have another format call elsewhere and the line number you are looking at may be incorrect. I still can't get over that the SQLServer is automatically generating DateTime objects. Its also possible that you may be in a do/while?

  • #7
    New Coder
    Join Date
    Aug 2012
    Posts
    65
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Sorry about the lack of documentation. Here is the code from the whole page:

    PHP Code:
    <center><h1><u> MSAG Database </u></h1>
    <br>            
    <br>
            
    <?php include 'includes/menu.php'?>
                
    <form method="post" action="index.php">
    <input type="hidden" name="submitted" value="true" />

    <label>Search Category:

    <select name="category">
        <option value="StreetName">Street Name</option>
        <option value="LOWNUMBER">Low Range</option>
        <option value="HighNumber">High Range</option>
        <option value="EOB">OEB</option>
        <option value="ESN">ESN Number</option>
        <option value="COMMUNITY">Community</option>
        <option value="TELCO">Telco</option>
        <option value="MAP">Map Number</option>
        <option value="DateEntered">Entry Date</option>
        <option value="DateUpdated">Last Modified</option>
    </select>
    </label>

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label>Search Criteria: <input type="text" name="criteria" /></label>

    <input type="submit" name="search" value="Search"/>

    </form>

    </center>
    <?php

    include 'includes/db/connect.php';

    if(isset(
    $_POST['search']) || isset($_POST['submit']))
    {     
        
    $category $_POST['category'];
        
    $criteria $_POST['criteria'];
        
        
    $query "SELECT * FROM MSAG WHERE $category LIKE '%$criteria%' ORDER BY StreetName ASC";
        
    $resource=sqlsrv_query($conn$query) or die(print_r(sqlsrv_errors(), true));


    echo 
    "
    <form action=\"\" method=\"post\">
            <table align=\"center\" border=\"1\" cellpadding=\"2\" cellspacing=\"2\" width=\"100%\" >
            <tr align = 'center'>
                <td><b>Prefix</b></td>
                <td><b>Street Name</b></td>
                <td><b>Low Range</b></td>
                <td><b>High Range</b></td>
                <td><b>OEB</b></td>
                <td><b>ESN Number</b></td>
                <td><b>Community</b></td>
                <td><b>Telco</b></td>
                <td><b>Map Number</b></td>
                <td><b>Entry Date</b></td>
                <td><b>Last Modified</b></td>
                <td><b>Details</b></td>
            </tr>
    "
    ;

    while(
    $result=sqlsrv_fetch_array($resource))

        echo 
    "
    <tr align = center>
    <td>"
    .$result['StreetPrefix']."</td>
    <td>"
    .$result['StreetName']."</td>
    <td>"
    .$result['LOWNUMBER']."</td>
    <td>"
    .$result['HighNumber']."</td>
    <td>"
    .$result['EOB']."</td>
    <td>"
    .$result['ESN']."</td>
    <td>"
    .$result['COMMUNITY']."</td>
    <td>"
    .$result['TELCO']."</td>
    <td>"
    .$result['MAP']."</td>
    <td>"
    .($result['DateEntered'] instanceof DateTime) ? $result['DateEntered']->format('Y-m-d H:i:s') : $result['DateEntered'] ." </td>
    <td>"
    .($result['DateUpdated'] instanceof DateTime) ? $result['DateUpdated']->format('Y-m-d H:i:s') : $result['DateUpdated'] ." </td>
    <td><a href=\"modify2.php?id="
    .$result['StreetID']."\"><input type=\"submit\" name = \"details\" value = \"Details\" /></a></td>
    </tr>
    "
    ;         
    }
    }    
    ?>
    </table>
    </form>

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Comment out the entire echo in the loop. Add this instead and post the results you get (drag it from source to keep it formatted):
    PHP Code:
    $i 0;
    while(
    $result=sqlsrv_fetch_array($resource))
    {
        print 
    str_pad('[Start ' $i ']'30'-'STR_PAD_BOTH) . PHP_EOL;
        
    var_dump($result['DateEntered'], $result['DateUpdated']);
        print 
    str_pad('[End ' $i++ . ']'30'-'STR_PAD_BOTH) . PHP_EOL PHP_EOL;
    // your commented out code would be here.


  • #9
    New Coder
    Join Date
    Aug 2012
    Posts
    65
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Okay, this is the output I get now (I had to add a LIMIT to the querry though, as it output all 4265 records if not):

    PHP Code:
    $query "SELECT * FROM MSAG WHERE $category LIKE '%$criteria%' ORDER BY StreetName ASC LIMIT 1"
    PHP Code:
    Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 102 [code] => 102 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near 'LIMIT'. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near 'LIMIT'. ) ) 

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    SQL Server doesn't have a LIMIT command. You can SELECT TOP X instead.
    Use the top 150. It pulls 112 records before it tosses an error in your original output.

  • #11
    New Coder
    Join Date
    Aug 2012
    Posts
    65
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Is this the line in output you are refering to?
    PHP Code:
    ---------[Start 112]---------- NULL NULL ----------[End 112
    If so. Do you have any advice on how I can fix this issue?

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yep, but nope I don't.
    null is incapable of comparing itself with an instanceof check, so you cannot pull up the method off of a non-object on it.

    I did find the problem though, and I didn't really think it would be an issue, but admittedly I typically use printf so I don't notice things like the concat.
    Precedence order of the concat is considered higher than that of the ternary. I'm not sure what it *thinks* its supposed to do with it, but its not what we say it's supposed to do with it.
    Simply wrap the ternary in ():
    PHP Code:
     (($result['DateEntered'] instanceof DateTime) ? $result['DateEntered']->format('Y-m-d H:i:s') : $result['DateEntered']) 
    for both of them, and it should work.

  • #13
    New Coder
    Join Date
    Aug 2012
    Posts
    65
    Thanks
    5
    Thanked 0 Times in 0 Posts
    That works like a charm so far. Thank you very much. I have another issue with the same code, I am trying to get it to where I can have the user select from multiple drop down menus and run the querry with that. Is that something I should open another thread for?

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yep, that's a different thing so best to create a new thread for that.

  • Users who have thanked Fou-Lu for this post:

    willscarlet (11-26-2012)

  • #15
    New Coder
    Join Date
    Aug 2012
    Posts
    65
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Will do. Thank you again, very much


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •