|
|
|
1 วันกับ php และ php class แรก |
|
|
|
|
|
|
|
ไม่มีอะไร แค่อยากโชว์เฉยๆ
index.php
<?php require_once(dirname(__FILE__) . '/conf/config.php'); ?>
<html>
<head>
<title>PHP OOP</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<?php
$sqlsvr = new database\sql_server();
$conn = $sqlsvr -> connection();
$query = "SELECT * FROM Section WHERE ID BETWEEN ? AND ?";
$params = array(2, 5);
$results = $sqlsvr -> query($conn, $query, $params);
if ($results)
{
echo "<ul>";
foreach ($results as $result)
{
echo "<li>
$result->ID $result->SectionCode (Database ID: $result->SectionName)
</li>";
}
echo "</ul>";
}
else
{
echo "<p>Sorry, we have no information regarding that manufacturer.</p>";
}
$sqlsvr -> close_connection($conn);
?>
</body>
</html>
config.php
<?php
// Include class
require_once(dirname(dirname(__FILE__)) . '/inc/class/sql_server.php');
require_once(dirname(dirname(__FILE__)) . '/inc/class/query_result.php');
// Database
define ( 'DB_HOST', '.\SQLEXPRESS' );
define ( 'DB_Name', 'SqlServer2012' );
define ( 'DB_USER', '' );
define ( 'DB_PASSWORD', '' );
define ( 'DB_CHARACTERSET', 'UTF-8' );
?>
sql_server.php
<?php
namespace database;
class sql_server
{
public function connection()
{
$connection_info = array("UID" => DB_USER,
"PWD" => DB_PASSWORD,
"Database" => DB_Name,
"CharacterSet" => DB_CHARACTERSET);
$conn = sqlsrv_connect(DB_HOST, $connection_info);
if ($conn === false)
{
echo "Unable to connect.<br />";
die(print_r(sqlsrv_errors(), true));
}
return $conn;
}
public function close_connection($conn)
{
sqlsrv_close( $conn);
}
public function query($conn, $sql, $params = false)
{
$res = ($params === false) ? sqlsrv_query($conn, $sql) : sqlsrv_query($conn, $sql, $params) ;
if ($res === false)
{
echo "Error in executing query.<br />";
die(print_r(sqlsrv_errors(), true));
}
$results = array();
while ($row = sqlsrv_fetch_array($res))
{
$result = new query_result();
foreach ($row as $k => $v)
{
$result -> $k = $v;
}
$results[] = $result;
}
return $results;
}
}
?>
query_result.php
<?php
namespace database;
class query_result
{
private $_results = array();
public function __set($var, $val)
{
$this -> _results[$var] = $val;
}
public function __get($var)
{
if (isset($this -> _results[$var]))
{
return $this -> _results[$var];
} else
{
return null;
}
}
}
?>
ก็สนุกดีเนอะ php เนี่ย แต่ก็ยังไม่ oop เต็มรูปแบบอยู่บ้างก็เถอะ อิอิ
Tag : PHP, Ms SQL Server 2012, Web (ASP.NET)
|
|
|
|
|
|
Date :
2013-01-22 13:28:05 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
View :
1094 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไปเร็วดีน่ะครับ
|
|
|
|
|
Date :
2013-01-22 13:30:21 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
วันนี้พอแล้วครับ อู้นาน ทำงานก่อน
ไว้พรุ่งนี้จะลอง mvc framework ดู
ตอนนี้ลง framework ยังไงยังไม่รู้เรื่องเลย
|
|
|
|
|
Date :
2013-01-22 13:43:22 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|