in messaging system i don't know how to view message - php

i am new in php i want to know how to view message
this is my code
$idto = $_GET['id'];
$query2 = mysql_query("select * from users where id =".$idto." ");
$row2 = mysql_fetch_assoc($query2);
echo "<b><font size='6' face='Comic Sans MS'><center>".$row2['firstname']."</center></font></b><br><br>";
$query2 = mysql_query("select * from message where id_from =".$idto." and id_to =". $_SESSION['id']." ORDER BY id");
$query1 = mysql_query("select * from message where id_to =".$idto." and id_from =". $_SESSION['id']." ORDER BY id");
i select message send by user1 & by user 2 but i dont know how to view them correctly
when i use this
while ($row = mysql_fetch_assoc($query2) )
{
echo'<fieldset style="width:250" align="center">';
echo"<legend><b>".$row2['firstname']."</b></legend>";
echo $row['message'];
echo "<br>";
echo $row['date'];
echo"<hr>";
echo"</fieldset>";
}
while ($row1 = mysql_fetch_assoc($query1) )
{
echo'<fieldset style="width:250" align="center">';
echo"<legend><b>".$_SESSION['firstname']."</b></legend>";
echo $row1['message'];
echo "<br>";
echo $row1['date'];
echo"<hr>";
echo"</fieldset>";
}
it view messages but in incorrectly way
can any one help me please thanks :)
if you dont understand what i am saying
i mean i want to know how to view message

Firstly switch from mysql_* to mysqli_* because mysql_* functions are deprecated.
To order your queries in ascending order:
$query1 = mysql_query("
SELECT *
FROM message
WHERE id_to = ".$idto."
AND id_from = ".$_SESSION['id']."
ORDER BY id ASC
");
$query2 = mysql_query("
SELECT *
FROM message
WHERE id_from = ".$idto."
AND id_to = ".$_SESSION['id']."
ORDER BY id ASC
");

Related

Displaying based on Results in Mysql Query

I have an orders table in mysql and in that for some orders I set particular order status like 'review'.
I want to setup a way if any order placed by a particular customer(first and last name) for whom i have previously set order status as 'review' to display a warning in the list.
$sql = "select * from order where firstname = ".$firstname." AND lastname = ".$lastname." AND order_status = 'review';";
$SQLresult = mysql_query("$sql", $DBcon_MySQL);
while($row = mysql_fetch_array($SQLresult)) {
foreach($row as $row){
$result = "warning!";
echo $result;
}
}
The above code does not display anything. please let me know how to fix this.
[EDIT After Applying Answer]
This is how i am using it.
<td width="200">
<?
$sql = "select * from cust_order where firstname = '$firstname' AND lastname = '$lastname' AND order_status = 'review'";
$SQLResult = mysql_query("$sql", $DBcon_MySQL);
while($row = mysql_fetch_array($SQLResult )) {
//$result;
foreach($row as $row ){
//$result="";
$result = "Warning!";
}
?>
<p><? echo $result;?></p>
<?} ?>
</td>
How should i insert a check that it should display warning only once No matter how many orders from single customer are marked as review, display warning only once?
try this,
$sql = "SELECT
*
FROM
`order`
WHERE
firstname = '$firstname' AND lastname = '$lastname' AND
order_status = 'review' LIMIT 1";
$SQLresult = mysql_query($sql, $DBcon_MySQL);
while($row = mysql_fetch_array($SQLresult)) {
foreach($row as $row){
$result = "warning!";
echo $result;
}
}
Please be informed that mysql functions are deprecated and not recommended. USE MySQLi or PDO instead. have a reference from following queries.
http://php.net/manual/en/book.mysqli.php
http://php.net/manual/en/book.pdo.php

sql statement select from DB limit didn't work

i have a question which is my limit statement didn't work i want the content select from database and limit the show content in 40 only but it didn't work
here is my SQL statement with php code
$chatroomID=$_GET['chatroomID'];
$userID = $_SESSION['id'];
$sql="SELECT * FROM chatroom_chat WHERE chatroom_id ='$chatroomID'";
$result1 = mysqli_query($connection, $sql) or die(mysqli_error($connection));
while ($row = mysqli_fetch_array($result1)) {
$chat = $row['chat_id'];
$sql3 ="SELECT * FROM (
SELECT * FROM chat WHERE id = '$chat' ORDER BY id DESC LIMIT 0,40
) sub
ORDER BY id ASC ";
$getChatData = mysqli_query($connection,$sql3) or die(mysqli_error($connection));
/*here have statement to get username*/
while($row3 = mysqli_fetch_array($getChatData)) {
echo "<div>all content</div>";
}
}
does my code have any syntax error? i no sure why it didn't work
SELECT * FROM (
SELECT * FROM chat WHERE id = '$chat' ORDER BY id DESC LIMIT 40
) sub
ORDER BY id ASC

PHP - how to select from a table one query

I got a table that contains ID, Names, message, and time, I want to select from the table only one message query from each ID, Currently I select all the messages, this is my code
$query= mysql_query("SELECT * FROM `table` ORDER BY `time`")or die(mysql_error());
while($arr = mysql_fetch_array($query)){
$num = mysql_num_rows($query);
$msg = $arr['message'];
echo '</br>';
echo $msg;
}
That Shows all messages ordered by time, Is there is a way to select only one message query from each ID?
Thanks,
Klaus
If you want only one message you can use LIMIT like this
SELECT * FROM table ORDER BY time LIMIT 1
Or if you want only one message from some id then you can use GROUP BY
SELECT * FROM table ORDER BY time GROUP BY id
Sure, pretty straightforward
This will fetch all messages given ID:
$id = 10 //Get your id in any way you need
$query= mysql_query("SELECT `message` FROM `table` WHERE `ID` = $id")or die(mysql_error());
while($arr = mysql_fetch_array($query)){
$num = mysql_num_rows($query);
$msg = $arr['message'];
echo $msg;
}
and this will fetch only the first message given ID
$id = 10
$query= mysql_query("SELECT `message` FROM `table` WHERE `ID` = $id LIMIT 1")or die(mysql_error());
$row = mysql_fetch_array($query));
if($row){
$msg = $row['message'];
echo $msg;
}else{
echo 'no messages for id '.$id;
}

