Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

slowerua's avatar

Cannot move project from php to laravel.

Hi guys, currently i moving my project from clear php to laravel and i move 70% of my project. But i cant move statistics. All my attempts to move it are failed.

Here is the full code on php and pdo.

Please help me to move it.

<?php
header("Content-Type:text/html;charset=UTF-8");
$dsn = "mysql:dbname=db;host=localhost";
$user = 'user';
$password = 'pw';

try{
    $dbh = new PDO($dsn,$user,$password);
    $dbh->exec('SET NAMES utf8');
}
catch(PDOException $e){
    echo 'Error :'.$e->getMessage();
}

$update_online_sql = "SELECT COUNT(ID) FROM csstats";
$update_online_res = $dbh->query($update_online_sql);
$update_online=$update_online_res->fetch(PDO::FETCH_BOTH);
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
            <div class="content container">
              <table class="table table-hover table-bordered">
                  <thead>
                      <tr>
                          <th class="text-center">Name</th>
                          <th class="text-center">K</th>
                          <th class="text-center">D</th>
                          <th class="text-center">HS</th>
                          <th class="text-center">Skill</th>
                      </tr>
                  </thead>
                  <tbody>
                    <?                  
                    $sql = $dbh->query("SELECT * FROM csstats");
                    $res=$sql->fetch(PDO::FETCH_BOTH);

                    foreach($sql as $res){
                        
                        $sql2 = $dbh->query("SELECT * FROM skill");
                        $res2=$sql2->fetch(PDO::FETCH_BOTH);
                        
                                $skill = null;
                                foreach($sql2 as $res2){    
                                    if($skill!=null){
                                        continue;
                                    }
                                    if((int)$res2['max']>=(int)$res['skill']){
                                        $skill=$res2['title'];
                                    }
                                }               
                    ?>
                      <tr>
                          <td class="text-center"><?=$res['name'];?></td>
                          <td class="text-center"><?=$res['kills'];?></td>
                          <td class="text-center"><?=$res['deaths'];?></td>
                          <td class="text-center"><?=$res['hs'];?></td>
                          <td class="text-center"><? echo"<div class='$skill'>$skill</div>"; ?>
                          </td>
                      </tr>
                    <?
                    }
                    ?>
                  </tbody>
              </table>
                </div>
            </div>
        </div>
    </body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</html>
0 likes
1 reply
jlrdw's avatar
jlrdw
Best Answer
Level 75

In a controller you can put

use Illuminate\Support\Facades\DB;

and use getPdo()

Like

$sth = DB::connection()->getPdo()->prepare($sql);

With getPdo() you are using your normal PDO. All PDO is available including binding.

When using PDO statements it's necessary to backslash like

$update_online=$update_online_res->fetch(\PDO::FETCH_BOTH);

That could work, while you look for other solutions. But I use getPdo() on some complex queries anyway.

1 like

Please or to participate in this conversation.