Good evening, I have a problem when i select some data from table. So, first, i call a select that return the last row of the table. After this, i insert new data in that table, and now call that select again and give me the same result of the first select. What i want is, when i insert, the last select returns the data inserted. If i execute another select already give me the last value inserted. My code is written in PHP and i use the interface mysqli. I already use transactions, refresh, set the cahce of mysqli to 0 in php.ini. Thanks.
EDITED 2: I make a test:
<?php
include 'conf.php';
$query="INSERT INTO assinante.comentarios (id_cmnt,id_user,comentario,now_data) values (null,4,'coiso',NOW());";
$res= mysqli_query($link,$query);
$query="SELECT * FROM $db_assin.comentarios;";
$res= mysqli_query($link,$query);
$linha = mysqli_fetch_assoc($res);
print_r($linha);
$query="INSERT INTO assinante.comentarios (id_cmnt,id_user,comentario,now_data) values (null,3,'ola',NOW());";
$res= mysqli_query($link,$query);
$query="SELECT * FROM $db_assin.comentarios;";
$res= mysqli_query($link,$query);
$linha = mysqli_fetch_assoc($res);
print_r($linha);
And it return:
Array ( [id_cmnt] => 29 [id_user] => 3 [comentario] => coiso [now_data] => 2014-01-29 02:10:24 ) Array ( [id_cmnt] => 29 [id_user] => 3 [comentario] => coiso [now_data] => 2014-01-29 02:10:24 )
It seems that save in buffer or something like this. If you can help i appreciate.
EDITED: The code
function getComentarios(){
include 'conf.php';
$query =
" SELECT utili.username, cmnt.comentario , cmnt.now_data , cmnt.id_cmnt
FROM ".$db_assin.".comentarios cmnt, ".$db_assin.".utilizadores utili
WHERE utili.id_user = cmnt.id_user
ORDER BY cmnt.id_cmnt DESC
LIMIT 1;";
$resultado = mysqli_query($link,$query);
if($resultado){
$linha = mysqli_fetch_array($resultado);
$json = json_encode($linha);
echo $json;
}
else{
echo mysqli_error($link);
}
mysqli_free_result($resultado);
}
function ins_comment($id_user,$comment){
include 'conf.php';
$data = date('Y-m-d H:i:s');
mysqli_autocommit($link, FALSE);
mysqli_query($link,"BEGIN");
$query_ins = "INSERT INTO ".$db_assin.".comentarios (id_cmnt,id_user,comentario,now_data) "
. "values (null,$id_user,'$comment',NOW());";
if (!$res_ins = mysqli_query($link,$query_ins)){
echo mysqli_error ($link);
mysqli_rollback($link);
}
else{
mysqli_commit($link);
}
mysqli_autocommit($link, TRUE);
mysqli_refresh($link, MYSQLI_REFRESH_HOSTS);
return $res_ins;
}
function comentar(){
include 'conf.php';
$user_c=$_POST['user_c'];
$comment=$_POST['comment'];
$query = "SELECT u.id_user "
. "FROM ".$db_assin.".utilizadores u "
. "WHERE u.username='$user_c';";
if($resultado = mysqli_query($link, $query)){
if (($linha = mysqli_fetch_array($resultado, MYSQLI_ASSOC)) != NULL){
$id_user=$linha['id_user'];
$query = "SELECT c.comentario "
. "FROM ".$db_assin.".utilizadores u, ".$db_assin.".comentarios c "
. "WHERE u.id_user=c.id_user AND u.id_user='$id_user';";
if($resultado = mysqli_query($link, $query)){
if (($linha = mysqli_fetch_array($resultado, MYSQLI_ASSOC)) != NULL){
if(strcmp($linha['comentario'],$comment)!=0){
if($res=ins_comment($id_user, $comment))
echo "Comentário submetido";
}
else{
echo "O mesmo comentário não pode ser submetido duas vezes!";
}
}
else{
if($res=ins_comment($id_user, $comment))
echo "Comentário submetido";
}
}
}
mysqli_free_result($resultado);
}
else echo mysqli_error($link);
}
Your second query doesn't have an order by
. Try
SELECT c.comentario
FROM ".$db_assin.".utilizadores u, ".$db_assin.".comentarios c "
WHERE u.id_user=c.id_user AND u.id_user='$id_user'
ORDER BY c.id_cmnt DESC
LIMIT 1;
By the way, you should learn to use explicit join syntax. This query is better written as:
SELECT c.comentario
FROM ".$db_assin.".utilizadores u join
".$db_assin.".comentarios c
on u.id_user = c.id_user
WHERE u.id_user = '$id_user'
ORDER BY c.id_cmnt DESC
LIMIT 1;
i tried really hard on this and i find the problem. I'm using websockets in php and jquery and in the code the mensage to websocket was writted before the post but it seems run after this. So i find for o function that send the message before the post and i discover the $.when(). Now i got this. One more time thanks for your support :)