mysqli :: query()期望参数1为字符串,给定对象 [英] mysqli::query() expects parameter 1 to be string, object given

查看:109
本文介绍了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()){

    }

}

我已经知道这是错误的,我不使用参数化结果,而是尝试使用数组索引,例如$ row [0].

我为此被大喊大叫.

例如,我想要的最终结果是

string1具有以下行:bob,mack,chris string2有行:爱丽丝,克莱尔,劳拉

如果$ string = string1,则输出应为:

克里斯 马克 鲍勃

我相信我的问题是我正在混合语句类型

解决方案

假定"$ link"是PHP的"mysqli"类的一个实例,并且"id"和"Id"是表中的两个不同列(如果不是这种情况,请尝试在".. ORDER BY Id .."段中用"id"替换"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);

I had saved the required steps for a parameterized string match query that outputs the subsequent rows. I had lost my files when transferring to a faulty drive. So... I'm trying to mash things together and this isn't working.

$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()){

    }

}

I can already tell that this is wrong, I don't use the parameterized results and I'm trying to use array indexes such as $row[0].

Going to get yelled at for this one I know.

The end result I want is for example:

string1 has rows: bob, mack, chris string2 has rows: alice, claire, lara

If $string=string1 then the output should be:

chris mack bob

I believe my problem is I am mixing statement types

解决方案

Assuming that "$link" is an instance of PHP's "mysqli" class, and that "id" and "Id" are two diffrent columns in your table (if it's not the case, please try replacing "Id" with "id" in the segment ".. ORDER BY Id.."), here is, based on your example, what I suggest that you try:

// 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);

这篇关于mysqli :: query()期望参数1为字符串,给定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