ผมเขียน php เชื่อม mssql แล้วติด ปัญหา แบ่งหน้าไม่สำเร็จครับ รบกวนท่านมีมีประสบการณ์ช่วยแนะนำด้วยครับ
ตอนนี้ติดปัญหาโค้ดแบ่งหน้าที่ผมเคยเขียนกับ php mysql มาใช้กับ mssql แต่เกิดปัญหา ช่วยดูให้หน่อยนะครับ
Code
Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'LIMIT'., SQL state 37000 in SQLExecDirect in C:\AppServ\www\Max\home_admin.php on line 110
37000
นี้โค้ดครับ
Code (PHP)
<?
include "chksession.php";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<style type="text/css">
<!--
body,td,th {
font-size: 14px;
color: #000000;
}
body {
background-color: #C0C0C0;
margin-top: 5px;
}
body {
background-color: #FFFFFF;
margin-top: 5px;
}
.style4 {color: #000000; }
-->
</style>
<title></title>
</HEAD>
<body onLoad="MM_preloadImages;" >
<div id="dhtmltooltip"></div>
<script type="text/javascript" src="dhtmltooltip.js"></script>
<?
include"head.html";
?>
<style type="text/css">
body,td,th {
font-family: Tahoma;
font-size: 14px;
}
</style>
<? include 'connect.php'; ?>
<? include 'menu.php';
$a = 1; ?>
<form name="frm<?=$a;?>" method="post" action="<?=$_SERVER['PHP_SELF'];?>" enctype="multipart/form-data">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#ff9933">
<tr bgcolor="#FFFFFF">
<td colspan="7" bgcolor="#FFFFFF" align="center">
<input type='text' name='txt_search' size='16'>
<select name='search_type'>
<option value='1'>รหัส</option>
<option value='2'>ชื่อ</option>
</select>
<input type='submit' value='SEARCH'>
<table width="600">
<tr>
<th width="40" bordercolor="#CC66FF" bgcolor="#FF99FF"> <div align="center">No.</div></th>
<th width="91" bordercolor="#CC66FF" bgcolor="#FF99FF"> <div align="center">EmployeeCode</div></th>
<th width="120" bordercolor="#CC66FF" bgcolor="#FF99FF"> <div align="center">FirstName</div></th>
<th width="98" bordercolor="#CC66FF" bgcolor="#FF99FF"> <div align="center">LastName</div></th>
<th width="60" bordercolor="#CC66FF" bgcolor="#FF99FF"> <div align="center">Race</div></th>
</tr>
<?
if(isset($_POST['txt_search']) && trim($_POST['txt_search']) != ""){
$txt_search = $_POST['txt_search'];
$search_type = $_POST['search_type'];
switch ($search_type){
case 1: $where = "EmployeeCode=" . $txt_search ; break;
case 2: $where = "FirstName LIKE '%" . $txt_search . "%'"; break;
}
$sql = "SELECT * FROM tEmployee WHERE Race = 'พม่า' and " . $where . " ORDER BY EmployeeCode ASC ";
}
else{
$sql = "SELECT * FROM tEmployee WHERE Race = 'พม่า' ORDER BY EmployeeCode ASC ";
}
$Per_Page =10;
if(!$Page)
$Page=1;
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$result = odbc_exec($cid,$sql) or die (odbc_error());
$Page_start = ($Per_Page*$Page)-$Per_Page;
$Num_Rows = odbc_num_rows($result);
if($Num_Rows<=$Per_Page)
$Num_Pages =1;
else if(($Num_Rows % $Per_Page)==0)
$Num_Pages =($Num_Rows/$Per_Page) ;
else
$Num_Pages =($Num_Rows/$Per_Page) +1;
$Num_Pages = (int)$Num_Pages;
if(($Page>$Num_Pages) || ($Page<0))
print "<center><b>จำนวน $Page มากกว่า $Num_Pages ยังไม่มีข้อความ<b></center>";
$sql .= " LIMIT $Page_start , $Per_Page";
$objQuery = odbc_exec($cid,$sql) or die (odbc_error());
while($objResult = odbc_fetch_array($objQuery))
{
$i++;
if($i%2==0)
{
$bg = "#FFDAB9";
}
else
{
$bg = "#FFFAFA";
}
?>
<tr bgcolor="<?=$bg;?>">
<td><span class="style4">
<?=$a++; ?>
</span></td>
<td><div align="center" class="style4">
<?=$objResult["EmployeeCode"];?>
</div></td>
<td><span class="style4">
<?=$objResult["FirstName"];?>
</span></td>
<td><span class="style4">
<?=$objResult["LastName"];?>
</span></td>
<td><div align="center" class="style4">
<?=$objResult["Race"];?>
</span></td>
</tr>
<?
}
?>
</table>
</form><tr bgcolor="#FFFFFF">
</BODY>
</HTML>
Tag : PHP, Ms SQL Server 2005, Ms SQL Server 2008
ประวัติการแก้ไข 2012-07-27 13:28:32
Date :
2012-07-27 13:27:59
By :
yokszaza
View :
1340
Reply :
3
ที่ให้มาผมทำไม่ได้อ่ะครับ
แต่ผมทำ Top ที่คุณบอก แล้วได้ แต่คลิกไปหน้า อื่นๆ ก็ได้ ข้อมูล 500 คนแรก เหมือนกันทุกหน้า ช่วยดูให้ที่ครับแก้ตรงไหนได้บางครับ
Code (PHP)
<?
include "chksession.php";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<style type="text/css">
<!--
body,td,th {
font-size: 14px;
color: #000000;
}
body {
background-color: #C0C0C0;
margin-top: 5px;
}
body {
background-color: #FFFFFF;
margin-top: 5px;
}
.style4 {color: #000000; }
-->
</style>
<title></title>
</HEAD>
<body onLoad="MM_preloadImages;" >
<div id="dhtmltooltip"></div>
<script type="text/javascript" src="dhtmltooltip.js"></script>
<?
include"head.html";
?>
<style type="text/css">
body,td,th {
font-family: Tahoma;
font-size: 14px;
}
</style>
<? include 'connect.php'; ?>
<? include 'menu.php';
$a = 1; ?>
<form name="frm<?=$a;?>" method="post" action="<?=$_SERVER['PHP_SELF'];?>" enctype="multipart/form-data">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#ff9933">
<tr bgcolor="#FFFFFF">
<td colspan="7" bgcolor="#FFFFFF" align="center">
<input type='text' name='txt_search' size='16'>
<select name='search_type'>
<option value='1'>รหัส</option>
<option value='2'>ชื่อ</option>
</select>
<input type='submit' value='SEARCH'>
<table width="600">
<tr>
<th width="40" bordercolor="#CC66FF" bgcolor="#FF99FF"> <div align="center">No.</div></th>
<th width="91" bordercolor="#CC66FF" bgcolor="#FF99FF"> <div align="center">EmployeeCode</div></th>
<th width="120" bordercolor="#CC66FF" bgcolor="#FF99FF"> <div align="center">FirstName</div></th>
<th width="98" bordercolor="#CC66FF" bgcolor="#FF99FF"> <div align="center">LastName</div></th>
<th width="60" bordercolor="#CC66FF" bgcolor="#FF99FF"> <div align="center">Race</div></th>
</tr>
<?
if(isset($_POST['txt_search']) && trim($_POST['txt_search']) != ""){
$txt_search = $_POST['txt_search'];
$search_type = $_POST['search_type'];
switch ($search_type){
case 1: $where = "EmployeeCode=" . $txt_search ; break;
case 2: $where = "FirstName LIKE '%" . $txt_search . "%'"; break;
}
$sql = "SELECT TOP 500 * FROM tEmployee WHERE Race = 'พม่า' and " . $where . " ORDER BY EmployeeCode ASC ";
}
else{
$sql = "SELECT TOP 500 * FROM tEmployee WHERE Race = 'พม่า' ORDER BY EmployeeCode ASC ";
}
$Per_Page =10;
if(!$Page)
$Page=1;
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$result = odbc_exec($cid,$sql) or die (odbc_error());
$Page_start = ($Per_Page*$Page)-$Per_Page;
$Num_Rows = odbc_num_rows($result);
if($Num_Rows<=$Per_Page)
$Num_Pages =1;
else if(($Num_Rows % $Per_Page)==0)
$Num_Pages =($Num_Rows/$Per_Page) ;
else
$Num_Pages =($Num_Rows/$Per_Page) +1;
$Num_Pages = (int)$Num_Pages;
if(($Page>$Num_Pages) || ($Page<0))
print "<center><b>จำนวน $Page มากกว่า $Num_Pages ยังไม่มีข้อความ<b></center>";
//$sql .= " LIMIT $Page_start , $Per_Page";
$objQuery = odbc_exec($cid,$sql) or die (odbc_error());
while($objResult = odbc_fetch_array($objQuery))
{
$i++;
if($i%2==0)
{
$bg = "#FFDAB9";
}
else
{
$bg = "#FFFAFA";
}
?>
<tr bgcolor="<?=$bg;?>">
<td><span class="style4">
<?=$a++; ?>
</span></td>
<td><div align="center" class="style4">
<?=$objResult["EmployeeCode"];?>
</div></td>
<td><span class="style4">
<?=$objResult["FirstName"];?>
</span></td>
<td><span class="style4">
<?=$objResult["LastName"];?>
</span></td>
<td><div align="center" class="style4">
<?=$objResult["Race"];?>
</span></td>
</tr>
<?
}
?>
</table>
<br>
<table width="69%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td> จำนวน
<?= $Num_Rows;?>
</b> แบ่งเป็น : <b>
<?=$Num_Pages;?>
</b> ไปยัง :
<?/* สร้างปุ่มย้อนกลับ */
if($Prev_Page)
echo " <a href='$PHP_SELF?Page=$Prev_Page'><< Back </a> ";
for($i=1; $i<$Num_Pages; $i++){
if($i != $Page)
echo " [ <a href='$PHP_SELF?Page=$i'>$i</a> ]";
else
echo "<b> $i </b>";
}
/*สร้างปุ่มเดินหน้า */
if($Page!=$Num_Pages)
echo " <a href ='$PHP_SELF?Page=$Next_Page'> Next >> </a>";
?>
</font> </td>
</tr>
</table>
<p> </p>
</form><tr bgcolor="#FFFFFF">
</BODY>
</HTML>
Date :
2012-07-27 14:56:42
By :
yokszaza
นี้อีกโค้ดที่ผมลองทำครับ มันไม่แบ่งหน้าครับมันออกหน้าเดียวเลยอ่ะครับ
Code (PHP)
<?
include "chksession.php";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<style type="text/css">
<!--
body,td,th {
font-size: 14px;
color: #000000;
}
body {
background-color: #C0C0C0;
margin-top: 5px;
}
body {
background-color: #FFFFFF;
margin-top: 5px;
}
.style4 {color: #000000; }
-->
</style>
<title></title>
</HEAD>
<body onLoad="MM_preloadImages;" >
<div id="dhtmltooltip"></div>
<script type="text/javascript" src="dhtmltooltip.js"></script>
<?
include"head.html";
?>
<style type="text/css">
body,td,th {
font-family: Tahoma;
font-size: 14px;
}
</style>
<? include 'connect.php'; ?>
<? include 'menu.php';
$a = 1; ?>
<form name="frm<?=$a;?>" method="post" action="<?=$_SERVER['PHP_SELF'];?>" enctype="multipart/form-data">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#ff9933">
<tr bgcolor="#FFFFFF">
<td colspan="7" bgcolor="#FFFFFF" align="center">
<input type='text' name='txt_search' size='16'>
<select name='search_type'>
<option value='1'>รหัส</option>
<option value='2'>ชื่อ</option>
</select>
<input type='submit' value='SEARCH'>
<table width="600">
<tr>
<th width="40" bordercolor="#CC66FF" bgcolor="#FF99FF"> <div align="center">No.</div></th>
<th width="91" bordercolor="#CC66FF" bgcolor="#FF99FF"> <div align="center">EmployeeCode</div></th>
<th width="120" bordercolor="#CC66FF" bgcolor="#FF99FF"> <div align="center">FirstName</div></th>
<th width="98" bordercolor="#CC66FF" bgcolor="#FF99FF"> <div align="center">LastName</div></th>
<th width="60" bordercolor="#CC66FF" bgcolor="#FF99FF"> <div align="center">Race</div></th>
</tr>
<?
if(isset($_POST['txt_search']) && trim($_POST['txt_search']) != ""){
$txt_search = $_POST['txt_search'];
$search_type = $_POST['search_type'];
switch ($search_type){
case 1: $where = "EmployeeCode=" . $txt_search ; break;
case 2: $where = "FirstName LIKE '%" . $txt_search . "%'"; break;
}
$sql = "SELECT * FROM tEmployee WHERE Race = 'พม่า' and " . $where . " ORDER BY EmployeeCode ASC ";
}
else{
$sql = "SELECT * FROM tEmployee WHERE Race = 'พม่า' ORDER BY EmployeeCode ASC ";
}
$Per_Page = 500; // Per Page
$Page = $_GET["Page"];
if(!$_GET["Page"])
{
$Page=1;
}
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
$Num_Pages =1;
}
else if(($Num_Rows % $Per_Page)==0)
{
$Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
$Num_Pages =($Num_Rows/$Per_Page)+1;
$Num_Pages = (int)$Num_Pages;
}
$Page_End = $Per_Page * $Page;
IF ($Page_End > $Num_Rows)
{
$Page_End = $Num_Rows;
}
$objQuery = odbc_exec($cid,$sql) or die (odbc_error());
while($objResult = odbc_fetch_array($objQuery))
{
$c++;
if($c%2==0)
{
$bg = "#FFDAB9";
}
else
{
$bg = "#FFFAFA";
}
?>
<tr bgcolor="<?=$bg;?>">
<td><span class="style4">
<?=$a++; ?>
</span></td>
<td><div align="center" class="style4">
<?=$objResult["EmployeeCode"];?>
</div></td>
<td><span class="style4">
<?=$objResult["FirstName"];?>
</span></td>
<td><span class="style4">
<?=$objResult["LastName"];?>
</span></td>
<td><div align="center" class="style4">
<?=$objResult["Race"];?>
</span></td>
</tr>
<?
}
?>
</table>
<br>
Total <?php echo $Num_Rows; ?> Record : <?php echo $Num_Pages; ?> Page :
<?php
if ($Prev_Page) {
echo " <a href=\"JavaScript:Ajax('$Prev_Page')\"><< Back</a> ";
}
for ($i = 1; $i <= $Num_Pages; $i++) {
if ($i != $Page) {
echo "[ <a href=\"JavaScript:Ajax('$i')\">$i</a> ]";
} else {
echo "<b> $i </b>";
}
}
if ($Page != $Num_Pages) {
echo " <a href=\"JavaScript:Ajax('$Next_Page')\">Next >></a> ";
} ?>
</form><tr bgcolor="#FFFFFF">
</BODY>
</HTML>
Date :
2012-07-27 15:08:11
By :
yokszaza
Load balance : Server 03