За последние 24 часа нас посетили 7930 программистов и 449 роботов. Сейчас ищут 106 программистов ...

Вывести значение message_text and user_current с DataBase в виде массивы

Тема в разделе "PHP для новичков", создана пользователем Harry_Thomson, 28 июн 2020.

  1. Harry_Thomson

    Harry_Thomson Новичок

    С нами с:
    28 июн 2020
    Сообщения:
    2
    Симпатии:
    0
    <?php

    class Config{

    private $servername = "localhost";
    private $username = "af";
    private $password ="00000";
    private $db ="chatdb";
    private $error=[];
    private $connet;

    private function mysql_connect(){

    $con = mysqli_connect($this->servername,$this->username,$this->password,$this->db);

    if (mysqli_connect_errno()) {

    array_push($this->error,"Connection Failed: " . mysqli_connect_error());
    return false;

    }else{
    $this->connet = $con;

    }
    }
    public function PrintError(){
    echo "<pre>";
    print_r($this->error);
    echo "</pre>";
    }

    public function MysqlInsert($query){
    $this->mysql_connect();
    $mysqlquery = $this->connet;

    if ($mysqlquery->query($query) === TRUE) {

    $mysqlquery->close();
    return true;
    } else {
    array_push($this->error,"Error: " . $query . "<br>" . $mysqlquery->error);
    $mysqlquery->close();

    return false;
    }


    }
    public function ReturnArray($query){
    $this->mysql_connect();
    $conn = $this->connet;

    $query = "SELECT message_text, user_current, created_at FROM chat";
    $result = $conn->query ($query) or die(mysqli_connect_error());

    if($row = $result > 0){
    while ($row = $result->fetch_array($result, MYSQLI_BOTH)) {
    return array ("ID: %s Name: %s ", $row[0], $row["user_current"], $row["message_text"]);
    }
    } else {
    echo ("No messages yet");

    }

    }
    }
    ?>

    Код message.php

    <?php
    session_start();
    include "config.php";
    $config = new Config();
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $msg = $_POST["message"];
    $name = $_SESSION["user"];
    $buttontype = $_POST["ClickedButton"];


    $sql = "INSERTINTO chat(message_text,user_current,chat_group_id) VALUES ('$msg','$name',123456)";

    $result = $config->MysqlInsert($sql);
    if($result){
    echo "added ";
    }
    else{
    $config->PrintError();
    }
    }
    ?>
    <!DOCTYPEhtml>
    <htmllang="en">
    <head>
    <metacharset="UTF-8">
    <metaname="viewport"content="width=device-width, initial-scale=1.0">
    <title>Chat</title>
    <linkrel="stylesheet"href="../Chat/style/stylephp.css">
    </head>
    <body>
    <divclass="msg_box">
    <h3><?php echo $_SESSION["user"]?> - Online</h3>
    <divclass="msg_output">
    </div>
    <formmethod="post">
    <textareaclass="form_textarea"name="message"id="msg"cols="30"
    rows="5"placeholder="Write something" ></textarea>

    <br>
    <inputtype="submit"name="ClickedButton"value="Send">

    <ahref="../Chat/logout.php">
    <inputtype="submit"name="ClickedButton"value="LogOut">
    </a>
    </form>
    </div>
    </body>
    </html>
     
  2. MouseZver

    MouseZver Суперстар

    С нами с:
    1 апр 2013
    Сообщения:
    6.376
    Симпатии:
    1.018
    Адрес:
    Лень