簡體   English   中英

在oop php中循環查詢

[英]loop through query in oop php

所以在我的項目中,我需要從給出2行的查詢中檢索一些數據(我在admin的php中測試了查詢)

但是我正在使用面向對象編程,我的方法如下所示:

function __construct($pid){

    Sys::database_connect();

    $prod_data = mysql_query("SELECT Produto.produto_id, Produto.nome, Produto.descricao, Produto.pvp, Produto.iva_id, Produto.fornecedor_id, Produto.views, Categoria.nome AS categoria, Imagem.file FROM Produto, Categoria, Imagem WHERE Produto.produto_id = '$pid' AND Produto.categoria_id=Categoria.categoria_id AND Imagem.Produto_produto_id = Produto.produto_id ORDER by Produto.produto_id") or die(mysql_error());

    $array_data = mysql_fetch_array($prod_data);

    $this->produto_id = $array_data['produto_id'];
    $this->nome = $array_data['nome'];
    $this->descricao = $array_data['descricao'];
    $this->pvp = $array_data['pvp'];
    $this->categoria = $array_data['categoria'];

            //this is the picture name (the first one will be the main pic of the product)
    $this->file = $array_data['file'];

    $pre_img_galery = array();
    $max_pics=count($array_data);
    for ($pci_for=0; $pci_for < $max_pics; $pci_for++) { 
        //array_push($pre_img_galery, $array_data['file'][$pci_for]);
        echo $this->$array_data['file'].'<br>';
    }
    $this->img_galery = $pre_img_galery;

}

該數據庫有2張圖片,結果導致2行在圖片字段中具有不同的值...

我的for循環只是回顯第一個文件名字符的字符...

如何將圖片文件名定義為數組(img_galery)?

我的解決方法:

function __construct($pid){

    Sys::database_connect();

    $prod_data = mysql_query("SELECT Produto.produto_id, Produto.nome, Produto.descricao, Produto.pvp, Produto.iva_id, Produto.fornecedor_id, Produto.views, Categoria.nome AS categoria, Imagem.file FROM Produto, Categoria, Imagem WHERE Produto.produto_id = '$pid' AND Produto.categoria_id=Categoria.categoria_id AND Imagem.Produto_produto_id = Produto.produto_id ORDER by Produto.produto_id") or die(mysql_error());

    /*while($row = mysql_fetch_array($prod_data)) {
        echo $prod_data['file'].'<br>';
    }*/

    while ($row =  mysql_fetch_array($prod_data)){

    echo $this->produto_id = $row['produto_id'];
    echo $this->nome = $row['nome'];
    echo $this->descricao = $row['descricao'];
    echo $this->pvp = $row['pvp'];
    echo $this->categoria = $row['categoria'];
    //echo $this->online = $array_data['online'];
    echo $this->file = $row['file'];

    $pre_img_galery = array();

    array_push($this->img_galery, $row['file']); 
    echo '<br><br><br>';
    print_r($this->img_galery);
    }

}

這是需要做的,您需要遍歷結果集,

 $prod_data = mysql_query("SELECT Produto.produto_id, Produto.nome, Produto.descricao, Produto.pvp, Produto.iva_id, Produto.fornecedor_id, Produto.views, Categoria.nome AS categoria, Imagem.file FROM Produto, Categoria, Imagem WHERE Produto.produto_id = '$pid' AND Produto.categoria_id=Categoria.categoria_id AND Imagem.Produto_produto_id = Produto.produto_id ORDER by Produto.produto_id") or die(mysql_error());

while($row =  mysql_fetch_array($prod_data) { 
  $pre_img_galery[]['produto_id'] = $row['produto_id'];
   ... //and so on for other indexes per row.
} 

注意:不建議使用Mysql_ *擴展名。 最好使用Mysqli_ *或PDO擴展名。 使用PHP時,請參見MySQL與MySQLi的問題,並在此處找到Mysqli_ *的安裝說明。

暫無
暫無

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

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