Merubah dari pdo ke mysqli

gan saya dapet program dari temen saya tapi saya gak bisa kembangin karna pake pdo sedangkan saya terbiasa pake mysql atau mysqli. tolong bantuannya untuk ubah ke mysqli dong saya gak begitu paham dengan pdo connect.php

<?php
$db_host		= 'localhost';
$db_user		= 'root';
$db_pass		= '';
$db_database	= 'sales';

$db = new PDO('mysql:host='.$db_host.';dbname='.$db_database, $db_user, $db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>

index.php

<?php
	//Start session
	session_start();

	//Unset the variables stored in session
	unset($_SESSION['SESS_MEMBER_ID']);
	unset($_SESSION['SESS_FIRST_NAME']);
	unset($_SESSION['SESS_LAST_NAME']);
?>
<html>
<head>
<title>
POS
</title>
    <link rel="shortcut icon" href="main/images/pos.jpg">

  <link href="main/css/bootstrap.css" rel="stylesheet">

    <link rel="stylesheet" type="text/css" href="main/css/DT_bootstrap.css">

  <link rel="stylesheet" href="main/css/font-awesome.min.css">
    <style type="text/css">
      body {
        padding-top: 60px;
        padding-bottom: 40px;
      }
      .sidebar-nav {
        padding: 9px 0;
      }
    </style>
    <link href="main/css/bootstrap-responsive.css" rel="stylesheet">

<link href="style.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
    <div class="container-fluid">
      <div class="row-fluid">
		<div class="span4">
		</div>

</div>
<div>
<?php
if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
	foreach($_SESSION['ERRMSG_ARR'] as $msg) {
		echo '<div style="color: red; text-align: center;">',$msg,'</div><br>';
	}
	unset($_SESSION['ERRMSG_ARR']);
}
?>
<form action="login.php" method="post">

			<h1 style="font-family: calibri;"><center><span class="glyphicon glyphicon-qrcode"></span>Point Of Sales</center></h1>
		<br>


<div class="qwe">
<input style="height:40px; width: 245px;" type="text" name="username" Placeholder="Username" required/><br>
</div>
<div class="qwe">
<input type="password" style="height:40px; width: 245px;" name="password" Placeholder="Password" required/><br>
		</div>
		<div class="qwe">
		 <button class="btn btn-large btn-primary btn-block pull-right" href="dashboard.html" type="submit"><i class="glyphicon glyphicon-edit"></i><b> Login</b></button>
</div>
		 </form>
</div>
</div>
</div>
</div>
</body>
</html>

login.php

<?php

	session_start();


	$errmsg_arr = array();


	$errflag = false;


	$link = mysql_connect('localhost','root',"");
	if(!$link) {
		die('Failed to connect to server: ' . mysql_error());
	}


	$db = mysql_select_db('sales', $link);
	if(!$db) {
		die("Unable to select database");
	}


	function clean($str) {
		$str = @trim($str);
		if(get_magic_quotes_gpc()) {
			$str = stripslashes($str);
		}
		return mysql_real_escape_string($str);
	}


	$login = clean($_POST['username']);
	$password = clean($_POST['password']);


	if($login == '') {
		$errmsg_arr[] = 'Username missing';
		$errflag = true;
	}
	if($password == '') {
		$errmsg_arr[] = 'Password missing';
		$errflag = true;
	}


	if($errflag) {
		$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
		session_write_close();
		header("location: index.php");
		exit();
	}


	$qry="SELECT * FROM user WHERE username='$login' AND password='$password'";
	$result=mysql_query($qry);

	if($result) {
		if(mysql_num_rows($result) > 0) {

			session_regenerate_id();
			$member = mysql_fetch_assoc($result);
			$_SESSION['SESS_MEMBER_ID'] = $member['id'];
			$_SESSION['SESS_FIRST_NAME'] = $member['name'];
			$_SESSION['SESS_LAST_NAME'] = $member['position'];

			session_write_close();
			header("location: main/index.php");
			exit();
		}else {
			//Login failed
			header("location: index.php");
			exit();
		}
	}else {
		die("Query failed");
	}
?>

avatar randishady25
@randishady25

15 Kontribusi 1 Poin

Diperbarui 5 tahun yang lalu

1 Jawaban:

Kamu bisa belajar tentang php termasuk PDO disini <a href=' https://sekolahkoding.com/track/belajar-php '>belajar PHP dari dasar di sekolahkoding</a>

avatar hilmanski
@hilmanski

2686 Kontribusi 2135 Poin

Dipost 5 tahun yang lalu

Login untuk ikut Jawaban