Попытка конвертировать в MySQLi. Я получил большую помощь в последние несколько дней, спасибо. Моя следующая проблема - как конвертировать, когда используется LEFT JOIN.
Рабочий код MYSQL
$q1 = 'SELECT * FROM earth LEFT JOIN cities ON (cities.countryid4city = earth.countryid) GROUP BY countryid ORDER by countryid';
$q2 = 'SELECT * FROM cities ORDER BY cityname';
$mytable1 = mysql_query($q1);
$mytable2 = mysql_query($q2);
echo mysql_error();
mysql_close();
$numrows_table1 = mysql_numrows($mytable1);
$numrows_table2 = mysql_numrows($mytable2);
$i = 0;
while ($i < $numrows_table1){
$countryid = mysql_result($mytable1,$i,'countryid');
$countryname = mysql_result($mytable1,$i,'countryname');
print "<br><br>Country is " . $countryname . ", and these cities are in it";
$j = 0;
while ($j < $numrows_table2){
$countryid4city = mysql_result($mytable2,$j,'countryid4city');
$cityname = mysql_result($mytable2,$j,'cityname');
if ($countryid4city == $countryid){
print "<br><br>" . $cityname;
}
$j++;
}
$i++;
}
Выход соответствует ожидаемому. (Обратите внимание, что этот сайт удаляет разрывы строк в этом, но они есть).
Country is USA, and these cities are in it
New York
San Francisco
Country is England, and these cities are in it
Chelsea
Clapham
London
Country is Sweden, and these cities are in it
Lidingö
Stockholm
Сломанное преобразование MYSQLI (следуя той же логике, как я думал)
$q1 = 'SELECT * FROM earth LEFT JOIN cities ON (cities.countryid4city = earth.countryid) GROUP BY countryid ORDER by countryid';
$q2 = 'SELECT * FROM cities ORDER BY cityname';
$mytable1 = mysqli_query($conned, $q1);
$mytable2 = mysqli_query($conned, $q2);
mysqli_close($conned);
while($row1 = mysqli_fetch_assoc($mytable1)){
$countryid = $row1['countryid'];
$countryname = $row1['countryname'];
print "<br><br>Country is " . $countryname . ", and these cities are in it";
while($row2 = mysqli_fetch_assoc($mytable2)){
$countryid4city = $row2['countryid4city'];
$cityname = $row2['cityname'];
if ($countryid4city == $countryid){
print "<br><br>" . $cityname;
}
}
}
Выход
Country is USA, and these cities are in it
New York
San Francisco
Country is England, and these cities are in it
Country is Sweden, and these cities are in it
Это только сбор значений LEFT JOIN из второй таблицы для первого значения первой таблицы. Что мне не хватает? Я полагаю, у меня, возможно, не было идеального решения для рабочей версии MYSQL.
Любая помощь с благодарностью. Спасибо.