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

iamamirsalehi's avatar

Mysql query does not work

Hey there. Is there any problem with my query that does not work?

"mysql -u root -p test < 'C:\Users\Amir\Desktop\package\storage\app\sql-tmp\db-dumps\mysql-test.sql'"
0 likes
15 replies
Sinnbeck's avatar

Are you running it on a an ubuntu shell? Or windows commandline?

Sinnbeck's avatar

You can try going to the directory first to see if that works

cd C:\Users\Amir\Desktop\package\storage\app\sql-tmp\db-dumps
mysql -u root -p test < mysql-test.sql
Tray2's avatar

You can't use php in a SQL file just pure SQL and PL/SQL.

iamamirsalehi's avatar

@sinnbeck I wanna run a MySQL query inside PHP code (That's not a PHP pure code). Does Laravel have any way to run that?

@tray2 Yeah I know that, thanks.

Tray2's avatar

What is it you are trying to achieve?

Is it restoring a backup file or is it just running pure SQL in php?

Tray2's avatar

Then you should not use any php at all. just run a sql backup like @sinnbeck said and you should be good. Or use the batabase program of your choice to run it.

So if you have edited your backup file remove all the changes you made.

iamamirsalehi's avatar

@tray2 Let me explain to you what am I doing exactly. I'm writing a package that when you fresh your migrations, it takes a backup of your database before fresh command runs, and at the end of the freshing migrations, it restores all the data. So your data is still in the tables.

this is the repository of the package and I committed the last changes. If it's possible for you please check it out and help me with what to do with it. https://github.com/iamamirsalehi/laravel-save-fresh

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102
DB::statement("mysql {$database['user']} {$database['pass']} {$database['db_name']} < '{$database['sqlFile']}'");

//try instead
$process = \Symfony\Component\Process\Process::fromShellCommandline("mysql {$database['user']} {$database['pass']} {$database['db_name']} < '{$database['sqlFile']}'");
$process->run();

https://symfony.com/doc/current/components/process.html

1 like
Sinnbeck's avatar

add

if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}

Please or to participate in this conversation.