Argument is not a valid MS SQL-result resource

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JamieC
    New Member
    • Mar 2010
    • 3

    Argument is not a valid MS SQL-result resource

    I am using a text box to search database for a reference number, the search is working ok but i am getting this error when i load the page for the first time:

    Warning: mssql_fetch_obj ect(): supplied argument is not a valid MS SQL-result resource in C:\Documents . . . on line 167

    When i do a search i get the expected results and the error disappears

    here is my code:
    Code:
    <form id="refsearch" name="searchRef" method="post">
      <table>
        <tr>
          <td>Enter Call Ref</td>
          <td><input name="searchref" type="text" class="textfield" id="searchref" /></td>
        </tr>
        <tr>
          <td><input type="submit" name="search" value="Search" /></td>
        </tr>
    
    <?php
        $reference = $_POST['searchref'];
        $query = "SELECT AmberCatRequestId ";
        $query.= "FROM Customer_Calls ";
        $query.= "WHERE CustomerCallId = $reference";
        
        $result = mssql_query($query);
        if($row = mssql_fetch_object($result))
        {
            $ref = $row->AmberCatRequestId;
        }
        
    ?>
        <tr>
        <td><h4><br />AmberCat Request:&nbsp;</h1></td>
        <td><h1><br /><?echo $ref;?></h4></td>
        </tr>
      </table>
    </form>
    how do i stopped the error from appearing when the page is first loaded?
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    Can you please post your code in code format.

    Code:
    $reference = $_POST['searchref'];
    $query = "SELECT AmberCatRequestId ";
    $query.= "FROM Customer_Calls ";
    $query.= "WHERE CustomerCallId = $reference";
    
    $result = mssql_query($query);
    if($row = mssql_fetch_object($result))
    {
    $ref = $row->AmberCatRequestId;
    }
    
    ?>
    your PHP code start with the line
    $_POST['searchref']
    but when you load your page for the first time there is no $_POST['searchref'] exists

    take a closer look at isset() function. you would definitely get an error. so what you can do is first make sure
    whether $_POST['searchref'] exists. If it exists perform rest of the operation otherwise skip it.

    Regards,
    Johny
    Last edited by Dormilich; Mar 8 '10, 09:24 PM. Reason: Please use [code] tags when posting code

    Comment

    • jpr0325
      New Member
      • Mar 2010
      • 4

      #3
      First time when u load a page there won't be value set for $_POST['searchref'] so you query will be worng..

      Check condition if $_POST['searchref'] is not equal to null and then execute the query part.

      Hope this will help u...:)

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        see also PHP - Common Newbie Pitfalls, 2: Supplied argument is not a valid MySQL result

        Comment

        • JamieC
          New Member
          • Mar 2010
          • 3

          #5
          thanks jpr0325, this is exactly what i was looking for

          Comment

          Working...