PHP mysqli_fetch_assoc用于将数据存储在数组中

时间:2022-09-13 20:13:46

I have a list of url's(link) in my database and can echo the data to the page fine but instead of outputting it, I need to store that info(I was thinking an array) into a variable to perform php tasks using the provided links. I have yet to figure out how to do this.

我在我的数据库中有一个url(链接)列表,可以将数据回显到页面,但不是输出它,我需要将该信息(我正在考虑一个数组)存储到变量中,以使用提供的方式执行php任务链接。我还没弄明白怎么做。

The code has been updated I removed any references to using the soon to be deprecated mysql_* functions and opted for the mysqli version.

代码已更新我删除了使用即将被弃用的mysql_ *函数的任何引用,并选择了mysqli版本。

Heres my code

继承我的代码

$query = "SELECT `Link` FROM `Table1` WHERE `Image` ='' AND `Source`='blah'";

if ($result = mysqli_query($dblink, $query)) {

while ($row = mysqli_fetch_assoc($result)) {  
    $link = $row['Link'];
    // echo ''.$link.'<br>';
        $html = file_get_html($link);
        foreach ($html->find('div.article') as $e) {
            $result = $e->find('img', 0);
            $imgsrc = $result->src . '<br>';
            echo $imgsrc;
        }
    }
}

This code is working through one iteration: It will find the first link stored in the DB, use that $link in the bottom foreach() statement and output the desired result. After the first iteration of the loop, an error occurs stating:

这段代码正在进行一次迭代:它将找到存储在DB中的第一个链接,在底层的foreach()语句中使用该$ link并输出所需的结果。循环的第一次迭代后,发生错误说明:

"mysqli_fetch_assoc() expects parameter 1 to be a mysql result"

“mysqli_fetch_assoc()期望参数1是一个mysql结果”

I think I understand why the problem is occurring - Since the $result is declared outside of the while loop, it is never set again after the first iteration/or changes in some way.

我想我理解为什么会出现这个问题 - 因为$ result是在while循环之外声明的,所以在第一次迭代之后它永远不会再次设置/或以某种方式改变。

or

I should be using mysqli_free_result() possibly, If that were the case I am not sure where it would go in the code.

我应该使用mysqli_free_result(),如果是这种情况,我不确定代码中的位置。

Thanks for any help you can offer!

谢谢你尽你所能的帮助!

4 个解决方案

#1


1  

When you do this:

当你这样做:

$result = mysqli_query($dblink, $query);

The functions return a link identifier you store in $result. This identifier we need to pass to fetch functions in order to be able to show it from which result to fetch. It shouldn't be changed until you are done fetching all the results you want.

这些函数返回您存储在$ result中的链接标识符。我们需要传递给提取函数的这个标识符,以便能够从中获取结果。在获取所需的所有结果之前,不应更改它。

This goes right the first time:

这是第一次:

$row = mysqli_fetch_assoc($result)

But then, in the foreach, you overwrite that variable with other information:

但是,在foreach中,您用其他信息覆盖该变量:

$result = $e->find('img', 0);

As such, when the next iteration comes around, it is no longer a valid result identifier, so MySQL doesn't know what to do with it.

因此,当下一次迭代出现时,它不再是有效的结果标识符,因此MySQL不知道如何处理它。

The fix is actually rather simple, you need to change the name of the variable you are using in the foreach:

修复实际上非常简单,您需要更改foreach中使用的变量的名称:

$result = $e->find('img', 0);
$imgsrc = $result->src . '<br>';

Becomes:

$found= $e->find('img', 0);
$imgsrc = $found->src . '<br>';

And voila, it should work...

瞧,它应该工作......

#2


1  

Your snippet is full of potential errors:

您的代码段中充满了潜在错误:

1) Not checking if query succeeded

1)不检查查询是否成功

$query_run = mysql_query($query)

You execute a query, but you never check if your query succeeded by verifying if $query_run is an actual resource and not FALSE.

您执行查询,但永远不会通过验证$ query_run是否是实际资源而不是FALSE来检查您的查询是否成功。

2) Validation of rows returned

2)验证返回的行

Your validation for the number of rows returned by the query is useless:

