blackman96
blackman96

Reputation: 11

ODBC query's not working with PHO

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

Upvotes: 1

Views: 390

Answers (1)

blackman96
blackman96

Reputation: 11

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.

Upvotes: 0

Related Questions