准备不同的mysqli声明

时间:2017-06-27 21:44:18

标签: php mysqli

当我需要准备多个陈述时,我迷路了。我在准备好的陈述和连接方面遇到了问题..

我有一个函数,我传递给数据库的连接($ 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;
}

1 个答案:

答案 0 :(得分:0)

这会导致错误:

$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()是处理错误的不好方法。