简体   繁体   中英

How to setup phpmyadmin and SQL for messaging via REST-api?

In my database I have a table called "Chat". Here i intend to store every message that has been sent between users in my application. First i have to store them in a list (by just simply doing a SELECT * FROM ColumnText WHERE OneID = '$OneID' and gather all the ID's from other users i have had messages with) but i now struggle to finish the last part which is to see the messages between these two users once i click on a row.

My plan is to gather the info with a call that would look something like this:

http://myURL.com/Chat.php?UserOneID=1548&UserTwoID=1724

Where one user has ID 1548 and another 1724 .

The first issue I am seeing is that who will I know which one sent the message. What i then did and that could potentially work is to do something like this instead:

Rename the ID's to something more clear like this first SenderID and RecieverID instead where the data could look something like this:

SenderID = 1548 RecieverID = 1724

and

SenderID = 1724 RecieverID = 1548

I wrote code for it that looks like this where i use a UNION to try to gather the data for the two ID's together, not just one ID where the call would look something like this:

http://myURL.com/Chat.php?SenderID=1548&RecieverID=1724
http://myURL.com/Chat.php?RecieverID=1548&SenderID=1724


<?php

class ConnectionInfo 
{   
public $conn; 
public function GetConnection() {
  $this->conn = mysqli_connect("serv", "user","pass", "db") or die(mysqli_error($mysql_pekare));

 }
}

$connectionInfo = new ConnectionInfo();
$connectionInfo->GetConnection();

if (!$connectionInfo->conn)
{
echo 'No Connection';
}

else
{
    $SenderID = $_GET['SenderID'];
    $RecieverID = $_GET['RecieverID'];

    $query = "(SELECT * FROM ColumnText WHERE SenderID = '$SenderID') UNION (SELECT * FROM ColumnText WHERE RecieverID = '$RecieverID')";

    $stmt = mysqli_query($connectionInfo->conn, $query);

    if (!$stmt)
    {
        echo 'Query failed';
    }

    else
    {
        $contacts = array(); 
        while ($row = mysqli_fetch_array($stmt)) 
        {

        $contact = array("Messages" => $row['Messages']);

        array_push($contacts, $contact);         
        }
        echo json_encode(array('results' => $contacts), JSON_PRETTY_PRINT);
    }

}

?>

The problem with this is that "UNION" does not seem to do the trick. I only seem to get all the data from the SenderID row even though the RecieverID is not matching with the one i send to the script.

Is my current approach the correct way and if so, how would i need to adjust my phpscript in order for this to work?

Why don't you just use an OR ? So the results have the correct receiver and the correct sender? And you got all messages from X to Y and all messages from Y to X

Your query line would look like this:

 $query = "SELECT * FROM ColumnText WHERE (SenderID = '$SenderID' AND RecieverID = '$RecieverID') OR (SenderID = '$RecieverID' AND RecieverID = '$SenderID')“;

Furthermore i strongly recommend you to use prepared statements

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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