PHP:如何将3D数组转换为mysql结果对象?

时间:2021-08-12 21:21:12

The answer might be write two functions! However, maybe a little knowledge that I do not have will help avoid a heck of a lot of re-factoring or database usage.

答案可能是写两个函数!但是,也许我没有的一些知识将有助于避免大量的重构或数据库使用。

I have a function with the very common syntax of:

我有一个很常见的语法的函数:

while($row = $db->sql_fetchrow($result)) {
// do some stuff
}

Most of the time, the $row will be a mysql object with zero, one or more rows therein. Simple.

在大多数情况下,$row将是一个mysql对象,其中有一个或多个行。简单。

However, it would be very handy if I could also utilise a three dimensional PHP array (previously built to be a bunch of "rows" with three key & value pairs) without handling them differently.

但是,如果我也可以使用一个三维的PHP数组(以前构建成一堆“行”,有三个键和值对),而不以不同的方式处理它们,这将非常方便。

The question I have is "What do I need to do to a 3D array to "convert" it to a mysql object that will work without change in the above example"?

我的问题是,“我需要对一个3D数组做什么,才能将它“转换”成一个mysql对象,在上面的例子中,这个对象将不会发生任何变化”?

1 个解决方案

#1


0  

There isn't a way to convert into a mysql object without writing to a tmp table and reading it back in. The following will deal with either sql statement or array turning up which then negates the need to convert as the question asked.

如果不写入tmp表并将其读入,就无法将其转换为mysql对象。下面将处理sql语句或出现的数组,这将消除在问题中进行转换的需要。

$all_rows = $notify->notification_sql(); // This either returns an SQL statement ready to run OR a set of rows in an array

if (!is_array($all_rows)) { // If it's not an array, run the SQL query and put it into an array of rows
    $result = $db->sql_query($all_rows);
    $all_rows = array();
    while($row = mysql_fetch_array($result)) {
        $all_rows[] = $row;
    }
}

foreach...

#1


0  

There isn't a way to convert into a mysql object without writing to a tmp table and reading it back in. The following will deal with either sql statement or array turning up which then negates the need to convert as the question asked.

如果不写入tmp表并将其读入,就无法将其转换为mysql对象。下面将处理sql语句或出现的数组,这将消除在问题中进行转换的需要。

$all_rows = $notify->notification_sql(); // This either returns an SQL statement ready to run OR a set of rows in an array

if (!is_array($all_rows)) { // If it's not an array, run the SQL query and put it into an array of rows
    $result = $db->sql_query($all_rows);
    $all_rows = array();
    while($row = mysql_fetch_array($result)) {
        $all_rows[] = $row;
    }
}

foreach...