Running two SQL queries on one php page (SET + SELECT)

让人想犯罪 __ 提交于 2021-02-05 09:13:07

问题


I need translate to "pt_br" the currently month and this code are working like a charm on phpmyadmin:

SET lc_time_names = 'pt_BR';
SELECT MONTHNAME(CURDATE()) AS `Mes`  
                              FROM trabalho_v2 

Someone may help me use the "SET lc_time_names = 'pt_BR';" on my php code?

<?php
// Show current month
include 'conection.php';                      

$sql .= "SELECT MONTHNAME(CURDATE()) AS `Mes` FROM trabalho_v2 ";                       
$busca  = mysqli_query($conexao,$sql);                      
$dados  = mysqli_fetch_array($busca);
$mes = $dados['Mes'];                       

I tried to create a new querie to inject only this stretch but it didn't work:

<?php
// Show current month
include 'conection.php';                      
$sql = "SET lc_time_names = 'pt_BR';";  
$sql .= "SELECT MONTHNAME(CURDATE()) AS `Mes` FROM trabalho_v2 ";                       

$busca  = mysqli_query($conexao,$sql);                      
$dados  = mysqli_fetch_array($busca);
$mes = $dados['Mes'];                       

回答1:


These are two separate queries. You can't concatenate them together. You have to execute them separately.

<?php

// Show current month
include 'conection.php';
                      
$sql = "SET lc_time_names = 'pt_BR'";
mysqli_query($conexao, $sql);

$sql = "SELECT MONTHNAME(CURDATE()) AS `Mes` FROM trabalho_v2";
$busca = mysqli_query($conexao, $sql);
$dados = mysqli_fetch_array($busca);
$mes = $dados['Mes'];

I also strongly encourage you to use OO style:

<?php

// Show current month
include 'conection.php';
                      
$sql = "SET lc_time_names = 'pt_BR'";
$conexao->query($sql);

$sql = "SELECT MONTHNAME(CURDATE()) AS `Mes` FROM trabalho_v2";
$busca = $conexao->query($sql);
$dados = $busca->fetch_array();
$mes = $dados['Mes'];


来源:https://stackoverflow.com/questions/65259094/running-two-sql-queries-on-one-php-page-set-select

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!