MySQL show results with inverted order?

I need to get the last 5 results, that's why I order them by Date DESC but I need to display the results from older to newer. How can I do this?
$data = mysql_query("SELECT * FROM Badges WHERE UID = '$user' ORDER by Date DESC LIMIT 5");
while($row = mysql_fetch_array( $data ))
{
print "<img class='badge' title='" . $row['Site'] . "' src='http://getfavicon.appspot.com/http://" . $row['Site'] . "?defaulticon=1pxgif' />";
}
$results = array();
while($row = mysql_fetch_array($data)) {
$results[] = $row;
}
$results = array_reverse($results);
foreach ($results as $row) {
echo $row['Site']; // etc
}
Manual link: http://php.net/function.array-reverse.php
$data = mysql_query("
SELECT * FROM (
SELECT * FROM Badges WHERE UID = '$user' ORDER by Date DESC LIMIT 5)
t ORDER BY Date
");
Also:
Please use mysql_real_escape_string (http://at2.php.net/manual/en/function.mysql-real-escape-string.php) for the variable $user. Depending on where you get that from it could be a security leak through sql injection.
You can actually order a second time in the same query.
I assume that you have an auto incrementad id (I'll call it 'EntryId' in this example) and then you hopefully should get what you need?
SELECT * FROM Badges WHERE UID = '$user' ORDER by Date DESC, EntryId ASC LIMIT 5

Show all data in column

I am trying to show all of the data in the 'status' column of my table but am having troubles. What am I doing wrong:
<?php
$query1 = "SELECT id, status FROM alerts WHERE customerid='".$_SESSION['customerid']."' ORDER BY id LIMIT $start, $limit ";
$result = mysql_query($query1);
while ($row = mysql_fetch_array($result))
{
echo $row['status'] ;
}
?>
Try this:
$query1 = "SELECT id, `status` FROM alerts WHERE customerid='".$_SESSION['customerid']."' ORDER BY id LIMIT $start, $limit ";
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo $row['status'];
}
Also, make sure that:
$_SESSION['customerid'], $start and $limit are not empty. You can test the constructed query with echo $query1;
Note: Addition of mysql_error() in in the mysql_query will allow you to see if there is an error in the query.
I am trying to show all of the data in
the 'status' column of my table
If you want to show all the rows, your query should be:
$query1 = "SELECT id, `status` FROM alerts ORDER BY id";
But if you want to show for a specific customer, your query should be:
$query1 = "SELECT id, `status` FROM alerts WHERE customerid='".$_SESSION['customerid']."' ORDER BY id";

Categories