PHP PDO Multiple queries doesnt get any value

后端 未结 2 1876
一个人的身影
一个人的身影 2021-01-29 05:54

I already did inner join before this code but then it seems like every time i have multiple queries it doesnt work. But then when I did it single query it seems fine and working

相关标签:
2条回答
  • 2021-01-29 06:42

    Although it's possible to run multiple queries in one call with PDO, there is not a single reason to do so. Neither your case is an exception. It will never work as you think. Instead of stuffing several queries in one call like this, you need a single query with JOIN.

    Another issue with your code is a cargo cult prepared statement. It looks like a real one but protects nothing. You should use parameters in your prepared query.

    $sql = "SELECT * FROM hr_details hd, personal_info pi WHERE 
            hd.personal_info_id=pi.personal_info_id AND hd.personal_info_id=?";
    $statement = $connection->prepare($sql);
    $statement->execute([$_POST["personal_info_id"]]);
    $result = $statement->fetchAll();
    
    0 讨论(0)
  • 2021-01-29 06:45

    Doesn't that work by doing this?

    SELECT * FROM hr_details hd, personal_info pi WHERE hd.personal_info_id=pi.personal_info_id AND personal_info_id = id
    

    You must be careful not to pass the variables directly in the SQL query, by using prepare

    http://php.net/manual/en/pdo.prepare.php

    $sql = 'SELECT * FROM hr_details hd, personal_info pi WHERE hd.personal_info_id=pi.personal_info_id AND personal_info_id = :id';
    $sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
    $sth->execute(array(':id' => $_POST["personal_info_id"]));
    $red = $sth->fetchAll();
    
    0 讨论(0)
提交回复
热议问题