MYSQL PHP分页不起作用的链接

时间:2015-10-02 09:10:16

标签: php mysql pagination

我正在尝试分页,我有下一个代码,但我无法让它工作。

fatal: [web] => SSH encountered an unknown error. The output was:
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /Users/smcgurk/.ssh/config
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: auto-mux: Trying existing master
debug1: Control socket "/Users/smcgurk/.ansible/cp/ansible-ssh-192.168.253.204-22-vagrant" does not exist

问题是我得到它的分页但它显示正确的链接。所以我认为问题出现在下一个代码中:

<?php   
    $link = mysqli_connect("localhost", "", " ", "");   

    if($link === false) { die('<span class="rosu">EROARE:</span> Nu s-a putut realiza conexiunea la baza de date.<br/><br/>Va rog verificati conexiunea pentru baza de date.<br/>' . mysqli_connect_error()); }

    $num_rec_per_page=10;
    if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
    $start_from = ($page-1) * $num_rec_per_page; 

    $sql = "SELECT * FROM My_Contracte WHERE contract_sters='nu' ORDER BY id_contract DESC LIMIT $start_from, $num_rec_per_page";
    $rs_result = mysql_query ($sql); //run the query
    if($result = mysqli_query($link, $sql)){
        if(mysqli_num_rows($result) > 0){
            echo '<table id="contracte">';           
            echo "<tr>";
            echo "<th>EDT</th>"; 
            echo "<th>Numar Contract</th>";
            echo "<th>C</th>";
            echo "<th>Data Realizare</th>";
            echo "<th>Nume Firma</th>";     
            echo "<th>Nume Locatie</th>";   
            echo "<th>Zona Judet</th>";
            echo "<th>Servicii</th>";
            echo "<th>Suma Plata</th>";     
            echo "<th>PER. CTR.</th>";
            echo "<th>Nume Contact</th>";
            echo "<th>Telefon</th>";                
            echo "<th>E</th>";
            echo "<th>T</th>";          
            echo "<th>W</th>";
            echo "<th>DEL</th>";
            echo "</tr>";        

        while($row = mysqli_fetch_array($result)){   
            echo "<tr>";
            echo "<td class='editbutton'><a href='modifica.php?ID=" . $row['id_contract'] . "'><img src='images/edit.png'></img></a></td>";
            echo '<td><a href="vezi.php?contract=' . $row['id_contract'] . '&firma=' . $row['nume_firma'] . '&numar=' . $row['numar_contract'] . '">' . $row['numar_contract'] . ' <img src="images/link.png" width="14px"></img></a></td>';
            echo "<td><a href='pdf/" . $row['link_contract_pdf'] . "' target='_blank'><img src='images/pdf.png' width='20px'></img></a></td>";
            echo "<td>" . $row['expirare_contract'] . "</td>";          
            echo "<td>" . $row['nume_firma'] . "</td>"; 
            echo "<td>" . $row['nume_locatie'] . "</td>"; 
            echo "<td>" . $row['zona_judet_oras'] . "</td>"; 
            echo "<td>" . $row['servicii_oferite'] . "</td>"; 
            echo "<td>" . $row['suma_de_plata'] . "</td>"; 
            echo "<td>" . $row['perioada_contract'] . "</td>"; 
            echo "<td>" . $row['nume_contact'] . "</td>"; 
            echo "<td>" . $row['telefon_contact'] . "</td>";    
            echo '<td><a href="mailto:' . $row['email_contact'] . '?Subject=Contract%20Info3D" target="_blank"><img src="images/email.png"></img></a></td>';       
            echo '<td><a href="http://' . $row['link_turvirtual'] . '"><img src="images/turvirtual.png"></img></a></td>'; 
            echo '<td><a href="http://' . $row['adresa_website'] . '"><img src="images/website.png"></img></a></td>';
            echo "<td class='deletebutton'><a href=sterge.php?ID=" . urlencode(str_replace(' ', '-', $row['id_contract'])) . "&NR=" . urlencode(str_replace(' ', '-', $row['numar_contract'])) . "&DT=" . urlencode(str_replace(' ', '-', $row['expirare_contract'])) . "&FR=" . urlencode(str_replace(' ', '-', $row['nume_firma'])) . "&ZN=" . urlencode(str_replace(' ', '-', $row['zona_judet_oras'])) . "&PL=" . urlencode(str_replace(' ', '-', $row['servicii_oferite'])) . "><img src='images/delete.png'></img></a></td>";
            echo "</tr>";      
        }     

        echo "</table>";      

        mysqli_free_result($result);    
        } else { print ('<span class="rosu">EROARE!</span> Nu am gasit contracte in baza de date.'); }  
        } else { print ('<span class="rosu">EROARE!</span> Nu s-a putut executa comada de listare a contractelor.<br/><br/><b>Motivul pentru care nu s-a putut accesa tabelul: </b>') . mysqli_error($link); print '.'; }

?>

<?php 
$sql = "SELECT * FROM My_Contracte WHERE contract_sters='nu'"; 
$total_records = mysql_num_rows($sql);  //count number of records
$total_pages = ceil($total_records / $num_rec_per_page); 

echo "<a href='lista.php?page=1'><div class='pagina'>1</div></a> "; // Goto 1st page  

for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='lista.php?page=".$i."'><div class='pagina'>".$i."</div></a> "; 
}; 
echo "<a href='lista.php?page=$total_pages'><div class='pagina'>$total_pages</div></a> "; // Goto last page
?>

谢谢!

= 编辑:

<?php 
$sql = "SELECT * FROM My_Contracte WHERE contract_sters='nu'"; 
$total_records = mysql_num_rows($sql);  //count number of records
$total_pages = ceil($total_records / $num_rec_per_page); 

echo "<a href='lista.php?page=1'><div class='pagina'>1</div></a> "; // Goto 1st page  

for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='lista.php?page=".$i."'><div class='pagina'>".$i."</div></a> "; 
}; 
echo "<a href='lista.php?page=$total_pages'><div class='pagina'>$total_pages</div></a> "; // Goto last page
?>

我在error_log中收到此错误。

81行是:

[02-Oct-2015 12:18:57 Europe/Bucharest] PHP Warning:  mysql_num_rows() expects parameter 1 to be resource, string given in /lista.php on line 81

2 个答案:

答案 0 :(得分:0)

试试这个我猜你已经把第一页和最后一页的链接放在了一起,你又在循环中做了这个。

for ($i=2; $i<$total_pages; $i++) { 
        echo "<a href='lista.php?page=".$i."'><div class='pagina'>".$i."</div> </a> "; 
}; 

答案 1 :(得分:0)

我得到了这样的工作:

<?php 

    include("db.php");

    $sql = mysql_query("SELECT COUNT(id_contract) FROM My_Contracte WHERE contract_sters = 'nu'"); 
    $total_records = mysql_result($sql, 0); 
    $total_pages = ceil($total_records / $num_rec_per_page); 

    echo "<a href='lista.php?page=1'><div class='pagina'>1</div></a> "; 

    for ($i=2; $i<=$total_pages; $i++) { 
            echo "<a href='lista.php?page=".$i."'><div class='pagina'>".$i."</div></a> "; 
    }; 

?>