Problem with Join query PHP

Asked

Viewed 86 times

1

Good,

And the next I have a query JOIN that makes me the query to 3 tables, that searches me in the bank all the existing notifications of the followers of the currently logged user. The problem is that I was now trying to fetch the information from the user who sent the notification, to show in the notification but the query stops returning me data. From what you can see it returns 500 error server.

Code

header('Cache-Control: no-cache, must-revalidate'); 
//Alteramos o cabeçalho para que o retorno seja do tipo JSON 
header('Content-Type: application/json; charset=utf-8');

session_start();

require_once("../funcoes/funcoes.php");

$user_actual = $_REQUEST['user_logado'];


$result_notificacoes = $conexao->prepare("SELECT * FROM notificacoes as noti 
                                    LEFT JOIN notificacoes_visualizacao as noti_vi ON noti_vi.id_notificacao = noti.id 
                                    INNER JOIN users_social as user ON user.id = noti.user_id
                                    WHERE noti.user_id IN (SELECT seg.followed FROM seguidores as seg WHERE seg.follower = :user_actual) 
                                    AND noti_vi.id_usuario_que_visualizou is NULL 
                                    OR noti.user_destino_notificacao = :user_atual2");                                  

$result_notificacoes->bindValue(':user_actual', $user_actual, PDO::PARAM_INT);
$result_notificacoes->bindValue(':user_atual2', $user_actual, PDO::PARAM_INT);
$result_notificacoes->execute();

$return = array();

$notificacoes = $result_notificacoes->fetchAll(PDO::FETCH_ASSOC);

foreach ($notificacoes as $row_notificacoes) {

    $sql = $conexao->prepare("INSERT INTO notificacoes_visualizacao (id_notificacao, id_usuario_que_visualizou) VALUES (:id_notificacao, :id_usuario_que_visualizou)");
    $sql->bindValue(':id_notificacao', $row_notificacoes['id'], PDO::PARAM_INT);
    $sql->bindValue(':id_usuario_que_visualizou', $user_actual, PDO::PARAM_INT); 
    $sql->execute();

$return[] = '
    <table border="0" class="barra_notificacoes" cellpadding="0" cellspacing="0">
        <tr>
            <td valign="top" width="10%">
                <div style="margin-left: 4px; margin-top: 5px;"><img width="50" height="50" src="images/avatar6.jpg"></div>
            </td>
            <td valign="top" width="50%">
                <div style="margin-left: 5px; margin-top: 2px;"><a href="users/<?= $row_user_anex->slug; ?>">César Sousa</a></div>
                <div style="margin-left: 5px;" class="comentario_user">Comentou a tua opinião!</div>
                <div style="margin-left: 5px;" class="tempo_do_post">1 segundo atrás</div> 
            </td>
        </tr>
    </table>
    ';                  
}
echo json_encode($return); 

The problem is when I add this line, the query stops working

 INNER JOIN users_social as user ON user.id = noti.user_id

Error log

 sabeonde/public_html/ajax/mostra_notificacoes.php:22
 Stack trace:
 #0 /home/sabeonde/public_html/ajax/mostra_notificacoes.php(22): PDOStatement->execute()
 #1 {main}
 thrown in /home/sabeonde/public_html/ajax/mostra_notificacoes.php on line 22

Error of the Try

 constraint fails (`sabeonde_sabeonde`.`notificacoes_visualizacao`, CONSTRAINT     `fk_notificacao_visualizacao_notificacao` FOREIGN KEY (`id_notificacao`) REFERENCES  `notificacoes` (`id`))' in /home/sabeonde/public_html/ajax/mostra_notificacoes.php:41
 Stack trace:
 #0 /home/sabeonde/public_html/ajax/mostra_notificacoes.php(41): PDOStatement->execute()
 #1 {main}
  • What does "crashes" mean? Returns no results? Returns wrong results? What is the structure of the tables involved? Describe what you want to obey with the query.

  • I edited the question and explained it better and put all the code

  • Thanks for the details. In case of error 500, check the PHP error log. The path of this file varies from distro to distro, and from hosting to hosting. The default path in Centos, for example, is /var/log/httpd/error_log. Add the relevant log snippet to the question, please.

  • had check and accuses the following error I will put up in the post

  • Put the remote $result_notificacoes->execute(); inside a block try-catch to see which message is associated with the generated exception. Please see this page for more details on how to do this: http://php.net/manual/en/class.pdoexception.php Please add the message to the question if you cannot understand it.

  • I put the error in the message I do not understand what whispers

  • I’m not an expert on Mysql. Apparently, some foreign key integrity restriction that Mysql should have checked earlier is failing. That means your database is inconsistent. You will have to analyze where the integrity is failing with the use of a tool like Mysql Workbench. The error message tells you the table and which restriction is in trouble, so it shouldn’t be too difficult. Once this is done, the problem must be solved, or at least the error 500 must stop happening.

  • but if I take out the Internet table users_social it already gives well

  • Yes because in this case Mysql does not go through the integrity failure, and therefore does not generate the error. But you also do not get the expected result. Generally speaking integrity restrictions have been created to be respected. Mysql is famous for having problems with this. Either you fix the faults by modifying your code, or you exchange DBMS, or you live with bizarre errors that will affect the stability of your application.

  • So let’s see if I got this and Mysql problem ?

  • Yes and no. The problem (integrity failure) was caused by a Mysql failure or a configuration error of yours, but this was before at the time of data entry. Now, this problem is appearing at the moment that Mysql will perform the JOIN products. Which storage engine are you using (Myisam, INNO,...)? Choosing it influences Mysql integrity restrictions support.

  • For tables using foreign key I am using INNO

  • I do not know what the behavior when mixing INNO with Myisam, and the referenced tables are in Myisam and those that reference are in INNO. That’s probably the problem. Use INNO at all, or check the documentation (I searched and did not find) something that supports you on mixing the two and still maintain integrity.

  • I’m downloading Mysql Workbench to see what’s missing

  • In your opinion what would be the database solution for a project that will have a large volume of users and content

  • In the systems I develop, data volume and integrity is fundamental, so I always use Postgresql or Oracle, depending on the client. Mysql has already had many problems, until we moved all systems from the company to Postgre (Oracle is client’s option due to cost), and since the change, our problems have been absolutely zero. We have never had a single problem and in terms of performance there have been no changes. Also, Postgre has the advantage of following patterns (unlike the mess that Mysql), and talks super well with Oracle. So my opinion is Postgre.

Show 12 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.