Mencantumkan kode program di posting Anda, tolong ikuti aturan yang sesuai, baca http://diskusiweb.com/discussion/39204/aturan-cara-menyisipkan-kode-program-di-diskusiweb
Tolong pilih kategori sesuai, jenis posting (pertanyaan atau bukan) dan sertakan tag/topik yang sesuai misal komputer, php, mysql, dll. Promosi atau posting tidak pada tempatnya akan kami hapus.
OOP PHP->Mysql Connection
  • saefullohsaefulloh
    Posts: 904
    <?
    class koneksi{
    public $server='localost';
    public $user='root';
    public $password = '';
    public $db="";
    public function conek($server2,$user2,$password2){
    $this->server=$server2;
    $this->user=$user2;
    $this->password=$password2;
    mysql_connect($this->server,$this->user,$this->password);
    }
    public function select_db($db2){
    $this->db=$db2;
    mysql_select_db($this->db);
    }
    public function select($table){
    $sql=mysql_query("select * from $table");
    $row=mysql_fetch_array($sql);
    return $row;
    }
    }
     
    ?>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
     
    <body>
    <?
    $konek = new koneksi();
    $konek->conek('localhost','root','passwordnya');
    $konek->select_db('personal');
     
    $nama_url=$konek->select('link');
    foreach($nama_url as $url){
    echo $url."<br>";
    }
    ?>
     
    </body>
    </html>



    image

    hasil :
    1
    1
    Datatech Solution
    Datatech Solution
    http://www.datatech-solution.com
    http://www.datatech-solution.com
    _blank
    _blank
    Website Datatech Solution
    Website Datatech Solution
    1
    1


    masih belumsempurna..
    saya pengine tuh hasilnya gini :
    http://www.datatech-solution.com
    Link url 2
    Link url 3
    Link url 4
    Link url 5
    dst...
  • pandapanda
    Posts: 134
    conek ... itu bahasa apa :D

    ya kagak bisa to pul kalo mau elo seluruh record yg di return kalo fungsi elo kaya gini :
    public function select($table){
        $sql=mysql_query("select * from $table");
        $row=mysql_fetch_array($sql);
        return $row;
    }

    itu bakal cuma 1 row pertama yg di return
    kalo mau, tampung di array, pake while baca seluruh record yg di dapat
    lalu yg di return array nya, bukan cuma 1 row

    di pemanggilnya sudah bener pake foreach
  • saefullohsaefulloh
    Posts: 904
    lo pake while atau foreach...
    atau diulang pake while baru nampili pake foreach mas..

    haha.. bbuat sendiri  jadi ya mau pake bahasa apa mah yg penting bisa diingat mas. haha...
  • saefullohsaefulloh
    Posts: 904
    "itu bakal cuma 1 row pertama yg di return
    kalo mau, tampung di array, pake while baca seluruh record yg di dapat
    lalu yg di return array nya, bukan cuma 1 row"

    mas aku masih kurang paham ni soal itu.. dijelasin lagi ya.. apa  kasih contoh deh... :-) haha..
  • pandapanda
    Posts: 134
    :P

    ndak usah ngomong class dolo

    elo kalo bikin script mau tarik seluruh row yg di SELECT utk ditampilkan, kaya apa ?
  • saefullohsaefulloh
    Posts: 904
    itu diatas mas... ada
    haha,... kalo ga pake  class bisa cuma lagi noba belajar oop.

    hasilnya kaya yang di image mas... :-)
  • pandapanda
    Posts: 134
    mana ada ?
    mengambil seluruh row dari database yg di SELECT

    yg ada cuma :
    - ambil 1 row dari database yg di SELECT <-- ini SALAH !!!
    - menampilkan return fungsi yg mengembalikan 1 row pake foreach

    ndak usah ngomong class dolo deh, bikin fungsi nya aja masih blom bener
  • saefullohsaefulloh
    Posts: 904
    haha.. makanya lagi belajar mas panda............
    diajarin ya aku mesti gmna.. ? hmm :-(
  • newusernewuser
    Posts: 2,998
    http://id.php.net/mysql_fetch_array

    bener gak usah mikirin class nya dulu.

    fungsi:
    public function select($table){
        $sql=mysql_query("select * from $table");
        $row=mysql_fetch_array($sql);
        return $row;
    }

    akan menghasilkan 1 row dari index pertama dari return array mysql_fetch_array.
    solusi nya buat 1 variable array aja lagi untuk menampung hasil fetch yg dilooping.
  • saefullohsaefulloh
    Posts: 904
    @newuser : mas gmna ?
  • saefullohsaefulloh
    Posts: 904
    caranya gmna ya ??
    buat 1 variable 1 lagi, aku kurang paham we.
    :-(

    duh.. masih tak coba2 tapi belum jadi.
  • newusernewuser
    Posts: 2,998
    @saefulloh  apanya yg gimana? :-/
  • saefullohsaefulloh
    Posts: 904
    itu katane buat 1 variable lagi.. nah aku  ga paham kali ini deh tolong contohin mas.. :-) hehe... tolong lah...
  • saefullohsaefulloh
    Posts: 904
    itu katane buat 1 variable lagi.. nah aku  ga paham kali ini deh tolong contohin mas.. :-) hehe... tolong lah...
  • newusernewuser
    Posts: 2,998
    lah biasanya gimana kalo nampung variable ke array? :D

    $data = array();
    while ($row = mysql_fetch_array($sql))  {
         $data[] = $row;
    }

    return $data;
  • saefullohsaefulloh
    Posts: 904
    oh ya ya.. haha...
    aku si gtu tau cuma kalo mau dibuat fungsi gmna.. yg di return mana..
    sae aku dari dulu ga pake fungsi apalagi class...
    ni baru lagi belajar mas..

    tak coba..  kalo ga jadiajarin lagi ya. :-)
  • saefullohsaefulloh
    Posts: 904
    ok.. jadi mas. haha,,,,, makasih deh.. hebatlah. :-)

Selamat datang,

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Tagged

Top Posters