 |
PHP/MySQL JSON (Web Services) มันเป็นอะไรอีกแล้วอะ ทำตามนี้ทุกอย่างแต่ขึ้น Not found data! |
|
 |
|
|
 |
 |
|
php mysql JSON มันเป็นอะไรอีกแล้วอะ ทำตามนี้ทุกอย่างแต่ขึ้น Not found data!
ที่มา https://www.thaicreate.com/community/php-web-service-json.html
คือเมื่อก่อนมันรันได้คิวรีข้อมูลให้ แต่พอเอาไฟล์ .htaccess เนื่องจากติดปัญหาส่งค่าผ่านฟอร์ม กลับมารันอีกทีรันไม่ผ่าน ขึ้น Not found data! งง 10 วันก่อนยังรันผ่านเลย งง มาก มีงี้ด้วยเหรอ
ก่อนอื่น
ฐานข้อมูลฝั่ง Server
Code (SQL)
CREATE TABLE `customer` (
`CustomerID` varchar(4) NOT NULL,
`Username` varchar(30) NOT NULL,
`Password` varchar(30) NOT NULL,
`Name` varchar(50) NOT NULL,
`Email` varchar(50) NOT NULL,
`CountryCode` varchar(2) NOT NULL,
`Budget` double NOT NULL,
`Used` double NOT NULL,
PRIMARY KEY (`CustomerID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Dumping data for table `customer`
--
INSERT INTO `customer` VALUES ('C001', 'win', 'win001', 'Win Weerachai', '[email protected]', 'TH', 1000000, 600000);
INSERT INTO `customer` VALUES ('C002', 'john', 'john002', 'John Smith', '[email protected]', 'EN', 2000000, 800000);
INSERT INTO `customer` VALUES ('C003', 'jame', 'jame003', 'Jame Born', '[email protected]', 'US', 3000000, 600000);
INSERT INTO `customer` VALUES ('C004', 'chalee', 'chalee004', 'Chalee Angel', '[email protected]', 'US', 4000000, 100000);
Web Service ฝั่ง Serverใช้:
Code (WebServiceServer.php)
<?php
require_once("lib/nusoap.php");
//Create a new soap server
$server = new soap_server();
//Configure our WSDL
$server->configureWSDL("getCustomer");
// Register our method and argument parameters
$varname = array(
'strCountry' => "xsd:string"
);
$server->register('resultCustomer',$varname, array('return' => 'xsd:string'));
function resultCustomer($strCountry)
{
$objConnect = mysql_connect("localhost","root","") or die(mysql_error());
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer WHERE 1 AND CountryCode like '%".$strCountry."%' ";
$objQuery = mysql_query($strSQL) or die (mysql_error());
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery))
{
$arrCol = array();
for($i=0;$i<$intNumField;$i++)
{
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
return json_encode($resultArray);
}
// Get our posted data if the service is being consumed
// otherwise leave this data blank.
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
// pass our posted data (or nothing) to the soap service
$server->service($POST_DATA);
exit();
?>
Code (WebServiceClient.php)
<html>
<head>
<title>ThaiCreate.Com</title>
</head>
<body>
<form name="frmMain" method="post" action="">
<h2>Search Customer</h2>
Search by Country Code
<input type="text" name="txtCountry" value="<?php echo $_POST["txtCountry"];?>">
<input type="submit" name="Submit" value="Submit">
</form>
<?php
if($_POST["txtCountry"] != "")
{
include("lib/nusoap.php");
$client = new nusoap_client("http://localhost/nusoap/WebServiceServer.php?wsdl",true);
$params = array(
'strCountry' => $_POST["txtCountry"]
);
$data = $client->call('resultCustomer', $params);
//echo '<pre>';
//var_dump(json_decode($data));
//echo '</pre><hr />';
$mydata = json_decode($data,true); // json decode from web service
if(count($mydata) == 0)
{
echo "Not found data!";
}
else
{
?>
<table width="500" border="1">
<tr>
<td>CustomerID</td>
<td>Name</td>
<td>Email</td>
<td>CountryCode</td>
<td>Budget</td>
<td>Used</td>
</tr>
<?php
foreach ($mydata as $result) {
?>
<tr>
<td><?php echo $result["CustomerID"];?></td>
<td><?php echo $result["Name"];?></td>
<td><?php echo $result["Email"];?></td>
<td><?php echo $result["CountryCode"];?></td>
<td><?php echo $result["Budget"];?></td>
<td><?php echo $result["Used"];?></td>
</tr>
<?php
}
}
}
?>
</body>
</html>
Tag : PHP, Web Service
|
|
 |
 |
 |
 |
Date :
2015-02-17 16:32:41 |
By :
paravatee |
View :
811 |
Reply :
9 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
function resultCustomer($strCountry)
{
$objConnect = mysql_connect("localhost","root","") or die(mysql_error());
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer WHERE 1 AND CountryCode like '%".$strCountry."%' ";
$objQuery = mysql_query($strSQL) or die (mysql_error());
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery))
{
$arrCol = array();
for($i=0;$i<$intNumField;$i++)
{
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
return json_encode($resultArray);
}
คิดว่าตรงนี้น่าจะมีปัญหาครับ
|
 |
 |
 |
 |
Date :
2015-02-17 18:49:07 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมแก้ใหม่แล้วคับ เป็นดังนี้
[/php]
ปัญหาเดิม Not found data! ทั้งที่ๆเมื่อ 7 วันก่อนยังรันได้ งง
|
ประวัติการแก้ไข 2015-02-18 09:34:26
 |
 |
 |
 |
Date :
2015-02-18 08:38:02 |
By :
paravatee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันน่าจะเป็นที่ Query แหละครับ ลองเอา SQL ไไปรันดูบน phpMyAdmin ครับ
|
 |
 |
 |
 |
Date :
2015-02-18 08:50:39 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เพิ่มเติมอีกนิดผมไฟล์ไว้ที่ http://localhost/LAB/01/nusoap/WebServiceServer.php น่าจะถูกนะ
|
 |
 |
 |
 |
Date :
2015-02-18 08:56:13 |
By :
paravatee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือเมื่อเช้าผมรันได้ 1 ครั้ง มันแสดง country code US ให้ นอกนั้นพอกลับไปรันครั้งที่ 2 ก็ไม่ได้อีกแล้วเป็นเพราะต้องไปกำหนดค่าอะไรมั้ยคับ งง
เมื่อเช้าแสดงผล 1 ครั้ง หลักจากนั้นลองครั้งที่ 2 ขึ้นว่า Not found data!
|
 |
 |
 |
 |
Date :
2015-02-18 09:13:08 |
By :
paravatee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เกี่ยวกะอันนี้หรือเปล่า This XML file does not appear to have any style information associated with it. The document tree is shown below.
สคริปเดิม ไม่ได้แก้อะไร 1 ชั่วโมงก่อนรันผ่าน พอผ่านไป 15 นาที รันขึ้น Not found data! พอทิ้งช่วงไป 2 ชั่วโมง รันผ่านขึ้นผลลัพธ์ สคริปไม่ได้แก้อะไรเลย งงมาก
|
 |
 |
 |
 |
Date :
2015-02-18 09:28:05 |
By :
paravatee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
งง ตอนนี้รันได้อีกแล้วคับ เดี๋ยวรอช่วงเย็น 3- 4 ชั่วโมงผ่านไปนะคับ

|
 |
 |
 |
 |
Date :
2015-02-18 09:36:57 |
By :
paravatee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แนะนำให้คุณลองไปนั่งไล่ให้ดี ๆ ก่อนครับ
|
 |
 |
 |
 |
Date :
2015-02-18 09:55:13 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตั้งสติก่อนถาม แต่อารมณ์ประมาณ code ก็ปกติ น่าจะรันได้ ทำไมไม่ได้สักที
หันตัวออกจากจอคอมพ์ ไปออกกำลังกาย ดูหนัง ฟังเพลง สังสรรค์
ผมว่าสมองผ่อนคลายเด๋วคุณก็เจอที่ผิดเองครับ
|
 |
 |
 |
 |
Date :
2015-02-18 10:08:56 |
By :
apisitp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|