1

这个问题在这里还没有答案:PDO 语句返回 FALSE 1 个答案

上面的问题只是错误处理看到我得到的错误!(已经有错误处理)并且如果我在 phpmyadmin 中运行它们,我的所有查询都可以正常工作。

您好我正在尝试进入我的动态页面。但我收到警告:

警告:在第 137 行为 foreach() 提供的参数无效

我试图修复它,但我似乎可以让它工作。我试图改变

foreach($results as $row)

至:

foreach($row = $results->fetch(PDO::FETCH_ASSOC)) 

foreach($row = $results->fetchAll())

但比我得到的错误:

解析错误:语法错误,第 137 行出现意外的 ')'

我试图在互联网上找到答案,但我可以找到解决这个问题的答案。

有人知道我该如何解决这个问题吗?而我做错了什么......我尝试使用 PDO。我对 PHP 和 PDO 还是有点陌生​​。

我的代码:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<?php 
    include('php/loginHandlerconnect.php'); 

    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting(-1);

    $db = loginHandlerconnect(); 

    //$tableName="vacature";        
    $targetpage = "vacatureoverzicht";  
    $limit = 10; 

    $query = "SELECT COUNT(vacatureID) as num FROM vacature";
    $res = $db->query($query);
    $total_pages = $res->fetch(PDO::FETCH_ASSOC);
    $total_pages = $total_pages['num'];

    $stages = 3;
    $page = $db->quote($_GET['page']);
    if($page){
        $start = ($page - 1) * $limit; 
    }else{
        $start = 0; 
    }

    $sql = 'SELECT vacature.*, werkgever.image, werkgever.plaats, werkgever.provincie, werkgever.naamBedrijf FROM vacature LEFT JOIN werkgever ON (vacature.werkgeverID = werkgever.werkgeverID) ORDER BY vacature.datum DESC LIMIT '. $start .', '. $limit .''; 
    $results = $db->query($sql); 

    // Initial page num setup
    if ($page == 0){$page = 1;}
    $prev = $page - 1;  
    $next = $page + 1;                          
    $lastpage = ceil($total_pages/$limit);      
    $LastPagem1 = $lastpage - 1;                    


    $paginate = '';
    if($lastpage > 1)
    {   
        $paginate .= "<div class='paginate'>";
        // Previous
        if ($page > 1){
            $paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
        }else{
            $paginate.= "<span class='disabled'>previous</span>";   }

        // Pages    
        if ($lastpage < 7 + ($stages * 2))  // Not enough pages to breaking it up
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page){
                    $paginate.= "<span class='current'>$counter</span>";
                }else{
                    $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
            }
        }
        elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
        {
            // Beginning only hide later pages
            if($page < 1 + ($stages * 2))       
            {
                for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<span class='current'>$counter</span>";
                    }else{
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
                }
                $paginate.= "...";
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";       
            }
            // Middle hide some front and some back
            elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
            {
                $paginate.= "<a href='$targetpage?page=1'>1</a>";
                $paginate.= "<a href='$targetpage?page=2'>2</a>";
                $paginate.= "...";
                for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<span class='current'>$counter</span>";
                    }else{
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
                }
                $paginate.= "...";
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";       
            }
            // End only hide early pages
            else
            {
                $paginate.= "<a href='$targetpage?page=1'>1</a>";
                $paginate.= "<a href='$targetpage?page=2'>2</a>";
                $paginate.= "...";
                for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<span class='current'>$counter</span>";
                    }else{
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
                }
            }
        }

                // Next
        if ($page < $counter - 1){ 
            $paginate.= "<a href='$targetpage?page=$next'>next</a>";
        }else{
            $paginate.= "<span class='disabled'>next</span>";
            }

        $paginate.= "</div>";       
    }
    echo $total_pages.' Results';
    // pagination
    echo $paginate; 

    /*
    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting(-1);
    */
    /* 
    * Nu hebben we een resultset met de volgende data per row 
    * vacatureID 
    * werkgeverID 
    * functie 
    * datum 
    * omschrijvingKort 
    * omschrijving 
    * image (werkgever) 
    * plaats (werkgever) 
    * provincie (werkgever) 
    * naamBedrijf (werkgever) 
    */ 
    foreach($results as $row) // line 137
    { 
        //Clean record 
        cleanRecord($row);
        echo generateDiv($row); 
    }

    function cleanRecord($row) 
    { 
        if (strlen($row['functie']) > 20) { 

            // truncate string 
            $stringCut = substr($row['functie'], 0, 20); 

            // make sure it ends in a word so assassinate doesn't become ass... 
            $row['functie'] = substr($stringCut, 0, strrpos($stringCut, ' ')).'...'; 
        } 
        if (strlen($row['naamBedrijf']) > 25) { 

            // truncate string 
            $stringCut2 = substr($row['naamBedrijf'], 0, 14); 

            // make sure it ends in a word so assassinate doesn't become ass... 
            $row['naamBedrijf'] = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
        } 
        if (strlen($row['omschrijvingKort']) > 63) { 

            // truncate string 
            $stringCut2 = substr($row['omschrijvingKort'], 0, 63); 

            // make sure it ends in a word so assassinate doesn't become ass... 
            $row['omschrijvingKort'] = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
        } 

        return $row; 
    } 
    function generateDiv($row) 
    { 
        $date = date("d-m-Y", strtotime($row['datum'])); 
        return " 
            <div class='vacatureinfo2'> 
                <img class='userimg2' src='../../uploads/userimage/". $row['image'] ."' id='imge'/> 
                <p class='func-br-pl pull-left'>". $row['functie'] ." - ". $row['naamBedrijf'] ." - ". $row['plaats'] ."</p><p class='dtm pull-right'>". $date ."</p> 
                <div class='omschrijvingkort2'> 
                    ". $row['omschrijvingKort'] ." 
                </div> 
                <p class='pull-right'><a href='reactie?id=" . $row['vacatureID'] . "'>Reageer</a> | <a href='informatie-vacature?id=" . $row['vacatureID'] . "'>Meer informatie</a></p> 
            </div> 
            <hr> 
        "; 
    }
