English 中文(简体)
SQL 使用 ODBC 查询
原标题:SQL Query using ODBC
  • 时间:2012-05-23 21:24:10
  •  标签:
  • php
  • sql
  • odbc

此脚本 :

 <?php    
    $myfamille=$_POST[ myfamille ];

    $conn = odbc_connect( sage , <Administrateur> ,  );

    if (!$conn) 
    {exit("Connection Failed: " . $conn);} 




    $sql="Select F_ARTSTOCK.AR_Ref,AR_Design,AS_QteSto 
    FROM F_ARTICLE,F_FAMILLE,F_ARTSTOCK
    where F_ARTICLE.FA_CodeFamille=F_FAMILLE.FA_CodeFamille
    AND F_ARTICLE.AR_Ref=F_ARTSTOCK.AR_Ref
    AND F_FAMILLE.FA_CodeFamille= ".$myfamille." 
    and F_ARTSTOCK.AS_QteSto <> 0";

    $rs=odbc_exec($conn,$sql);
    if (!$rs) 
    {exit("Error in SQL");} 
    while($e=odbc_fetch_object($rs))
           {$output[]=$e;}
    print(json_encode($output));
    ?>

给我这个错误:

Notice: Undefined variable: output in C:wampwwwarticlecbase.php on line 24

请注意,删除这条线使代码有效,我不知道有什么问题

AND F_FAMILLE.FA_CodeFamille= ".$myfamille." 

我也有类似的脚本,但用 Sql 服务器,效果很好

<?php
 $myservername=$_POST[ myservername ];
$servername=".\".$myservername;
$myusername=$_POST[ myusername ];
$mypassword=$_POST[ mypassword ];
$db_name="bijou";
$myfamille=$_POST[ myfamille ];
  $connectionInfo = array( "Database"=>$db_name, "UID"=>$myusername, "PWD"=>$mypassword);
$conn = sqlsrv_connect( $servername, $connectionInfo);

if (!$conn) 
{exit("Connection Failed: " . $conn);} 


$sql="Select F_ARTSTOCK.AR_Ref,AR_Design,AS_QteSto FROM F_Article,F_Famille,F_ARTSTOCK
where F_ARTICLE.FA_CodeFamille=F_FAMILLE.FA_CodeFamille
AND F_ARTICLE.AR_Ref=F_ARTSTOCK.AR_Ref
AND F_FAMILLE.FA_CodeFamille= ".$myfamille."  and F_ARTSTOCK.AS_QteSto != .000000";


$rs=sqlsrv_query($conn,$sql); 
if (!$rs) 
{exit("Error in SQL");} 

while($e=sqlsrv_fetch_object($rs))
       { $output[]=$e;}

print(json_encode($output));
?>

并使用有机碳化合物的工艺:

     <?php    
$myusername=$_POST[ myusername ];
$conn = odbc_connect($myusername, <Administrateur> ,  );

if (!$conn) 
{exit("Connection Failed: " . $conn);} 

$sql="SELECT   FA_CodeFamille AS FA_CodeFamille,FA_Intitule AS FA_Intitule FROM   F_FAMILLE";
$rs=odbc_exec($conn,$sql);

if (!$rs) 
{exit("Error in SQL");} 
while($e=odbc_fetch_object($rs))
       { $output[]=$e;}
print(json_encode($output));

?>

Please help me. regards


$output = array();
while($e=odbc_fetch_object($rs)) {
    $output[] = $e;
}

Is working fine on many scripts i have any help please

问题回答

PHP在抱怨,因为变量在使用之前没有申报。

$output = array();
while($e=odbc_fetch_object($rs)) {
    $output[] = $e;
}

否则,如果循环未输入,则该变量未申报,因此,当您 json_encode($output) , output 不存在。

另外,我建议你放弃旧的ODBC方法,转向PDO(或至少是PDO ODBC驱动器)。

此外,您的代码可以输入 SQL 。 您应该使用已准备好的语句, 或者正确回避您插入查询的字符串 。 也就是说, 您应该查看 < a href=" http://php. net/ odbc_ prepare" rel = “ no follow” >odbc_ prepare

此外,$_POST[ key] 永远不能保证有人居住。 您应该总是使用类似 :

$blah = (isset($_POST[ blah ])) ? $_POST[ blah ] : null;

$blah = (array_key_exists($_POST[ blah ])) ? $_POST[ blah ] : null;

或者,如果你特别偏执 像我一样:

$blah = (array_key_exists($_POST[ blah ]) && is_string($_POST[ blah ])) ? $_POST[ blah ] : null;

If the user has PHP parse it as an array, err或s can then occur from passing an array to a function. This can make PHP unnecessary generate notices/err或s, so I like to avoid that possibility.

(An example of f或cing $_GET[ test ] to be an array would be page.php?test[]=blah)





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签