您对查询返回的行数的验证是无用的:

if (mysql_num_rows($query_run)==NULL) { 
    echo 'No results found.';
}

This is never true, as mysql_num_rows() returns an inte or FALSE, never NULL.

这是永远不会的,因为mysql_num_rows()返回一个inte或FALSE,永远不会为NULL。

3) Use of variable with potentially invalid value

3)使用具有潜在无效值的变量

Using

while ($query_row = mysql_fetch_assoc($query_run)) { ... }

is risky as you never check if $query_run is an actual resource, which is required by mysql_fetch_assoc().

因为你永远不会检查$ query_run是否是mysql_fetch_assoc()所需的实际资源,所以风险很大。

4) Misunderstanding of while loop

4)对while循环的误解

The following lines are probably wrong too:

以下几行也可能是错误的:

while ($query_row = mysql_fetch_assoc($query_run)) {
    $link = $query_row['Link'];
    // echo ''.$link.'<br>';

}
$html = file_get_html($link);

You iterate over all rows returned by the query. After the while loop exits, $link only contains the value of the last row as single variable cannot contain the values of multiple rows.

您迭代查询返回的所有行。 while循环退出后,$ link仅包含最后一行的值,因为单个变量不能包含多行的值。

Conclusion

I strongly recommend you improve your error checking and improve the overall quality of your code. Also consider using one of the newer extensions like mysqli or PDO, the mysql extension is deprecated.

我强烈建议您改进错误检查并提高代码的整体质量。另外考虑使用mysqli或PDO之类的新扩展,不推荐使用mysql扩展。

#3


0  

If you want to add all links to an array try this:

如果要将所有链接添加到数组,请尝试以下操作:

$link[] = $query_row['Link'];

Instead of:

$link = $query_row['Link'];

You were close but you weren't using square brackets you were using parentheses as shown here:

你很接近,但你没有使用方括号,你使用括号,如下所示:

$link = $query_row($link);

Also, try taking $query_run out of the if statement. It should look something like this:

另外,尝试从if语句中取出$ query_run。它应该看起来像这样:

$query = "SELECT `Link` FROM `Table1` WHERE `Value1` ='' AND `Source`='blah'";
$query_run = mysql_query($query);
if ($query_run) {
    echo 'Query Success!<br><br>';
    if (mysql_num_rows($query_run) == NULL) {
        echo 'No results found.';
    }
    while ($query_row = mysql_fetch_assoc($query_run)) {
        $link[] = $query_row['Link'];
        // echo ''.$link.'<br>';

    }
    $html = file_get_html($link);
    foreach ($html->find('div.article') as $e) {
        $result = $e->find('img', 0);
        $imgsrc = $result->src . '<br>';
        echo $imgsrc;
    }
}

#4


0  

You should revisit the PHP Language Reference.

您应该重新访问PHP语言参考。

The foreach loop syntax is

foreach循环语法是

foreach($array as $element)

or

foreach($array as $key=>$value)

But you seem to have other weak points that I fear are not in the scope of * to mend. For example your own code would work quite well by just moving a single } from line 11 down a few lines.

但是你似乎还有其他的弱点,我担心这些弱点不在*的修补范围内。例如,只需将第11行从第11行向下移动几行,您自己的代码就能很好地工作。

#1


1  

When you do this:

当你这样做:

$result = mysqli_query($dblink, $query);

The functions return a link identifier you store in $result. This identifier we need to pass to fetch functions in order to be able to show it from which result to fetch. It shouldn't be changed until you are done fetching all the results you want.

这些函数返回您存储在$ result中的链接标识符。我们需要传递给提取函数的这个标识符,以便能够从中获取结果。在获取所需的所有结果之前,不应更改它。

This goes right the first time:

这是第一次:

$row = mysqli_fetch_assoc($result)

But then, in the foreach, you overwrite that variable with other information:

但是,在foreach中,您用其他信息覆盖该变量:

$result = $e->find('img', 0);

As such, when the next iteration comes around, it is no longer a valid result identifier, so MySQL doesn't know what to do with it.

因此,当下一次迭代出现时,它不再是有效的结果标识符,因此MySQL不知道如何处理它。

