2012-06-28 50 views
0

當調用成員函數在非對象上組合string()時遇到問題。解決調用成員函數在非對象上的combinetring()

**Index.php** 
inlcude("string.php"); 
calldata('usa'); 


**string.php** 
$a=new a(); 
funciton calldata($val){ 
$st1="select a from table 1 where country=".$a->combinestring($val); 
return $st1; 
} 

**Class A** 
function combinestring($abc){ 
    Return "'".$abc."'"; 
} 

未知$ a-> combinetring($ val);

如何解決這個問題。

問候

回答

0

你得到錯誤

調用一個成員函數combinestring()非對象

上,因爲你是在變量調用一個成員函數那不是一個對象。這意味着$a不是一個對象。

在string.php中,不能在函數定義中使用$a,因爲變量具有局部範圍。你不能像那樣訪問那個對象實例。但是,您可以通過使用全局變量來完成此操作

string.php文件應該是這樣的:

$a=new a(); 
funciton calldata($val){ 
    global $a; 
    $st1="select a from table 1 where country=".$a->combinestring($val); 
    return $st1; 
} 

前往此鏈接的變量範圍的更多信息:http://php.net/manual/en/language.variables.scope.php

0

使用PDO

funciton calldata($val){ 
    $st1="select a from table 1 where country = ?"; 
    $pdo = new PDO('host', 'user', 'pass'); 
    $result = $pdo->prepare($st1)->execute($val); 
    return $result; 
} 

這是你在做什麼了很多不同的,但你的a類沒有逃脫輸入查詢,這就是不好的。

相關問題