Reputation: 151
I got some strange problems. Using my class i connecting to mysql, connection is successful but when i call a function its not works. Im using my own error handler and if i got in php some error on this line it using exit; i have used echo before calling function and after page is not killing (it means that php work successful) mysql query i have checked its works too but when im using return 'some text'; its not returning. any suggestions why function is not working?
my class:
<?php
include('../config.php');
include('../chat_error_handler.php');
class Chat {
private $mysqli;
var $con;
//Constructor open db conn
function __construct() {
$this->mysqli = new mysqli('localhost', 'root', '', 'test');
}
//destructor close db conn
function __destruct() {
$this->mysqli->close();
}
public function postNewMessage($user_name, $message, $color, $room_id) {
$user_name = $this->mysqli->real_escape_string($user_name);
$message = $this->mysqli->real_escape_string($message);
$color = $this->mysqli->real_escape_string($color);
$room_id = $this->mysqli->real_escape_string($room_id);
$query = 'INSERT INTO chat (posted_on, account, message, color, room_id)' .
' VALUES (NOW(), "'.$user_name.'", "'.$message.'", "'.$color.'", "'. $room_id .'")';
$result = $this->mysqli->query($query);
return 'Pranesimas';
$result->close();
}
My php im using to debug:
<?php
include('core/chat.class.php');
include('chat_error_handler.php');
$chat = new Chat();
$message = 'testas';
echo $message.'<br/>';
$chat->postNewMessage('testing', $message, '#000000', 1);
echo '<br/>'.$message;
?>
Upvotes: 3
Views: 279
Reputation: 11862
public function postNewMessage($user_name, $message, $color, $room_id) {
$user_name = $this->mysqli->real_escape_string($user_name);
$message = $this->mysqli->real_escape_string($message);
$color = $this->mysqli->real_escape_string($color);
$room_id = $this->mysqli->real_escape_string($room_id);
$query =
"INSERT INTO
chat ( posted_on,
account,
message,
color,
room_id )
VALUES ( NOW(),
'". $user_name ."',
'". $message ."',
'". $color ."',
'". $room_id ."' )";
$result = $this->mysqli->query($query);
if ( $result ) {
return 'Pranesimas';
} else {
return false;
}
}
Upvotes: 2
Reputation: 9309
About 'var'
in PHP5 and upper.
It's for declaring class member variables in PHP4, and is no longer needed. It will work in PHP5, but will raise an E_STRICT
warning in PHP from version 5.0.0
up to version 5.1.2
, as of which it has been deprecated.
UPDATE:
public function postNewMessage($user_name, $message, $color, $room_id) {
return 'Pranesimas1';
$user_name = $this->mysqli->real_escape_string($user_name);
$message = $this->mysqli->real_escape_string($message);
$color = $this->mysqli->real_escape_string($color);
return 'Pranesimas2';
$room_id = $this->mysqli->real_escape_string($room_id);
$query = 'INSERT INTO chat (posted_on, account, message, color, room_id)' .
' VALUES (NOW(), "'.$user_name.'", "'.$message.'", "'.$color.', "'. $room_id .'")';
$result = $this->mysqli->query($query);
return 'Pranesimas';
$result->close();
}
Try add return step by step for detect where is problem and let me know about result.
Upvotes: 0