The fix is actually rather simple, you need to change the name of the variable you are using in the foreach:

修复实际上非常简单,您需要更改foreach中使用的变量的名称:

$result = $e->find('img', 0);
$imgsrc = $result->src . '<br>';

Becomes:

$found= $e->find('img', 0);
$imgsrc = $found->src . '<br>';

And voila, it should work...

瞧,它应该工作......

#2


1  

Your snippet is full of potential errors:

您的代码段中充满了潜在错误:

1) Not checking if query succeeded

1)不检查查询是否成功

$query_run = mysql_query($query)

You execute a query, but you never check if your query succeeded by verifying if $query_run is an actual resource and not FALSE.

您执行查询,但永远不会通过验证$ query_run是否是实际资源而不是FALSE来检查您的查询是否成功。

2) Validation of rows returned

2)验证返回的行

Your validation for the number of rows returned by the query is useless:

您对查询返回的行数的验证是无用的:

if (mysql_num_rows($query_run)==NULL) { 
    echo 'No results found.';
}

This is never true, as mysql_num_rows() returns an inte or FALSE, never NULL.

这是永远不会的,因为mysql_num_rows()返回一个inte或FALSE,永远不会为NULL。

3) Use of variable with potentially invalid value

3)使用具有潜在无效值的变量

Using

while ($query_row = mysql_fetch_assoc($query_run)) { ... }

is risky as you never check if $query_run is an actual resource, which is required by mysql_fetch_assoc().

因为你永远不会检查$ query_run是否是mysql_fetch_assoc()所需的实际资源,所以风险很大。

4) Misunderstanding of while loop

4)对while循环的误解

The following lines are probably wrong too:

以下几行也可能是错误的:

while ($query_row = mysql_fetch_assoc($query_run)) {
    $link = $query_row['Link'];
    // echo ''.$link.'<br>';

}
$html = file_get_html($link);

You iterate over all rows returned by the query. After the while loop exits, $link only contains the value of the last row as single variable cannot contain the values of multiple rows.

您迭代查询返回的所有行。 while循环退出后,$ link仅包含最后一行的值,因为单个变量不能包含多行的值。

Conclusion

I strongly recommend you improve your error checking and improve the overall quality of your code. Also consider using one of the newer extensions like mysqli or PDO, the mysql extension is deprecated.

我强烈建议您改进错误检查并提高代码的整体质量。另外考虑使用mysqli或PDO之类的新扩展,不推荐使用mysql扩展。

#3


0  

If you want to add all links to an array try this:

如果要将所有链接添加到数组,请尝试以下操作:

$link[] = $query_row['Link'];

Instead of:

$link = $query_row['Link'];

You were close but you weren't using square brackets you were using parentheses as shown here:

你很接近,但你没有使用方括号,你使用括号,如下所示:

$link = $query_row($link);

Also, try taking $query_run out of the if statement. It should look something like this:

另外,尝试从if语句中取出$ query_run。它应该看起来像这样:

$query = "SELECT `Link` FROM `Table1` WHERE `Value1` ='' AND `Source`='blah'";
$query_run = mysql_query($query);
if ($query_run) {
    echo 'Query Success!<br><br>';
    if (mysql_num_rows($query_run) == NULL) {
        echo 'No results found.';
    }
    while ($query_row = mysql_fetch_assoc($query_run)) {
        $link[] = $query_row['Link'];
        // echo ''.$link.'<br>';

    }
    $html = file_get_html($link);
    foreach ($html->find('div.article') as $e) {
        $result = $e->find('img', 0);
        $imgsrc = $result->src . '<br>';
        echo $imgsrc;
    }
}

#4


0  

You should revisit the PHP Language Reference.

您应该重新访问PHP语言参考。

The foreach loop syntax is

foreach循环语法是

foreach($array as $element)

or

foreach($array as $key=>$value)

But you seem to have other weak points that I fear are not in the scope of * to mend. For example your own code would work quite well by just moving a single } from line 11 down a few lines.

但是你似乎还有其他的弱点,我担心这些弱点不在*的修补范围内。例如,只需将第11行从第11行向下移动几行,您自己的代码就能很好地工作。