?>

更新:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<?php 
    include('php/loginHandlerconnect.php'); 

    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting(-1);

    $db = loginHandlerconnect(); 

    $targetpage = "vacatureoverzicht";  
    $limit = 10; 

    $query = "SELECT COUNT(vacatureID) as num FROM vacature";
    $res = $db->query($query);
    $total_pages = $res->fetch(PDO::FETCH_ASSOC);
    $total_pages = $total_pages['num'];

    $stages = 3;
    $page = $db->quote($_GET['page']);
    if($page){
        $start = ($page - 1) * $limit; 
    }else{
        $start = 0; 
    }

    $sql = $db->query('SELECT vacature.*, werkgever.image, werkgever.plaats, werkgever.provincie, werkgever.naamBedrijf FROM vacature LEFT JOIN werkgever ON (vacature.werkgeverID = werkgever.werkgeverID) ORDER BY vacature.datum DESC LIMIT '. $start .', '. $limit .''); 
    $results = $sql->fetchAll();

    // Initial page num setup
    if ($page == 0){$page = 1;}
    $prev = $page - 1;  
    $next = $page + 1;                          
    $lastpage = ceil($total_pages/$limit);      
    $LastPagem1 = $lastpage - 1;                    


    $paginate = '';
    if($lastpage > 1)
    {   
        $paginate .= "<div class='paginate'>";
        // Previous
        if ($page > 1){
            $paginate.= "<a href='". $targetpage ."?page=". $prev ."'>previous</a>";
        }else{
            $paginate.= "<span class='disabled'>previous</span>";   
        }

        // Pages    
        if ($lastpage < 7 + ($stages * 2))  // Not enough pages to breaking it up
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page){
                    $paginate.= "<span class='current'>". $counter ."</span>";
                }else{
                    $paginate.= "<a href='". $targetpage ."?page=". $counter ."'>". $counter ."</a>";}                  
            }
        }
        elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
        {
            // Beginning only hide later pages
            if($page < 1 + ($stages * 2))       
            {
                for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<span class='current'>". $counter ."</span>";
                    }else{
                        $paginate.= "<a href='". $targetpage ."?page=". $counter ."'>". $counter ."</a>";}                  
                }
                $paginate.= "...";
                $paginate.= "<a href='". $targetpage ."?page=". $LastPagem1 ."'>". $LastPagem1 ."</a>";
                $paginate.= "<a href='". $targetpage ."?page=". $lastpage ."'>". $lastpage ."</a>";     
            }
            // Middle hide some front and some back
            elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
            {
                $paginate.= "<a href='". $targetpage ."?page=1'>1</a>";
                $paginate.= "<a href='". $targetpage ."?page=2'>2</a>";
                $paginate.= "...";
                for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<span class='current'>". $counter ."</span>";
                    }else{
                        $paginate.= "<a href='". $targetpage ."?page=". $counter ."'>". $counter ."</a>";}                  
                }
                $paginate.= "...";
                $paginate.= "<a href='". $targetpage ."?page=". $LastPagem1 ."'>". $LastPagem1 ."</a>";
                $paginate.= "<a href='". $targetpage ."?page=". $lastpage ."'>". $lastpage ."</a>";     
            }
            // End only hide early pages
            else
            {
                $paginate.= "<a href='". $targetpage ."?page=1'>1</a>";
                $paginate.= "<a href='". $targetpage ."?page=2'>2</a>";
                $paginate.= "...";
                for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<span class='current'>". $counter ."</span>";
                    }else{
                        $paginate.= "<a href='". $targetpage ."?page=". $counter ."'>". $counter ."</a>";}                  
                }
            }
        }

                // Next
        if ($page < $counter - 1){ 
            $paginate.= "<a href='". $targetpage ."?page=". $next ."'>next</a>";
        }else{
            $paginate.= "<span class='disabled'>next</span>";
            }

        $paginate.= "</div>";       
    }
    echo $total_pages.' vacatures';

    /*
    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting(-1);
    */
    /* 
    * Nu hebben we een resultset met de volgende data per row 
    * vacatureID 
    * werkgeverID 
    * functie 
    * datum 
    * omschrijvingKort 
    * omschrijving 
    * image (werkgever) 
    * plaats (werkgever) 
    * provincie (werkgever) 
    * naamBedrijf (werkgever) 
    */ 
    foreach($results as $row) 
    { 
        //Clean record 
        cleanRecord($row);
        echo generateDiv($row); 
    }

    function cleanRecord($row) 
    { 
        if (strlen($row['functie']) > 20) { 

            // truncate string 
            $stringCut = substr($row['functie'], 0, 20); 

            // make sure it ends in a word so assassinate doesn't become ass... 
            $row['functie'] = substr($stringCut, 0, strrpos($stringCut, ' ')).'...'; 
        } 
        if (strlen($row['naamBedrijf']) > 25) { 

            // truncate string 
            $stringCut2 = substr($row['naamBedrijf'], 0, 14); 

            // make sure it ends in a word so assassinate doesn't become ass... 
            $row['naamBedrijf'] = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
        } 
        if (strlen($row['omschrijvingKort']) > 63) { 

            // truncate string 
            $stringCut2 = substr($row['omschrijvingKort'], 0, 63); 

            // make sure it ends in a word so assassinate doesn't become ass... 
            $row['omschrijvingKort'] = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
        } 

        return $row; 
    } 
    function generateDiv($row) 
    { 
        $date = date("d-m-Y", strtotime($row['datum'])); 
        return " 
            <div class='vacatureinfo2'> 
                <img class='userimg2' src='../../uploads/userimage/". $row['image'] ."' id='imge'/> 
                <p class='func-br-pl pull-left'>". $row['functie'] ." - ". $row['naamBedrijf'] ." - ". $row['plaats'] ."</p><p class='dtm pull-right'>". $date ."</p> 
                <div class='omschrijvingkort2'> 
                    ". $row['omschrijvingKort'] ." 
                </div> 
                <p class='pull-right'><a href='reactie?id=" . $row['vacatureID'] . "'>Reageer</a> | <a href='informatie-vacature?id=" . $row['vacatureID'] . "'>Meer informatie</a></p> 
            </div> 
            <hr> 
        "; 
    }

    // pagination
    echo $paginate; 
