简体   繁体   中英

Using PHP with ODBC and sql?

HI there just a little problem I can't seem to fix. I'm trying to read a sql output into a single string that can then be used to enter parameters into a select box. The only problem being that when I try and run the query it comes back as gibberish (that's a technical term)

Can anyone see any problems in the query?

<?php
$connection=odbc_connect('Hesk','Trace_user','Cwmbran1');

$query= "SELECT DISTINCT
    STUFF(
        (
            SELECT + '#' + [AssetName] + ', ' + CONVERT(NVARCHAR(3), [AssetID] ) + 'ID' +'#NEXT'
            FROM tblAssets
            ORDER BY [AssetName]
            FOR XML PATH ('')
        ) + '#', 1, 1, ''
    ) AS AssetList
    FROM tblAssets;";

$Assets = odbc_exec($connection, $query);
$AssetResult = odbc_result($Assets, 1);

echo $AssetResult

odbc_clode($connection);
?>

The expected output should be something like this:

10.19.0.229, 201ID#NEXT#10.19.0.82, 293ID#NEXT#37-RUSSELL, 57ID#NEXT#3COM CORPORATION 10.19.0.62, 318ID#NEXT#

However the output I'm getting is:

ÕWtstsádÉnÿÿÿÿ”Én ÿÿÿÿÄÉnÿÿÿÿT9m¦”ôÉnÿÿÿÿ”:m A4Ù‘ÿÿÿÿ|>m›jc*` Vÿÿÿÿ!á!!@÷Y!2_DB_Username_Password_2YÈöÈö)À³ôPÎW0ÌWÙ#) ×W ×Wø STUFF( (SELECT + '#' + [AssetName] + ', ' + CONVERT(NVARCHAR(3), [AssetID] ) + 'ID' +'#NEXT' FROM tblAssets ORDER BY [AssetName] FOR XML PATH ('')) + '#' , 1, 1, '') AS AssetList FROM tblAssets;'°ØW°ØWø

Can anyone offer any help? The only thing i can think is I'm displaying the query and not the result but I have no idea, I'm not too familiar with php.

Shouldn't you call odbc_fetch_row before calling odbc_result ?

Check example here: http://phplens.com/phpeverywhere/node/view/9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM