friendship

friendship

Saturday, February 11, 2012

koneksi php dan include!

<?php
include "functioncetak.php";
$check = mysql_connect("localhost","root","")
or die("koneksi gagal ");

if($check == true)
{
    print "<p>Welcome to the Jungle</p> ";
    $kondisi = mysql_select_db("latihan");
   
    if($kondisi == true)
    {
        print "<p>DataBase Terhubung</p>";
    }
   
}
?>
==========

<?php
function tambah($angka1, $angka2)
{
    return $angka1 + $angka2;
}

print tambah(12, 23)."<br/>";
print tambah(10, 25)."<br/>";
print tambah(20, 15)."<br/>";

function showMessage()
{
    print "<hr><marquee>pak angkik kayak entuD</marquee></hr>";
}

showMessage();

?>
=====================

<?php
function tambah($angka1, $angka2)
{
    return $angka1 + $angka2;
}

print tambah(12, 23)." ";
print tambah(10, 25)." ";
print tambah(20, 15)." ";
?>
============

contoh connectsi gagal

<?php
$check = mysql_connect("localhost","root1","")
or die("koneksi gagal ");

if($check == true)
{
    print "<p>Welcome to the Jungle</p> ";
    $kondisi = mysql_select_db("latihan");
   
    if($kondisi == true)
    {
        print "<p>DataBase Terhubung</p>";
    }
   
}
?>

=====================

edit dan tambah

<html>
<head><title> form tambahan </title> </head>
<body bgcolor='linen'>
<?php
include "connect.php";
$sql= "select * from mahasiswa
    where nim ='".$_GET['nim']." ' ";
$executeQuery = mysql_query($sql);

$nim = "";
$nama = "";
$alamat = "";
while($data=mysql_fetch_array($executeQuery))
{
    $nim = $data['nim'];
    $nama = $data['nama'];
    $alamat = $data['alamat'];
}
mysql_free_result($executeQuery);
?>

<font color='blue'><h2><center> FORM EDIT MAHASISWA</center></font>
<hr>
<form action ='' method=POST>
<p>NIM: <br><input type ='text' name ='nim_edit' value='<?php print $nim; ?>' /> </br> </p>
<p>Nama: <br><input type ='text' name ='nama_edit' value='<?php print $nama; ?>' /> </br> </p>
<p>Alamat: <br><input type ='text' name ='alamat_edit' value='<?php print $alamat; ?>' /> </br> </p>
<input type = 'submit' nama='simpan' value= 'simpan' />
</form>
</hr>
</html>


<?php
if($_POST)
{
    $sql = "update mahasiswa set nama= '".$_POST['nama_edit']. "',
        alamat= '".$_POST['alamat_edit']. "'
        where nim= '".$_POST['nim_edit']. "'";

    $edit = mysql_query($sql);
    if($edit)
    {
        print "<html><body onload=\"self.location.href='form.php' \">
            </body></html>";
    }
    else
    {
        print mysql_error();
    }
}
?>

edit dan hapus

<?php
include "connect.php";
?>
<p>
<table border=1>
<tr>
    <td>Nim</td>
    <td>Nama</td>
    <td>Alamat</td>
    <td>Edit</td>
    <td>Hapus</td>
</tr>

<?php
$sql = "select * from mahasiswa group by nim asc";
$executequery = mysql_query($sql);
$data= array();
while($data=mysql_fetch_array($executequery))
{
    print "<tr>";
    print "<td>".$data['nim']."</br>";
    print "<td width = 35%>".$data['nama']."</br>";
    print "<td width = 50%>".$data['alamat']."</br>";
    print "<td width = 5%>
        <a href= 'edit_mahasiswa.php?nim=".$data['nim']."'>edit</a></td>";
    print "<td width = 5%>
        <a href= 'hapus_mahasiswa.php?nim=".$data['nim']." '>hapus</a></td>";
    print "</tr>";
   


}

?>
</table>