?>

更新 2:

将下面的代码更改为 PDO 时可能会出现问题?

<?php
include('connect.php'); 

$tableName="vacature";      
$targetpage = "testpaging2.php";    
$limit = 10; 

$query = "SELECT COUNT(vacatureID) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
    $start = ($page - 1) * $limit; 
}else{
    $start = 0; 
}   

// Get page data
$query1 = "SELECT vacature.*, werkgever.image, werkgever.plaats, werkgever.provincie, werkgever.naamBedrijf FROM vacature LEFT JOIN werkgever ON (vacature.werkgeverID = werkgever.werkgeverID) ORDER BY vacature.datum DESC LIMIT $start, $limit";
$result = mysql_query($query1);

// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;  
$next = $page + 1;                          
$lastpage = ceil($total_pages/$limit);      
$LastPagem1 = $lastpage - 1;                    


$paginate = '';
if($lastpage > 1)
{   
    $paginate .= "<div class='paginate'>";
    // Previous
    if ($page > 1){
        $paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
    }else{
        $paginate.= "<span class='disabled'>previous</span>";   }

    // Pages    
    if ($lastpage < 7 + ($stages * 2))  // Not enough pages to breaking it up
    {   
        for ($counter = 1; $counter <= $lastpage; $counter++)
        {
            if ($counter == $page){
                $paginate.= "<span class='current'>$counter</span>";
            }else{
                $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
        }
    }
    elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
    {
        // Beginning only hide later pages
        if($page < 1 + ($stages * 2))       
        {
            for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
            {
                if ($counter == $page){
                    $paginate.= "<span class='current'>$counter</span>";
                }else{
                    $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
            }
            $paginate.= "...";
            $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
            $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";       
        }
        // Middle hide some front and some back
        elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
        {
            $paginate.= "<a href='$targetpage?page=1'>1</a>";
            $paginate.= "<a href='$targetpage?page=2'>2</a>";
            $paginate.= "...";
            for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
            {
                if ($counter == $page){
                    $paginate.= "<span class='current'>$counter</span>";
                }else{
                    $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
            }
            $paginate.= "...";
            $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
            $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";       
        }
        // End only hide early pages
        else
        {
            $paginate.= "<a href='$targetpage?page=1'>1</a>";
            $paginate.= "<a href='$targetpage?page=2'>2</a>";
            $paginate.= "...";
            for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
            {
                if ($counter == $page){
                    $paginate.= "<span class='current'>$counter</span>";
                }else{
                    $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}                    
            }
        }
    }

            // Next
    if ($page < $counter - 1){ 
        $paginate.= "<a href='$targetpage?page=$next'>next</a>";
    }else{
        $paginate.= "<span class='disabled'>next</span>";
        }

    $paginate.= "</div>";       


}
 echo $total_pages.' Results';



    while($row = mysql_fetch_array($result))
    {

    echo "
        <div class='vacatureinfo2'> 
            <img class='userimg2' src='../../uploads/userimage/". $row['image'] ."' id='imge'/> 
            <p class='func-br-pl pull-left'>". $row['functie'] ." - ". $row['naamBedrijf'] ." - ". $row['plaats'] ."</p><p class='dtm pull-right'>". $date ."</p> 
            <div class='omschrijvingkort2'> 
                ". $row['omschrijvingKort'] ." 
            </div> 
            <p class='pull-right'><a href='reactie?id=" . $row['vacatureID'] . "'>Reageer</a> | <a href='informatie-vacature?id=" . $row['vacatureID'] . "'>Meer informatie</a></p> 
        </div> 
        <hr>";

    }
 // pagination
 echo $paginate;

因为当我运行这个页面时它可以工作!但它不是 PDO...查看此更新之前的代码以了解我如何将代码更改为 PDO 请注意我也在该代码中使用函数!如果我知道将其更改为 PDO 时我做错了什么,也许这会解决这个问题?但我不知道我做错了什么!!

4

1 回答 1

-1

尝试更新

$sql = 'SELECT vacature.*, werkgever.image, werkgever.plaats, werkgever.provincie, werkgever.naamBedrijf FROM vacature LEFT JOIN werkgever ON (vacature.werkgeverID = werkgever.werkgeverID) ORDER BY vacature.datum DESC LIMIT '. $start .', '. $limit .''; 
$results = $db->query($sql); 

$sql = $db->query('SELECT vacature.*, werkgever.image, werkgever.plaats, werkgever.provincie, werkgever.naamBedrijf FROM vacature LEFT JOIN werkgever ON (vacature.werkgeverID = werkgever.werkgeverID) ORDER BY vacature.datum DESC LIMIT '. $start .', '. $limit .''); 
$results = $db->fetchAll($sql);

实际上没有做 fetchAll 你的 $result 是一个 PDO 查询对象

而且foreach($row = $results->fetch(PDO::FETCH_ASSOC))您刚刚初始化了 $row 变量,您忘记在那里定义 $value 所以语法应该是foreach($array = $results->fetch(PDO::FETCH_ASSOC) as $row)虽然我不太确定我们是否可以$array = $results->fetch(PDO::FETCH_ASSOC)在 foreach 中使用

于 2015-10-14T09:29:09.827 回答