|
 |
|
สอบถาม php ไม่ แสดง Error ครับ สอบถามครับ เครื่องไม่แสดง Error งงๆๆ (คือประเด็นคือ พึ่งหัดจับ PHP) |
|
 |
|
|
 |
 |
|
Code (PHP)
error_reporting(E_ERROR | E_WARNING | E_PARSE);
|
 |
 |
 |
 |
Date :
2014-05-19 17:28:01 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โค้ดผิดเยอะมากครับ แฮะๆ เพิ่งหัดจับ PHP แต่เล่น CLASS เลย เด๋วจะท้อเอานะครับ ลองศึกษาคำสั่งแบบพื้นฐานใช้ชำนาญก่อนดีไม๊ครับ  
|
 |
 |
 |
 |
Date :
2014-05-19 17:38:09 |
By :
arm8957 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ๋อปกติเคยเขียนบ้างครับ APS.NET (แต่ที่จริงก็ไม่เคยเขียน OOP หรอกครับ) เรื่องท้ออาจมีบ้างครับแต่ผมว่าการจะพัฒนาก็คงต้องทำ ^ ^
แต่ตอนนี้ ก็ยังไม่ได้อ่าครับ ช่วยผมทีนะครับ ขอบคุณมากครับ
|
 |
 |
 |
 |
Date :
2014-05-19 17:42:41 |
By :
zero1150kfc |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<?php
error_reporting(E_ALL);
class myMysql {
private $dbServer = '127.0.0.1';
private $dbName = 'user_db';
private $dbUser = 'root';
private $dbPass = '1234';
private $Charset = 'utf8';
public $con;
public function __construct() {
$this->con = new mysqli($this->dbServer, $this->dbUser, $this->dbPass, $this->dbName);
$this->con->set_charset($this->Charset);
}
public function myQurey($qurey) {
return $this->con->query($qurey);
}
}
$con = new myMysql();
$qr = $con->myQurey("SELECT * FROM tb_user");
?>
<table border="1">
<?php
while($rs = $qr->fetch_array()) {
?>
<tr>
<td><?php echo $rs["UserID"];?></td>
<td><?php echo $rs["UID"];?></td>
<td align="right"><?php echo $rs["UPASS"];?></td>
<td align="right"><?php echo $rs["Fname"];?></td>
</tr>
<?php
}
?>
</table>
ลองเอาไปปรัปดูนะครับ ผมแก้ให้ละ ใช้ได้ 
|
 |
 |
 |
 |
Date :
2014-05-19 21:26:05 |
By :
geidtiphong |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แสดงว่าคุณตั้งค่า display_errors ใน php.ini ไว้เป็น 0
และที่การใช้
error_reporting(E_ALL);
ini_set("display_errors", 1);
ไม่เกิดผลอะไร เพราะมัน error ก่อนที่โค้ดตรงนั้นจะทำงานครับ
โดย เป็น PARSE_ERROR แน่นอน ตรง
Code
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$dbServer = 'localhost';
$dbName = 'cofc';
$dbUser = 'root';
$dbPass = '12344';
class myMysql{
public myQurey($qurey){
$mysqli = new mysqli($dbServer, $dbUser, $dbPass, $dbName);
return $mysqli->query($qurey);
}
}
?>
PARSE ERROR จะหยุดการ compile ทันที (ก่อนที่จะเริ่มทำงานจริงๆ) และในเมื่อ display_errors ใน php.ini เป็น 0 มันก็จะไม่แสดง error ใดใดให้คุณเห็น
วิธีแก้ ก็คือต้องไปแก้ display_errors ใน php.ini โดยตรงครับ
ลองแก้ตรงนี้ดู แล้วมันจะทำงานได้ และแสดง error แน่นอน
<?php
error_reporting(E_ALL); // เพราะตรงนี้
ini_set("display_errors", 1); // และตรงนี้ทำงานแล้ว
$dbServer = 'localhost';
$dbName = 'cofc';
$dbUser = 'root';
$dbPass = '12344';
class myMysql{
public function myQurey($qurey){ // ตก function
$mysqli = new mysqli($dbServer, $dbUser, $dbPass, $dbName);
return $mysqli->query($qurey);
}
}
?>
และตรงนี้
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$result = new myMysql(); // php เวอร์ชั่นต่ำกว่า 5.4 ไม่สามารถเรียกใช้ -> หลังจาก new ได้โดยตรง
$result->myQurey('select * from customer');
while($objResult = $result->fetch_array()){
?><tr>
<td><?php echo $objResult["id"];?></td>
<td><?php echo $objResult["name"];?></td>
<td align="right"><?php echo $objResult["status"];?></td>
<td align="right"><?php echo $objResult["remark"];?></td>
</tr>
<?php
}
?>
|
 |
 |
 |
 |
Date :
2014-05-19 21:32:23 |
By :
. |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
1. คุณลืมใส่ visibility ก่อนตั้งชื่อแปร ครับ
2. ต้องใช้ $this ในการใช้งาน
3. ต้องประกาศ instant ก่อนใช้งาน function ใน class (หากอยากใช้งาน function ภายนอก ควรเลือกใช้ public ครับ)
4. แนะนำ private สำหรับค่าที่ใช้ติดต่อกับ database ครับ เพราะจะได้ ห่อหุ่มข้อมูลส่วนนี้ครับ
5. แนะนำใช้ __constant กับการติดต่อ database ไว้เลยเพราะ __constant จะทำงานเป็น function แรกเสมอ กล่าวคือ แค่เรียกใช้ Class
$con = new myMysql(); ระบบก็จะติดต่อกับ database อัตโนมัติ สะดวก ครับ
|
 |
 |
 |
 |
Date :
2014-05-19 21:32:58 |
By :
geidtiphong |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สรุปอีกทีนะครับ กลัวงง
เมื่อ php เริ่มทำงาน มันจะอ่าน php.ini ก่อน ซึ่ง display_errors ในนั้นของคุณคงจะเป็น 0
เมื่อมัน compile โค้ดของคุณ หากเจอ Parse Error (Syntax Error) มันจะหยุด compile และแสดง error ทันที
แต่เพราะการตั้งค่าใน display_errors php.ini ของคุณ มันจึงไม่แสดง error ครับ
การใช้ error_reporting() และ ini_set() ในโค้ดของคุณไม่มีความหมาย เพราะมันไม่มีโอกาสได้ทำงาน (ตายก่อนจะไปถึง)
|
 |
 |
 |
 |
Date :
2014-05-19 21:37:25 |
By :
. |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|