2015-06-05 68 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

相关问题