dpgjci27392 2018-05-02 18:47
浏览 78

使用具有多个查询的函数将mysql转换为mysqli的问题

I have 2 php files. Additional some php code included in Joomla directly in an article with Sourcerer.

Something seems to be missing here.

The first error I get is:

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

the warning shows to the line in db-queries.php where it says

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

The second error is:

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

It shows to the script in the Joomla article where I want to check the num_rows. That's pretty clear, because there is no result. So I mainly need to fix error #1...

So somehow the script didn't know that $db is. I'm stuck...can anybody help or give a small hint?

1: db-connect.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 included code in Joomla article

<?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  } ?>
  • 写回答

1条回答 默认 最新

  • douji5746 2018-05-02 19:04
    关注

    i suggest this:

    db-connect.php

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

    on db-queries

    if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) {
        /* do my code */
        printf("Numbers %d rows.
    ", 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
    ", $mysqli->error);
        }
        $result->close();
    }
    

    i suggest read doc Official Php Mysqli

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题