Consultation in asp works but in php does not work

Posted on 2006-05-14
Last Modified: 2013-12-12
QUERY in asp works but in php does not work , I NEED THAT THIS QUERY WORKS IN PHP
The code php is this

$conn = odbc_connect('ANDRES','AFCORTES','ASrds5a');


if ($conn)
$sql =  "SELECT *  FROM CCOP2XBDT.F5Z7501  WHERE $AVMCU = '445000';
//this function will execute the sql satament
$result=odbc_exec($conn, $sql);

echo "<table  align=\"center\" border=\"1\" borderColor=\"\" cellpadding=\"0\" cellspacing=\"0\">\n";
echo "<tr> ";
// -- print field name
$colName = odbc_num_fields($result);
for ($j=1; $j<= $colName; $j++)
echo "<th  align=\"left\" bgcolor=\"#CCCCCC\" > <font color=\"#990000\"> ";
echo odbc_field_name ($result, $j );
echo "</font> </th>";
// end of field names
while(odbc_fetch_row($result)) // getting data
 if ( $c%2 == 0 )
 echo "<tr bgcolor=\"#d0d0d0\" >\n";
 echo "<tr bgcolor=\"#eeeeee\">\n";
       echo "<td>";
       echo odbc_result($result,$i);
       echo "</td>";        
       if ( $i%$j == 0 )  
           $nrows+=1; // counting no of rows    
   echo "</tr>";

echo "</td> </tr>\n";
echo "</table >\n";
// --end of table  
if ($nrows==0) echo "<br/><center> Sin datos $month  que concuerden con su busqueda</center>  <br/>";
else echo "<br/><center> Total Records:  $nrows </center>  <br/>";
odbc_close ($conn);

else echo "odbc not connected <br>";


<!--#include file="Connections/PERRITOS.asp" -->
Dim Recordset1__MMColParam
Recordset1__MMColParam = "      445000"
If (Request("MM_EmptyValue") <> "") Then
  Recordset1__MMColParam = Request("MM_EmptyValue")
End If
Dim Recordset1
Dim Recordset1_numRows

Set Recordset1 = Server.CreateObject("ADODB.Recordset")
Recordset1.ActiveConnection = MM_PERRITOS_STRING
Recordset1.Source = "SELECT *  FROM CCOP2XBDT.F5Z7501  WHERE $AVMCU = '" + Replace(Recordset1__MMColParam, "'", "''") + "'"
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 1

Recordset1_numRows = 0
Set Recordset1 = Nothing



Warning: odbc_exec(): SQL error: [IBM][iSeries Access ODBC Driver][DB2 UDB]SQL0104 - Token = was not valid. Valid tokens: ( + - ? : DAY NOT RRN CASE CAST CHAR DATE DAYS HASH HOUR LEFT., SQL state 37000 in SQLExecDirect in c:\appserv\www\new_intra\odbc\funciona.php on line 15

Warning: odbc_num_fields(): supplied argument is not a valid ODBC result resource in c:\appserv\www\new_intra\odbc\funciona.php on line 20

Warning: odbc_fetch_row(): supplied argument is not a valid ODBC result resource in c:\appserv\www\new_intra\odbc\funciona.php on line 30

Question by:andretty76
    LVL 16

    Accepted Solution

    $sql =  "SELECT *  FROM CCOP2XBDT.F5Z7501  WHERE $AVMCU = '445000';

    your field starts with an $, which resembles a PHP variable, and enclosing your sql statment within a douple quates force php to treat $AVMCU as a variable. you got to escape it
    $sql =  "SELECT *  FROM CCOP2XBDT.F5Z7501  WHERE \$AVMCU = '445000';

    didn't read rest of the code, test that and post your result here again
    LVL 40

    Expert Comment

    In you ASP code, you are replacing ' with ''.

    Recordset1.Source = "SELECT *  FROM CCOP2XBDT.F5Z7501  WHERE $AVMCU = '" + Replace(Recordset1__MMColParam, "'", "''") + "'"

    So, the PHP equivalent would be to use the str_replace function

    $new_value = str_replace("'", "''", $old_value);

    This will do the same as the Replace() function you used in ASP.

    I would like to see the EXACT sql statement that is attempting to be used.

    Before the odbc_exec() statement can you ...

    echo ">>>$sql<<<";

    and then view-source on the results page and cut and paste everything between >>> and <<<.

    DO NOT SIMPLY CUT AND PASTE FROM THE BROWSER WINDOW AS THIS IS A VIEW OF THE DATA NOT THE DATA!!!!!! Browsers do all sorts of things to data (remove spaces, interpret values as symbols, etc).

    LVL 40

    Expert Comment

    The purpose of the echo is to help you diagnose the problem. It is NOT a solution. Yet!
    LVL 40

    Expert Comment

    Can you try removing the spaces from either side of the = sign?

    Can you try replacing = with the word EQUALS

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now