Execute sql query from sql file

后端 未结 4 909
野的像风
野的像风 2021-01-07 10:09

I want to include all tables from a database \"shop\" to mydatabase newshop. I export that \"shop\" database, and now it is named as shop.sql. Is t

4条回答
  •  一个人的身影
    2021-01-07 11:10

    Considering this question is tagged with PHP, here's how you could do it:

    mysql_query('USE newshop');
    $queries = file('shop.sql');
    while(list($i, $query) = each($queries)){
        $query = trim($query, ';');
        if(!empty($query) && $query != 'USE shop'){
            @mysql_query($query);
        }
    }
    

    Make sure "USE shop" in the if statement matches whatever syntax is used in the SQL file. For instance, you might need to make it USE 'shop' or USE schemaname.shop. Or, if there's no USE command in there at all, you can just take that condition right out.

提交回复
热议问题