Trouble with MSSQL Query

Discussion in 'Databases' started by scottlpool2003, Jan 3, 2013.

  1. #1
    I'm in the middle of migrating a Linux server to a Windows server. In the process I need to change over the databases from MySQL to MSSQL.

    I'm not 'overly familiar' with MSSQL so kind of learning on the job.

    Anyway, I'm having a little trouble with a query throwing out the following error:

    Warning: mssql_query(): supplied argument is not a valid MS SQL-Link resource in D:\... on line 47
    
    Warning: mssql_num_rows(): supplied argument is not a valid MS SQL-result resource in D:\... on line 51
    
    Warning: mssql_fetch_array(): supplied argument is not a valid MS SQL-result resource in D:\... on line 60
    PHP:
    It's this section of code causing the problem:

    $resultb = mssql_query("
    SELECT       id, title, url
    FROM         cat_objects
    WHERE       parent_id = ",$resultRow['id'],"
    ORDER BY	 title ASC
    ");	
    PHP:
    Full code:

    <?php
    
    //GET CATEGORIES
    
    $numCols = 2;
    
    $result = mssql_query("
    SELECT       id, title, url
    FROM         categories
    ");
    
    
    //Count number of rows and divide into 2
    $numPerCol = ceil(mssql_num_rows($result) / $numCols);
    echo "<div style=\"width:364px;\">\n";
    for($col = 1; $col <= $numCols; $col++) {
       echo "<div style=\"width:100%;float:left;margin-left:5px;\">";
       for($row = 0; $row < $numPerCol; $row++) {
          $resultRow = mssql_fetch_assoc($result);
          if($resultRow == false) {
             break;
          }
    
    
    
    //Output data
          echo '<h3>',$resultRow['title'],'</h3>';
    
    //Get subcategories
    $resultb = mssql_query("
    SELECT       id, title, url
    FROM         cat_objects
    WHERE		 parent_id = ",$resultRow['id']," 
    ORDER BY	 title ASC
    ");	
    
    
    //Check if no results
    if(mssql_num_rows($resultb)==0){
      $message = "There are no subcategories";
    }
    
    else {
    $message = "";
    }
    
    
    while($rowb = mssql_fetch_array($resultb))
    
    //Add comma to all but last result
      {	
    	     if ($count++ > 0) echo " | ";
    
    	  echo "<a href=\"cat.php?id=",$rowb['urlb'],"\" title=\"$titleb\">",$rowb['title'],"</a>";
      }
    	echo "$message <br /><br />";
       }
       echo "\n</div>\n";
    }
    echo "</div>\n";
    ?>
    PHP:
    Help greatly appreciated.
     
    scottlpool2003, Jan 3, 2013 IP
  2. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #2
    Issue fixed.

    Code for anybody experiencing similar issues:

    <?php
    
    //GET CATEGORIES
    
    $numCols = 2;
    
    $result = mssql_query("
    SELECT       id, title, url
    FROM         categories
    ");
    
    
    //Count number of rows and divide into 2
    $numPerCol = ceil(mssql_num_rows($result) / $numCols);
    echo "<div style=\"width:364px;\">\n";
    for($col = 1; $col <= $numCols; $col++) {
       echo "<div style=\"width:100%;margin-left:5px;\">";
       for($row = 0; $row < $numPerCol; $row++) {
          $resultRow = mssql_fetch_assoc($result);
          if($resultRow == false) {
             break;
          }
    
    
    
    //Output data
          echo '<h3>',$resultRow['title'],'</h3>';
    
    //Get subcategories
    $resultb = mssql_query("
    SELECT       id, title, url
    FROM         cat_objects
    WHERE		 parent_id = " . $resultRow['id'] . " 
    ");	
    
    
    //Check if no results
    if(mssql_num_rows($resultb)==0){
      $message = "No categories yet.";
    }
    
    else {
    $message = "";
    }
    
    
    while($rowb = mssql_fetch_array($resultb))
    
    //Add space to all but last result
      {	
    	     if ($count++ > 1) echo " &nbsp; ";
    
    	  echo "<a href=\"cat.php?id=",$rowb['urlb'],"\" title=\"$titleb\">",$rowb['title'],"</a>";
      }
    	echo "$message <br /><br />";
       }
       echo "\n</div>\n";
    }
    echo "</div>\n";
    ?>
    PHP:
     
    scottlpool2003, Jan 3, 2013 IP