Fatal error: Call to a member function query() on a non-object
Code (PHP)
$sql = $db->query("INSERT INTO uploadfile(filename)VALUES ('{$filename}')");
แก้เป็น
Code (PHP)
$sql = $db->query("INSERT INTO uploadfile(filename)VALUES ('$filename')");
Date :
2014-05-06 09:59:52
By :
Freeland
เดานะครับ ผมคิดว่า เปลี่ยนจาก ' ' เป็น " " ดูนะครับ ถ้าไม่งั้นต้องรบกวนท่านอื่นครับ ^^
Date :
2014-05-06 10:26:44
By :
Freeland
โอ้ ตอบช้าไป
Date :
2014-05-06 10:57:04
By :
mr.win
Fatal error: Call to a member function query() on a non-object in C:\AppServ\www\supplierdata\supplier_save.php on line 46
เออเร่อแบบนี้ แก้ยังไงค่ะ รบกวนผู้รู้หน่อยค่ะ
ข้างล่างนี้ คือหน้าโค้ดค่ะ
<?php require("./inc/class.database.php");?>
<?php
$nId_supplier = $_POST['nId_supplier'];
$cFname_suppiler = $_POST['cFname_suppiler'];
$cStatus = $_POST['cStatus'];
$CreateDate = $_POST['CreateDate'];
$CreateBy = $_POST['CreateBy'];
if($nId_supplier){
//update
$sql = "
UPDATE tb_supplier SET
tb_supplier.nId_supplier = '$nId_supplier',
tb_supplier.cFname_suppiler = '$cFname_suppiler',
tb_supplier.cStatus = '$cStatus',
tb_supplier.CreateDate = '$CreateDate',
tb_supplier.CreateBy = '$CreateBy',
WHERE tb_supplier.nId_supplier = '$nId_supplier'
";
$db->query($sql);
}elseif(cFname_suppiler){
//insert
$sql = "
INSERT INTO tb_news (
tb_supplier.cFname_suppiler,
tb_supplier.cStatus,
tb_supplier.CreateDate,
tb_supplier.CreateBy
)VALUES(
'$nId_supplier',
'$cFname_suppiler',
'$cStatus',
'$CreateDate',
'$CreateBy',
'$nId_supplier'
)
";
$db->query($sql);
}
header('location:/supplierdata/Add-Update Schedule.php');
?>
Date :
2018-11-08 13:55:25
By :
waen waen
เอา tag php ครอบ หน่อยครับ
แล้ว เอา บันทัด 46 มาแสดงไว้ด้วยครับ
เดาเอาว่า $db ไม่มี อาจจะต้องเช็ค ไฟล์ ที่ include เขียนไว้อย่างไร
Date :
2018-11-08 15:06:21
By :
Chaidhanan
}elseif(cFname_suppiler){ ตรงนี้ขาด $
}elseif($cFname_suppiler){
Date :
2018-11-08 17:35:51
By :
Chaidhanan
ขอบคุณนะค่ะ ตอนนี้เออเร่อนั้นแก้ได้แล้วค่ะ
Date :
2018-11-09 08:57:18
By :
waen waen
Fatal error: Call to a member function select_filter() on a non-object in C:\AppServ\www\supplierdata\login_check.php on line 19
แต่ติดเออเร่อ ไฟล์นี้ค่ะ รบกวนผู้รู้ช่วยดูหน่อยนะค่ะ พึ่งจะเริ่มเขียนโปรแกรมค่ะ ไม่ค่อยรู้เรื่องค่ะ แต่จะพยายามนะค่ะ
โค้ดหน้า login ค่ะ
<?php session_start(); session_destroy(); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index</title>
<meta http-equiv="Content-Type" content="text/html; harset="utf-8">
<link href="css/default.css" rel="stylesheet" />
<script type="text/javascript" src="js/functions.js"></script>
<style>
body {font-family: Arial, Helvetica, sans-serif;}
form {border: 3px solid #f1f1f1;}
input[type=text], input[type=password] {
width: 20%;
padding: 10px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;
}
button {
background-color: #4CAF50;
color: white;
padding: 12px 110px;
margin: 8px 0;
border: none;
cursor: pointer;
width: auto;
}
button:hover {
opacity: 0.8;
}
.cancelbtn {
width: auto;
padding: 12px 106px;
background-color: #f44336;
}
.imgcontainer {
text-align: center;
margin: 24px 0 12px 0;
}
img.avatar {
width: 15%;
border-radius: 50%;
}
.container {
padding: 16px;
}
span.psw {
float: right;
padding-top: 16px;
}
/* Change styles for span and cancel button on extra small screens */
@media screen and (max-width: 1000px) {
span.psw {
display: block;
float: none;
}
.cancelbtn {
width: 50%;
}
}
p{
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<div class="row margin-top60">
<h2 class="margin-top60">Please enter your username and password.</h2>
<form action="login_check.php" method="POST">
<div class="img container">
<img src="image/FemaleAvatar-1.png" alt="Avatar" class="avatar">
</div>
<div class="container">
<div class="row font60">
<label for="inputFname"><b>Username</b></label></br>
<input type="text" name="cFname" id="inputFname" placeholder="Enter Username" >
</br>
</div>
<div class="row font60">
<label for="inputPassword"><b>Password</b></label></br>
<input type="password" name="password" maxlength="8" placeholder="Enter Password">
</br>
</div>
</div>
<div class="margin-top60">
<button class="margin-top60" type="submit">Login</button>
</br>
<button type="button" class="cancelbtn">Cancel</button>
</div>
</form>
</div>
</div>
</body>
</html>
นี้โค้ดหน้า login_check ค่ะ รบกวนหน่อยนะค่ะ
<?php require("./inc/class.database.php");?>
<?php
$cFname = $_POST['cFname'];
$cPassword = $_POST['password'];
//echo"recive{$cPassword}<br>";
$sql = "
SELECT
tb_userlogin.cFname,
tb_userlogin.cPassword
FROM
tb_userlogin
WHERE
tb_userlogin.cFname = ?;
";
$data = $db->select_filter($sql,$cFname);
//print_r($data);
$session=0;
foreach($data as $value){
$pwd = $value['cPassword'];
//echo $pwd;
//echo "<br> {$cPassword}";
if($pwd==$cPassword){
$session=1;
}
}
//echo $session;
if($session==1){
session_start();
$_SESSION['login'] = true;
$_SESSION['user'] = $cFname;
header("Location:/datasupplier/form_supplier1.php");
}else{
header("Location:/datasupplier/index1.php");
}
?>
Date :
2018-11-09 09:06:43
By :
waen waen
$data = $db ->select_filter ($sql,$cFname);
ไม่รู้ว่า สีน้ำเงิน มีตัวแปร หรือไม่ function call to non-object
อาจต้องเอา ไฟล์ นี้มาดูด้วยครับ require("./inc/class.database.php");?>
Date :
2018-11-09 09:36:21
By :
Chaidhanan
นี้เป็นโค้ดไฟล์ class.database ค่ะ รบกวนด้วยนะค่ะ
<?php
class Config
{
var $host = '';
var $user = '';
var $password = '';
var $database = '';
function Config()
{
//$this->host = "CHON-TSM-SLA-01"; //กำหนดชื่อ server
//$this->database = "Train04_AppDemo"; //กำหนดชื่อ database บน Microsoft SQL
$this->host = "RAY-TSM-WEB-01\\SQLEXPRESS"; //กำหนดชื่อ server
//$this->database = "TSM_TEST"; //กำหนดชื่อ database บน Microsoft S
$this->database = "s_supplier"; //กำหนดชื่อ database บน Microsoft SQL
$this->user = "sa"; //กำหนดชื่อ user name ที่จะ connect database
$this->password = "p@ssword2+"; //กำหนด password ที่จะ connect ไปที่ database
//$this->sl8_host = "RAY-TSM-SLD-01"; //กำหนดชื่อ serv
//$this->sl8_host = "CHON-TSM-SLA-01";
//$this->sl8_database = "LS_TSMEC_APP"; //กำหนดชื่อ database บน Microsoft SQL
//$this->sl8_database = "Train05_Appdemo";
//$this->sl8_user = "sa"; //กำหนดชื่อ user name ที่จะ connect database
//$this->sl8_password = "p@ssword1+"; //กำหนด password ที่จะ connect ไปที่ datab
}
}
class Database
{
// Connection parameters
var $host = '';
var $user = '';
var $password = '';
var $database = '';
var $sl8_host = '';
var $sl8_user = '';
var $sl8_password = '';
var $sl8_database = '';
var $persistent = false;
// Database connection handle
var $conn = NULL;
// Query result
var $result = false;
var $total = 0;
var $num_page = 0;
var $prev_page = 0;
var $next_page = 0;
var $page = 0;
// function DB($host, $user, $password, $database, $persistent = false)
function Database()
{
$config = new Config();
$this->host = $config->host;
$this->user = $config->user;
$this->password = $config->password;
$this->database = $config->database;
//$this->sl8_host = $config->sl8_host;
//$this->sl8_user = $config->sl8_user;
//$this->sl8_password = $config->sl8_password;
//$this->sl8_database = $config->sl8_database;
}
function open()
{
// Connect to the MSSQL server
$connection_string = 'DRIVER={SQL SERVER};SERVER=' . $this->host . ';DATABASE=' . $this->s_supplier;
$this->conn = odbc_connect($connection_string,$this->user, $this->password) or die (”เชื่อมไม่ได้”);
if (!$this->conn) {
header("Location: error.html");
//echo mysql_error();
exit;
return false;
};
return true;
}
function open_sl8()
{
// Connect to the MSSQL server
$connection_string = 'DRIVER={SQL SERVER};SERVER=' . $this->sl8_host . ';DATABASE=' . $this->sl8_database;
$this->conn = odbc_connect($connection_string,$this->sl8_user, $this->sl8_password) or die (”เชื่อมไม่ได้”);
if (!$this->conn) {
header("Location: error.html");
//echo mysql_error();
exit;
return false;
};
return true;
}
function close()
{
return (@odbc_close($this->conn));
}
function error()
{
return (odbc_error());
}
function query($sql = '')
{
$this->result = @odbc_exec($this->conn,$sql);
return ($this->result != false);
}
/*
function query($sql = '')
{
$this->result = @odbc_exec($this->conn,$sql);
return ($this->result != false);
}
*/
function numRows()
{
return (@odbc_num_rows($this->result));
}
function fetchArray()
{
return (@odbc_fetch_array($this->result));
}
function fetchAssoc()
{
return (@odbc_fetch_assoc($this->result));
}
function freeResult()
{
return (@odbc_free_result($this->result));
}
//function sumqty($doc_id){
// select sum() from
//}
function ThaiToUtf8($string)
{
if(false == preg_match('#[\241-\377]#i', $string))
{
return $string;
}
return strtr($string, array("\xa1" => "\xe0\xb8\x81",
"\xa2" => "\xe0\xb8\x82",
"\xa3" => "\xe0\xb8\x83",
"\xa4" => "\xe0\xb8\x84",
"\xa5" => "\xe0\xb8\x85",
"\xa6" => "\xe0\xb8\x86",
"\xa7" => "\xe0\xb8\x87",
"\xa8" => "\xe0\xb8\x88",
"\xa9" => "\xe0\xb8\x89",
"\xaa" => "\xe0\xb8\x8a",
"\xab" => "\xe0\xb8\x8b",
"\xac" => "\xe0\xb8\x8c",
"\xad" => "\xe0\xb8\x8d",
"\xae" => "\xe0\xb8\x8e",
"\xaf" => "\xe0\xb8\x8f",
"\xb0" => "\xe0\xb8\x90",
"\xb1" => "\xe0\xb8\x91",
"\xb2" => "\xe0\xb8\x92",
"\xb3" => "\xe0\xb8\x93",
"\xb4" => "\xe0\xb8\x94",
"\xb5" => "\xe0\xb8\x95",
"\xb6" => "\xe0\xb8\x96",
"\xb7" => "\xe0\xb8\x97",
"\xb8" => "\xe0\xb8\x98",
"\xb9" => "\xe0\xb8\x99",
"\xba" => "\xe0\xb8\x9a",
"\xbb" => "\xe0\xb8\x9b",
"\xbc" => "\xe0\xb8\x9c",
"\xbd" => "\xe0\xb8\x9d",
"\xbe" => "\xe0\xb8\x9e",
"\xbf" => "\xe0\xb8\x9f",
"\xc0" => "\xe0\xb8\xa0",
"\xc1" => "\xe0\xb8\xa1",
"\xc2" => "\xe0\xb8\xa2",
"\xc3" => "\xe0\xb8\xa3",
"\xc4" => "\xe0\xb8\xa4",
"\xc5" => "\xe0\xb8\xa5",
"\xc6" => "\xe0\xb8\xa6",
"\xc7" => "\xe0\xb8\xa7",
"\xc8" => "\xe0\xb8\xa8",
"\xc9" => "\xe0\xb8\xa9",
"\xca" => "\xe0\xb8\xaa",
"\xcb" => "\xe0\xb8\xab",
"\xcc" => "\xe0\xb8\xac",
"\xcd" => "\xe0\xb8\xad",
"\xce" => "\xe0\xb8\xae",
"\xcf" => "\xe0\xb8\xaf",
"\xd0" => "\xe0\xb8\xb0",
"\xd1" => "\xe0\xb8\xb1",
"\xd2" => "\xe0\xb8\xb2",
"\xd3" => "\xe0\xb8\xb3",
"\xd4" => "\xe0\xb8\xb4",
"\xd5" => "\xe0\xb8\xb5",
"\xd6" => "\xe0\xb8\xb6",
"\xd7" => "\xe0\xb8\xb7",
"\xd8" => "\xe0\xb8\xb8",
"\xd9" => "\xe0\xb8\xb9",
"\xda" => "\xe0\xb8\xba",
"\xdf" => "\xe0\xb8\xbf",
"\xe0" => "\xe0\xb9\x80",
"\xe1" => "\xe0\xb9\x81",
"\xe2" => "\xe0\xb9\x82",
"\xe3" => "\xe0\xb9\x83",
"\xe4" => "\xe0\xb9\x84",
"\xe5" => "\xe0\xb9\x85",
"\xe6" => "\xe0\xb9\x86",
"\xe7" => "\xe0\xb9\x87",
"\xe8" => "\xe0\xb9\x88",
"\xe9" => "\xe0\xb9\x89",
"\xea" => "\xe0\xb9\x8a",
"\xeb" => "\xe0\xb9\x8b",
"\xec" => "\xe0\xb9\x8c",
"\xed" => "\xe0\xb9\x8d",
"\xee" => "\xe0\xb9\x8e",
"\xef" => "\xe0\xb9\x8f",
"\xf0" => "\xe0\xb9\x90",
"\xf1" => "\xe0\xb9\x91",
"\xf2" => "\xe0\xb9\x92",
"\xf3" => "\xe0\xb9\x93",
"\xf4" => "\xe0\xb9\x94",
"\xf5" => "\xe0\xb9\x95",
"\xf6" => "\xe0\xb9\x96",
"\xf7" => "\xe0\xb9\x97",
"\xf8" => "\xe0\xb9\x98",
"\xf9" => "\xe0\xb9\x99",
"\xfa" => "\xe0\xb9\x9a",
"\xfb" => "\xe0\xb9\x9b"));
}
}
?>
Date :
2018-11-09 10:20:44
By :
waen waen
ที่บันทัดสุดท้าย ก่อนเครื่องหมาย ?> ของไฟล์ include
$db=new Database();
คุณไม่ได้ประกาศตัวแปบเพื่อใช้แทน คลาส
และ แน่ใจเหรอว่าใช่ไฟล์นี้ เพราะ ผมไม่เห็น method สีแดง ในคลาสที่คุณเอามาแสดง
เด๊่ยวก็ error อีก หา function ไม่เจอ
$data = $db->select_filter ($sql,$cFname);
Date :
2018-11-09 13:06:00
By :
Chaidhanan
ตอนนี้ใช้ไฟล์ include ไฟล์นี้ค่ะ
<?php
class DB {
public $conn;
function __construct(){
self::connect();
}
public function connect(){
try
{
/// Database Connection ///
$db_name = 'mysql:host='.'RAY-TSM-WEB-01\\SQLEXPRESS'.';dbname='.'s_supplier';
$db_user = 'sa';
$db_pass = 'p@ssword2+';
$db_options = [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
];
$this->conn = new PDO($db_name, $db_user, $db_pass, $db_options);
$this->conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(Exception $e)
{
die( print_r( $e->getMessage() ) );
}
}
public function select($tsql){
$get = $this->conn->prepare($tsql);
try
{
$get->execute();
}
catch(Exception $e)
{
echo "<pre>";
print_r( $e ) ;
exit;
}
$data = $get->fetchAll(PDO::FETCH_ASSOC);
$this->count = count($data);
return $data;
}
public function query( $tsql ){
$get = $this->conn->prepare($tsql);
try
{
$get->execute();
}
catch(Exception $e)
{
echo "<pre>";
print_r( $e ) ;
exit;
}
return $get;
}
public function select_filter($tsql,$filter){
$get = $this->conn->prepare($tsql);
try
{
$get->execute($filter);
$data = $get->fetchAll(PDO::FETCH_ASSOC);
$this->count = count($data);
return $data;
} catch(Exception $e){
return "error";
exit;
}
}
public function insert_getId( $tsql ){
$get = $this->conn->prepare($tsql);
try
{
$get->execute();
return $this->conn->lastInsertId();
}
catch(Exception $e)
{
echo "<pre>";
print_r( $e ) ;
exit;
}
}
public function getCount( ){
return $this->count;
}
// ตัวสร้าง dropdown select
public function dropdown($sql, $conf){ // sql, config
/*
type of config
id,name,class,key, value, selected ,multiple
*/
$id = $conf['id'];
$name = $conf['name'];
$class = $conf['class'];
$style = $conf['style'];
$value = $conf['value'];
$text = $conf['text'];
$selected = $conf['selected'];
$firstopt = $conf['firstopt'];
$disabled = $conf['disabled'];
$required = $conf['required'];
$readonly = $conf['readonly'];
$line_order = $conf['line_order'];
$attr = $conf['attr'];
$data = $this->select($sql);
echo "<select name='$name' id='$id' class='$class' style='$style' $disabled $readonly $attr $required > ";
if($firstopt){
echo "<option value=''>$firstopt</option>";
}
$i = 1;
$num = 0;
foreach($data as $data_arr){
$num++;
if($line_order){
$txt_order ="{$num}.";
}else{
$txt_order ="";
}
$value_option = $data_arr[$value];
$text_option = $data_arr[$text];
if($selected==$value_option){ $selected_txt = ' selected '; }else{ $selected_txt = ''; }
echo "<option value='$value_option' $selected_txt >{$txt_order}{$text_option}</option>";
$i++;
}
echo '</select>';
}
}
$db = new DB;
$con=mysqli_connect(DB_HOST,DB_USER,DB_PWD,DBNAME);
?>
รันดูแล้ว ฟ้องเออเร่อแบบนี้ค่ะ Parse error: syntax error, unexpected '[' in C:\AppServ\www\supplierdata\inc\db\db_class.php on line 15 บอก syntax error
Date :
2018-11-09 13:44:38
By :
waen waen
Date :
2018-11-09 14:15:55
By :
Chaidhanan
ต้องแก้ยังไงดีค่ะ
Date :
2018-11-09 14:17:48
By :
waen waen
Code (PHP)
$db_options = [
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
];
//ลองเปลี่ยนเป็น
$db_options = array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' );
Date :
2018-11-09 16:39:39
By :
Chaidhanan
Fatal error: Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' in C:\AppServ\www\supplierdata\inc\db\db_class.php on line 15
ยังไม่ได้เลยค่ะ
Date :
2018-11-09 17:25:08
By :
waen waen
ใช้ php เวอร์ชั่นไหนเหรอครับ ได้ลง extension pdo_mysql หรือยัง
ประวัติการแก้ไข 2018-11-09 17:43:46
Date :
2018-11-09 17:42:54
By :
Chaidhanan
Load balance : Server 02