简体   繁体   English

在“ for”循环的每次迭代中,如何增加mysql中的行?

[英]How do I increment row in mysql at each iteration of a“for” loop?

I want my little script to take a link that i have already inserted into the database, get therelevent text from that webpage and insert it into a different column on that same row. 我希望我的小脚本获得一个已插入数据库的链接,从该网页获取相关事件文本,然后将其插入同一行的另一列中。 Then do it again on the next row. 然后在下一行再次进行。 I am getting this "Object of class mysqli could not be converted to string" error but Im not sure how to represent the row number correctly. 我收到此“无法将mysqli类的对象转换为字符串”错误,但是我不确定如何正确表示行号。 Heres what i have so far. 这是我到目前为止所拥有的。

UPDATE Ok guys I appreciate your help. 更新好的,谢谢您的帮助。 I think I unclear about this. 我想我对此还不清楚。 The error message says it on the line that says $query1 = "$conn, SELECT adlink, key FROM usedcars WHERE key = $x"; 错误消息在表示$query1 = "$conn, SELECT adlink, key FROM usedcars WHERE key = $x";的行上说$query1 = "$conn, SELECT adlink, key FROM usedcars WHERE key = $x"; Or at least, thats what its saying right now. 至少,这就是现在的说法。 I have tried all the solutions suggested, and so far I just get a bunch of other errors. 我已经尝试了所有建议的解决方案,到目前为止,我还遇到了许多其他错误。

for ($y=1;$y=1201;$y++)
    {
    $x = 1;

    $query1 = "$conn, SELECT adlink, key  FROM usedcars WHERE key = $x";

    $query2 = "$conn, INSERT INTO usedcars (adtext), VALUES ($final_text) WHERE key = $x";

    $link_result = mysqli_query($query1);

    $text_holder = file_get_contents($link_result);
    $final_text = parse_array($text_holder, "postingBody", "<!-- .posting -->");

    mysqli_query($query2);
    echo "<font size='18' color='#FFFF00'>Placing text from $link_result into database</font><br>"; 
    $x++;
    }

I know its probably something simple...Im just not as smart as you guys. 我知道这可能很简单...我只是不像你们那么聪明。

first check whether all fields with key = $x exists 首先检查key = $x所有字段是否存在

use update instead of insert 使用update而不是insert

WHERE is used to filter existing results as said by @Eliel not to add a new one... 如@Eliel所说, WHERE用于过滤现有结果,而不添加新的结果...

$query1 = "$conn, SELECT adlink, key  FROM usedcars WHERE key = $x";

$query2 = "$conn, UPDATE usedcars (adtext), VALUES ($final_text) WHERE key = $x";

$link_result = mysqli_query($query1);

if want to add a new one use: 如果要添加一个新的用途:

$query1 = "$conn, SELECT adlink, key  FROM usedcars WHERE key = $x";

$query2 = "$conn, INSERT INTO usedcars (adtext,***,***) VALUES ($final_text,***,***)"; // provide all necessary fields

$link_result = mysqli_query($query1);

When you are using $query1 = "$conn, SELECT adlink, key FROM usedcars WHERE key = $x"; 当您使用$query1 = "$conn, SELECT adlink, key FROM usedcars WHERE key = $x"; and pass it to mysqli_query() , the $conn is assumed as a string and it tries to use $conn as string.Thats why the error is coming. 并将其传递给mysqli_query() ,将$conn假定为字符串,并尝试将$conn用作字符串。这就是为什么会出现错误。

try this - 尝试这个 -

$query1 = "SELECT adlink, key  FROM usedcars WHERE key = $x";

$query2 = "UPDATE usedcars (adtext), VALUES ($final_text) WHERE key = $x";

$link_result = mysqli_query($conn, $query1);

this is your query 这是你的查询

for ($y=1;$y=1201;$y++)
{
$x = 1;

$query1 = "$conn, SELECT adlink, key  FROM usedcars WHERE key = $x";

$query2 = "$conn, INSERT INTO usedcars (adtext), VALUES ($final_text) WHERE key = $x";

$link_result = mysqli_query($query1);

$text_holder = file_get_contents($link_result);
$final_text = parse_array($text_holder, "postingBody", "<!-- .posting -->");

mysqli_query($query2);
echo "<font size='18' color='#FFFF00'>Placing text from $link_result into database</font><br>"; 
$x++;
}

My suggested query 我建议的查询

 $x = 1;//put it before loop, every loop this $x will become one and the incrementation doesn't take effect
for ($y=1;$y=1201;$y++)
{


$query1 = "$conn, SELECT adlink, key  FROM usedcars WHERE key = $x";

$query2 = "$conn, INSERT INTO usedcars (adtext), VALUES ($final_text) ";//remove your where

$link_result = mysqli_query($query1);

$text_holder = file_get_contents($link_result);
$final_text = parse_array($text_holder, "postingBody", "<!-- .posting -->");

mysqli_query($query2);
echo "<font size='18' color='#FFFF00'>Placing text from $link_result into database</font><br>"; 
$x++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM