簡體   English   中英

准備不同的mysqli語句

[英]prepare different mysqli statement

當我需要准備多條陳述時我迷路了。 我在准備語句和連接時遇到問題。

我有一個函數,並將與數據庫的連接($ con變量)傳遞給它。 在此函數中,我准備了更多語句,但出現錯誤,服務器回復:

mysql_error() expects parameter 1 to be resource, object given in select.php on line 1086

這是我調用函數並傳遞連接變量的頁面:

include('include/connect.php');

echo show_volanti($con);

這是我上面包含的連接文件:

$con = mysqli_connect($host,$user,$password,$db);

if (!$con) {
    die('Connection Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error()); }


if(!mysqli_set_charset($con, "utf8mb4")) {
    printf("Error loading character set utf8: %s\n", mysqli_error($con));
    exit(); }

這是函數:

function show_volanti($data){

    $con = $data;                                                   // PASSO CONNESSIONE
    $con2 = '';                                                     // SECONDA CONNESSIONE
    $id = 1;                                                        // 1 VOLANTE

    $visibile = 1;                                                  // VARIABILE DI VISIBILITA'

    $rows1 = array();                                               // PREPARO ARRAY 1 PER ID ARTICOLI VOLANTE
    $rows2 = array();                                               // PREPARO ARRAY 2 PER ARTICOLI VOLANTE
    $rows3 = array();                                               // PREPARO ARRAY PER GALLERIA IMMAGINI ARTICOLI

    $stmt1 = '';                                                    // PREPARO GLI STATEMENT
    $stmt2 = '';                                                    // PREPARO GLI STATEMENT
    $stmt3 = '';                                                    // PREPARO GLI STATEMENT

    $g = '';                                                        // RIFERIMENTO ASSOCIAZIONE GALLERY VIEWER

    $id_articoli = array();                                         // IMMAGAZZINO ID ARTICOLI DA RECUPERARE NELLA GALLERIA


    $con2 = mysqli_stmt_init($con);                                 // INIZIALIZZO LA CONNESSIONE

    $stmt1 = mysqli_stmt_prepare($con2,'SELECT
                                        articoli.id
                                        FROM articoli
                                        WHERE articoli.genere1 = ?
                                        AND articoli.visibile = ?')
                                        or die(mysql_error($con2));// QUERY INSERIMENTO DATI

    mysqli_stmt_bind_param($stmt1,'ii',$id,$visibile);                          // LEGO I PARAMETRI

    mysqli_stmt_execute($stmt1);                                                // ESEGUO LA QUERY

    mysqli_stmt_bind_result($stmt1,$rows1['id']);

    while(mysqli_stmt_fetch($stmt1)){
        $id_articoli = $rows1['id'];
    }

    // CREO RIFERIMENTO PER GALLERIA NEL VIEWER

    $g .= "g";
    $g .= $id_articoli;

    $stmt2 = mysqli_stmt_prepare($con2,'SELECT
                                        articoli.id,
                                        articoli.titolo,
                                        articoli.descrizione
                                        FROM articoli
                                        WHERE articoli.genere1 = ?
                                        AND articoli.visibile = ? ')
                                        or die(mysqli_error($con2));                // QUERY INSERIMENTO DATI

    mysqli_stmt_bind_param($stmt2,'ii',$id,$visibile);                          // LEGO I PARAMETRI

    mysqli_stmt_execute($stmt2);                                                // ESEGUO LA QUERY

    mysqli_stmt_bind_result($stmt2,$rows2['id'],$rows2['titolo'],$rows2['descrizione']);                                        


    // PREPARO QUERY PER LA GALLERIA

    $stmt3 = mysqli_stmt_prepare($con2,'SELECT
                                        galleria.id,
                                        galleria.foto
                                        FROM galleria
                                        WHERE galleria.rif_id = ?
                                        AND articoli.visibile = ? ')
                                        or die(mysqli_error($con2));                        // QUERY INSERIMENTO DATI

    mysqli_stmt_bind_param($stmt3,'ii',$id_articoli,$visibile);                         // LEGO I PARAMETRI

    mysqli_stmt_execute($stmt3);                                                        // ESEGUO LA QUERY

    mysqli_stmt_bind_result($stmt3,$rows3['id'],$rows3['foto']);            

    $html = "";
    $html .= "<div class='container'>";
    $html .= "  <div class='row'>";
    $html .= "    <div class='col-sm-12'>";
    $html .= "      <div class='panel panel-default'>";
    $html .= "        <div class='panel-body'>";

    while (mysqli_stmt_fetch($stmt2)){
        $html .= "            <div class='col'>";
        $html .= "              <div class='panel panel-default'>";
        $html .= "                <div class='panel-heading'>$rows2[titolo]</div>";
        $html .= "                  <div class='panel-body'>";
        $html .= "                      <div class='row'>";
        $html .= "                          <div class='class_p'>$rows2[descrizione]</div>";
        $html .= "                      <div> <!-- end first row -->";

        $html .= "                      <div class='class_container clearfix'>";

        while(mysqli_stmt_fetch($stmt3)){
            $html .= "                              <div class='thumbnail col-sm-2'>";
            $html .= "                                  <div class='class_img'>";
            $html .= "                                      <a href='$rows3[foto]' data-toggle='lightbox' data-gallery='$g' >";
            $html .= "                                          <img src='$rows3[foto]' class='img-responsive' class='img-fluid'>";
            $html .= "                                      </a>";
            $html .= "                                  </div> <!-- end class_img -->";
            $html .= "                              </div> <!-- end thumbnail col-sm-2- -->";   
        }

        $html .= "                      </div> <!-- end class_container -->";

        $html .= "                </div> <!-- end panel body -->";
        $html .= "              </div> <!-- end panel panel-default -->";
        $html .= "            </div> <!-- end col -->";
    }

    $html .= "        </div> <!-- end panel-body -->";
    $html .= "      </div> <!-- end panel panel-default -->";
    $html .= "    </div> <!-- end col-sm-12 -->";

    $html .= "  </div> <!-- end row -->";

    mysqli_close($con2);                                                // CHIUDO CONNESSIONE   


    return $html;
}

這導致錯誤:

$stmt1 = mysqli_stmt_prepare($con2,'SELECT
articoli.id,
FROM articoli
WHERE articoli.genere1 = ?
AND articoli.visibile = ?')
or die(mysql_error($con2));// QUERY INSERIMENTO DATI

mysql_error更改為mysqli_error ,您的問題應已解決。

注意:使用or die()是處理錯誤的不好方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM