简体   繁体   English

php类无法正常工作

[英]php Class not working functions

I got some strange problems. 我遇到了一些奇怪的问题。 Using my class i connecting to mysql, connection is successful but when i call a function its not works. 使用我的类我连接到mysql,连接成功但是当我调用一个函数时它不起作用。 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'; 我在调用函数之前使用了echo并且在页面没有查杀之后(这意味着php工作成功)mysql查询我已经检查了它的工作但是当我使用返回'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我用来调试:

<?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;
?>
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;
    }
}

About 'var' in PHP5 and upper. 关于PHP5和上层的'var' It's for declaring class member variables in PHP4, and is no longer needed. 它用于在PHP4中声明类成员变量,不再需要它。 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. 它将在PHP5中运行,但会在PHP中从版本5.0.0到版本5.1.2引发E_STRICT警告,因为它已被弃用。

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. 尝试逐步添加返回以检测问题所在并让我知道结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM