FewG's avatar
Level 1

How to get MYSQL version

Hello,

is there a way to get the MYSQL version like that

mysqli_get_server_info()
0 likes
7 replies
bashy's avatar
bashy
Best Answer
Level 65

What about doing a RAW query for the version?

SHOW VARIABLES LIKE "%version%";
3 likes
Chris1904's avatar

Sorry for bringing this up again, but I found myself googling to this answer multiple times and found this solution to be easier:

$results = DB::select( DB::raw("select version()") );
$mysql_version =  $results[0]->{'version()'};
$mariadb_version = '';

if (strpos($mysql_version, 'Maria') !== false) {
    $mariadb_version = $mysql_version;
    $mysql_version = '';
}

Now, this will check whether MariaDB is used or MySQL with Laravel.

6 likes
mccubbin's avatar

$mysql = DB::select('select version()')[0]->{'version()'};

2 likes
mvnrsa's avatar

Since Laravel 9 you can use the DB::scalar() method:

$mysql_version = DB::scalar('select version()');
2 likes
ZermattChris's avatar

@mvnrsa Just what I was looking for -- mucho thanks :)

  <ul class="text-center mb-2">
    @php
      $results = DB::scalar('select version()');
    @endphp
    <li>PHP: {{ phpversion() }}</li>
    <li>Laravel: {{ app()->version() }}</li>
    <li>MySQL: {{  print_r($results) }}</li>
  </ul>
Tray2's avatar

@ZermattChris It's considered bad practice to have calls to the database inside your blade views, I suggest moving that call to your controller instead and pass the variable from there.

Please or to participate in this conversation.