使用mysqli和fetch_object进行嵌套循环 [英] Nested looping with mysqli and fetch_object
问题描述
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$''''''' ,pword)或死(无法连接到databse!);
mysqli_select_db($ mysqli,db_name)或者死(Could not Select Database!);
$ result = $ mysqli-> query('SELECT * FROM table_1');
$ title = $ mysqli-> query('SELECT title FROM table_2');
$ b $ if($ result& $ title){
while($ t_row = $ title-> fetch_object()){
$ b $ ($ row = $ result-> fetch_object()){
$ s_title = strtolower($ t_row-> title);
$ m_title = strtolower($ row-> description);
$ b $ if($ s_title == $ m_title){
echoMatched:$ s_title< strong> with< / strong> $ m_title< br />;
break;
}
}
}
}
你应该在var中存储第二个循环的结果。
if($ result&& $ title)在第一次查看第二个游标之后, ){
$ results_rows = array();
while($ row = $ result-> fetch_object()){
$ results_rows [] = strtolower($ row-> description);
$ b while($ t_row = $ title-> fetch_object()){
$ s_title = strtolower($ t_row-> title);
if(in_array($ s_title,$ results_rows)){
echoMatched:$ s_title。 PHP_EOL;
}
}
}
I'm attempting to do a comparision between two tables. If I find a match, I need to copy data from one table and insert it into the other. I'm able to pull the data from the tables just fine, but I'm having issues with a nested loop I created. It only loops through and finds one result. When looking through the tables, there are 1000s of matches, so I assume I'm doing something wrong. My only guess is the use of fetch_object and how I'm using it. I attempted using fetch_assoc instead, but I get the same result:
$mysqli = mysqli_connect("127.0.0.1", "uname", "pword") or die("Can't connect to databse!");
mysqli_select_db($mysqli, "db_name") or die("Couldn't Select Database!");
$result = $mysqli->query('SELECT * FROM table_1');
$title = $mysqli->query('SELECT title FROM table_2');
if($result && $title){
while($t_row = $title->fetch_object()){
while($row = $result->fetch_object()){
$s_title = strtolower($t_row->title);
$m_title = strtolower($row->description);
if($s_title == $m_title) {
echo "Matched: $s_title <strong>with</strong> $m_title<br />";
break;
}
}
}
}
You should store results of the second loop in var. Because after the first looking your second cursor is at the end of the result.
if($result && $title){
$resulted_rows = array();
while($row = $result->fetch_object()){
$resulted_rows[] = strtolower($row->description);
}
while($t_row = $title->fetch_object()){
$s_title = strtolower($t_row->title);
if(in_array($s_title, $resulted_rows)) {
echo "Matched: $s_title" . PHP_EOL;
}
}
}
这篇关于使用mysqli和fetch_object进行嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!