1

I'm working with an ODBC connection rather than MySQL. I have a search function which I have more or less copied over to apply with my ODBC connection, however they are not working. Here is my code except for the connection:

<!doctype html>
<html>
<title> Quoting System </title>
<head>
</head>
<body>

<form class="form" method="POST" action='try31.php'>
        Quote Number <input  class="form-control" type="text" name="quote" id="quote" placeholder="Enter Quote Number">
        <br> &nbsp <input class="btn btn-default" type="submit" name="search" value="Search">
</form>     

<table>
    <tr>
        <th>Company Name</th>
        <th>Address1</th>
        <th>Address2</th>
    </tr>   

<?php

  if (!$conn){
    if (phpversion() < '4.0'){
      exit("Connection Failed: . $php_errormsg" );
    }
    else{
      exit("Connection Failed:" . odbc_errormsg() );
    }
}

if(isset($_POST['search'])){ 
$quote = $_POST['quote'];
$query = "SELECT * FROM dbo.tblVersions2 WHERE QuoteNumber LIKE '".$quote."'";
}

$result = odbc_exec($conn,$query);

    while($row =odbc_fetch_row($result)){
        echo "<tr>";
            echo "<td>".$row[2]."</td>";
            echo "<td>".$row[3]."</td>"; 
            echo "<td>".$row[4]."</td>";
        echo "</tr>";
}



// Disconnect the database from the database handle.
//odbc_close($conn);

?>
</table>
</body>
</html>

As I don't get an error message, I know my connection is working, however currently, when I select the button, data does not appear as expected... Please help! Thanks

blackman96
  • 11
  • 1
  • Does it give you any error? What database you use under ODBC? What system you try to run it on? `(phpversion() < '4.0')` PHP 4.0 was released in May 2000, I really doubt people still use anything before it. – E_p Jul 13 '16 at 15:03
  • And I do not see code that creates connection in your example – E_p Jul 13 '16 at 15:05
  • Hi, It doesn't give an error my connection code is:$conn=odbc_connect($data_source,$user,$password); if (!$conn){ if (phpversion() < '4.0'){ exit("Connection Failed: . $php_errormsg" ); } else{ exit("Connection Failed:" . odbc_errormsg() ); } } if ($conn) { echo "Connected."; } – blackman96 Jul 13 '16 at 15:23
  • When I click on the button to execute my query, the table does not fill as expected, I do not get an error. I use SQL Server 2008, PHP 5.3.10 – blackman96 Jul 13 '16 at 15:24
  • ` the table does not fill as expected` Does it fill wrong or not at all? – E_p Jul 13 '16 at 15:33
  • The table does not fill at all. – blackman96 Jul 13 '16 at 15:35
  • Try to use http://php.net/manual/en/function.odbc-error.php to see if there any errors – E_p Jul 13 '16 at 20:45

1 Answers1

0

Found that with MS SQL ODBC connection the query syntax is different than MySQL. I changed where I was calling my columns in the table from:

$result = odbc_exec($conn,$query);

while($row =odbc_fetch_row($result)){
    echo "<tr>";
        echo "<td>".$row[2]."</td>";
        echo "<td>".$row[3]."</td>"; 
        echo "<td>".$row[4]."</td>";
    echo "</tr>";

}

To this:

 $result = odbc_exec($conn, $stmt);

  while (odbc_fetch_row($result)) // while there are rows
  {
     echo "<tr>";
        echo "<td>" . odbc_result($result, "CompanyName") . "</td>";
        echo "<td>" . odbc_result($result, "Address1") . "</td>";
     echo "</tr>";
  }

The odbc_result function was crucial here.

blackman96
  • 11
  • 1