3

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;
?>
16
  • <cringe> Using MySQLi, but still escaping fields and building the query by injection Commented Jan 28, 2014 at 8:24
  • what are the errors you are receiving ? if errors turned up Commented Jan 28, 2014 at 8:26
  • 1
    What do you mean by "the function doesn't work". What do you expect to happen and what exactly happens? Also having anything after the return statement in a function isn't very useful. Commented Jan 28, 2014 at 8:28
  • error allways turned up and its returns nothing. this function must insert message into table and return this message back. but nothing happens and no errors i can catch. Commented Jan 28, 2014 at 8:32
  • "'.$color.', should be "'. $color .'" - regardless, if an MySQL insert isn't working, always attempt to get the Query String and try yourself in PHPMyAdmin or your MySQL Command line, then you'll see errors Commented Jan 28, 2014 at 8:44

2 Answers 2

2
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;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Now i got error in my destructor but when i have removed it it work only return now not working. Can you explain what the point of problem was?
I do believe the only thing that was wrong with your code was the SQL Query, it was being escaped by " rather than single quotations.
Secondly, $result->close() might be throwing an error, thus it's not returning correctly. As you'd use $result->close() if it were in a loop normally (I believe).
0

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.

4 Comments

i have user this var for some testing. now i'll remove at all this var.
if return is succes delete. Trouble be there are where return not work
i dont get any return :(
maybe problem with my php configuration o etc.?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.