พอดีกำลังฝึกเปลี่ยนมาใช้mysqli ช่วยตรวจให้หน่อยครับ
พอดีกำลังฝึกเปลี่ยนโค้ดเดิมมาใช้mysqli ไม่รู้ว่าเขียนถูกต้องตรงตามหลักไหม ช่วยตรวจให้หน่อยครับ
ไฟล์ config.php
<?php //ติตต่อdatabase
$localhost = "localhost";
$user ="root";
$pass ="root";
$dbname ="pajdb";
$link = mysqli_connect($localhost , $user , $pass , $dbname) or die("Error Connect to Database");
mysqli_set_charset($link, "utf8");
mysqli_set_charset($link, "character_set_results=utf8");
mysqli_set_charset($link, "character_set_client=utf8");
mysqli_set_charset($link, "character_set_connection=utf8");
header("Content-type:text/html; charset=UTF-8");
ob_start();
@ session_start(); //เริ่ม session
$base_path = "http://localhost:8080/no/paj";
date_default_timezone_set('Asia/Bangkok'); ?>
ไฟล์ informationdetail.php
<?php
include_once("header.php");
if(!empty($_GET["id"])){
$id = $_GET["id"];
$select = "Select * from information Where md5(md5(md5(info_id)))='$id'";
}elseif(!empty($_GET["type_id"])){
$type_id = $_GET["type_id"];
$select = "Select * from information Where md5(md5(md5(type)))='$type_id'";
}
$query = mysqli_query($link,$select) or die(mysqli_error($link));
while ($data = mysqli_fetch_array($query)){
?>
<div class="container theme-showcase bg" role="main">
<div class="row">
<div class="panel panel-mbrown">
<div class="panel-heading">
<center><h3><b><?php echo $data['info_title']; ?></b></h3></center>
</div>
</div>
<div class="col-md-12">
<div class="col-md-4"></div>
<div class="col-md-4">
<?php if($data['info_image'] != ""){ ?>
<center>
<a href="img/information/<?php echo $data['info_image']; ?>" target='_blank' >
<img src="img/information/<?php echo $data['info_image'];?>" width="" height="auto" border="0" class="thumbnail img-responsive aj" alt="Responsive image"></a>
</center>
<?php }else{} ?>
</div>
<div class="col-md-4"></div>
</div>
<div class="col-md-12 borders ">
<div class="">
<?php echo $data['description'];?>
</div></div>
<div class="col-md-12">
<center>
<?php $select1 = "Select * from information_images Where md5(md5(md5(info_id)))='$id'";
$query1 = mysqli_query($link,$select1) or die(mysqli_error($link));
while ($data1 = mysqli_fetch_array($query1)){ ?>
<div class="col-xs-4 col-sm-2 col-md-2 col-lg-2">
<a href="img/sub_information/<?php echo $data1['img_oname']; ?>" target='_blank' > <!--target='_blank' เป็นคำสั่งให้เปิด link ในหน้าใหม่-->
<img src="img/sub_information/<?php echo $data1['img_oname']; ?>" class="img-thumbnail img-responsive" alt="Responsive image"></a>
</div>
<?php } ?>
</center>
</div>
<div class="">
<div class="hidden-xs col-sm-12 col-md-12 col-lg-12"><hr>
<!--Facebook Like&Share-->
<div class="fb-like" data-href="<?php echo $base_path; ?>/information_detail.php?id=<?php echo $data['info_id'];?>" data-width="" data-layout="standard" data-action="like" data-size="large" data-show-faces="true" data-share="true"></div> <br>
<!--End Facebook Like&Share-->
</div>
<div class="col-xs-12 hidden-sm hidden-md hidden-lg"><hr>
<!--Facebook Like&Share-->
<div class="fb-like" data-href="<?php echo $base_path; ?>/information_detail.php?id=<?php echo $data['info_id'];?>" data-width="" data-layout="standard" data-action="like" data-size="small" data-show-faces="false" data-share="true"></div> <br>
<!--End Facebook Like&Share-->
</div>
<!--Facebook Comment-->
<div class="fb-comments" data-href="<?php echo $base_path; ?>/information_detail.php?id=<?php echo $data['info_id'];?>" data-width="" data-numposts="5"></div>
<!--End Facebook Comment-->
</div>
</div></div>
<?php } mysqli_close($link); ?>
Tag : PHP, MySQL, HTML/CSS
Date :
2016-10-14 22:12:38
By :
no007
View :
797
Reply :
3
เปลี่ยนมาเขียน MySQLi แต่ยังเขียน Procedural
ต้องฝึก Object oriented style นะ
Code (PHP)
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
/* Create table doesn't return a resultset */
if ($mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
printf("Table myCity successfully created.\n");
}
/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) {
printf("Select returned %d rows.\n", $result->num_rows);
/* free result set */
$result->close();
}
/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = $mysqli->query("SELECT * FROM City", MYSQLI_USE_RESULT)) {
/* Note, that we can't execute any functions which interact with the
server until result set was closed. All calls will return an
'out of sync' error */
if (!$mysqli->query("SET @a:='this will not work'")) {
printf("Error: %s\n", $mysqli->error);
}
$result->close();
}
$mysqli->close();
?>
Date :
2016-10-17 13:00:53
By :
fossil31
ใช้แบบ object ดีกว่าอีกเสียงครับ
Date :
2016-10-25 10:44:51
By :
mr.v
Load balance : Server 03