2015-06-05 66 views
0

我已經保存了輸出後續行的參數化字符串匹配查詢所需的步驟。轉移到故障驅動器時,我丟失了文件。所以...我試圖把東西混合在一起,這是行不通的。mysqli :: query()期望參數1是字符串,給出的對象

$stmt = $link->prepare("SELECT id,entry,date from table WHERE string=? ORDER by Id DESC"); 
$stmt->bind_param('s',$string); 
$stmt->execute(); 
$stmt->bind_result($id_db,$entry_db,$date_db); 

if (($result = $link->query($stmt)) { 

    while ($row = $result->fetch_row()){ 

    } 

} 

我已經可以告訴大家,這是錯誤的,我不使用參數化的結果,我嘗試使用數組索引,如$行[0]。

要知道,這個人會被大吼一聲。

最終的結果我想要的是例如:

字符串1具有行:鮑勃,麥克,克里斯 字符串2具有行:愛麗絲,克萊爾,拉拉

如果$字符串=字符串1,則輸出應該是:

克里斯 麥克 鮑勃

我相信我的問題是,我混合語句類型

+1

請在你的MySQL數據庫上創建一個」DESCRIBE表「並更新這篇文章的結果。 –

+0

你好Emiliano Sangoi,我必須弄清楚那是什麼,我沒有碰到過 – janicehoplin

+1

在mysql中,DESCRIBE命令顯示了表的結構 –

回答

1

假設「$ link」是PHP的「mysqli」類的一個實例,並且「id」和「Id」是表中的兩個不同列(如果不是這種情況,請嘗試用「id」替換「Id」 「在段」.. ORDER BY ID ..「),這裏是,根據你的例子,我建議你嘗試:

// Declare your "prepare" statement (make sure to change "Id" for "id" if both are used 
// in reference to the same column in your table) 
$stmt = $link->prepare('SELECT id, entry, date FROM table WHERE string = ? ORDER BY Id DESC'); 

// Bind the $string variable 
$stmt->bind_param('s',$string); 

// Execute the statement 
$stmt->execute(); 

// Store the result (this is sometimes useful, depending on the data types in your table) 
$stmt->store_result(); 

// Check whether at least one row in table matches the query, if not, stop here... 
if ($stmt->num_rows === 0) exit('No matching rows'); // or do something else... 

// Declare a container (i.e. storage) for each row (this is optional and depends on what 
// you are trying to achieve) 
$data = []; 

// Loop through results (this is just an example; this could be achieved in a number 
// of different ways) 
for ($i = 0; $i < $stmt->num_rows; $i++) 
{ 
    // Add a new array cell to $data, at index $i 
    $data[$i] = []; 

    // Bind result for row $i 
    $stmt->bind_result($data[$i]['id'],$data[$i]['entry'],$data[$i]['date']); 

    // Fetch $i^{th} row 
    $stmt->fetch(); 
} 

// Check if it worked (temporary) 
var_dump($data); 
+0

感謝您的回覆。手冊中面向對象的例子,適用於我,但我會分析您的答案,以便將來使用。 http://php.net/manual/en/mysqli-stmt.fetch.php – janicehoplin

相關問題