首页 > 解决方案 > 使用具有多个查询的函数将 mysql 转换为 mysqli 的问题

问题描述

我有 2 个 php 文件。在 Sourcerer 的一篇文章中直接添加了 Joomla 中包含的一些 php 代码。

这里似乎缺少了一些东西。

我得到的第一个错误是:

Warning: mysqli_query() expects parameter 1 to be mysqli, null given 

警告显示在 db-queries.php 中的行中

return $result = mysqli_query($db, $q);"

第二个错误是:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given.

它显示在 Joomla 文章中我要检查 num_rows 的脚本中。这很清楚,因为没有结果。所以我主要需要修复错误#1 ...

所以不知何故,脚本不知道 $db 是。我被卡住了……有人可以帮忙或给个小提示吗?

1:数据库连接.php

$sqlhost = "xxx";
$sqluser = "xxx";
$sqlpassword = "xxx";
$sqldb = "xxx";

$db = mysqli_connect ($sqlhost, $sqluser, $sqlpasswort, $sqldb);
mysqli_set_charset($db,"utf8");

2 db-queries.php

<?php include ("db-connect.php");
function squadresults ($squad_nr) {
global $db;
$q = "
    SELECT spieler_id, spiel1, spiel2, spiel3, spiel4, spiel5, spiel6, netto, hdc, gesamt_hdc, qualifiziert
    FROM onyc_ergebnisse
    WHERE squad_nr='".$squad_nr."'
    ORDER BY gesamt_hdc DESC, hdc ASC";
return $result = mysqli_query($db, $q);
}

3 Joomla 文章中包含的代码

<?php
    require_once JPATH_SITE.'/_skripte_/db-queries.php';
    $q = squadresults ('despo_2018'); // the function squadresults in db-queries.php is called
    var_dump ($q); // just to check if there are any results --> NULL
    if (mysqli_num_rows($q)==0) {echo "<p class='info'><br/>This squad hasn't been played yet.</p>";}
    else {?>
<table class="table-striped">
<tr class="header">
    <td class="header">Place</td>
    <td class="header">Player</td>
    <td class="header">Result</td>
    <td class="header">HDC</td>
    <td class="header">Total</td>
    <td class="header">qual</td>
</tr>

<?php $platz = 1; while ($row = mysqli_fetch_assoc($q)) {
$spieler_id = $row['spieler_id'];
$spieler = mysqli_fetch_row (spielernamen_ergebnisse($spieler_id));
var_dump ($spieler);
echo "<tr>
<td class='center'>".$platz.".</td>
<td class='spieler'>".$spieler[0]." ".$spieler[1]."</td>
<td class='center'>".$row['spiel1']."</td>
<td class='center'>".$row['hdc']."</td>
<td class='center'>".$row['gesamt_hdc']."</td>
<td class='center'>".$row['qualifiziert']."</td>
</tr>";
$platz++;
}?>
</table>
<?php  } ?>

标签: phpmysqli

解决方案


我建议这样做:

db-connect.php

$mysqli = new mysqli("localhost", "user", "pass", "db_test");

/* check the con */
if (mysqli_connect_errno()) {
    printf("Error: %s\n", mysqli_connect_error());
    exit();
}
/* set utf-8 */
if (!$mysqli->set_charset("utf8")) {
    printf("Error on set utf8: %s\n", $mysqli->error);
    exit();
} 

关于数据库查询

if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) {
    /* do my code */
    printf("Numbers %d rows.\n", mysqli_num_rows($result));
}
/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = $mysqli->query("SELECT * FROM City", MYSQLI_USE_RESULT)) {

    /* Note, that we can't execute any functions which interact with the
       server until result set was closed. All calls will return an
       'out of sync' error */
    if (!$mysqli->query("SET @a:='this will not work'")) {
        printf("Error: %s\n", $mysqli->error);
    }
    $result->close();
}

我建议阅读doc Official Php Mysqli


推荐阅读