gpt4 book ai didi

php - mysqli::query() 期望参数 1 为字符串,给定对象

转载 作者:行者123 更新时间:2023-11-29 12:11:06 25 4
gpt4 key购买 nike

我已保存了输出后续行的参数化字符串匹配查询所需的步骤。当转移到有故障的驱动器时我丢失了文件。所以......我试图将东西混在一起,但这是行不通的。

$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、chrisstring2 包含行:alice、claire、lara

如果 $string=string1 那么输出应该是:

克里斯马克鲍勃

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

最佳答案

假设“$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);

关于php - mysqli::query() 期望参数 1 为字符串,给定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30675593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com