I have a Laravel 5.2.45 application, also, I have a complex query to do, so I tried to consult it using:
DB::select("the query");
I read that this should work, but it's not the case, so just testing I simplified the query to: "Select * from aTable" but it also doesn't give any result, it takes a long long timeloading the webpage and then just doesnt show anything. I'm using this exactly: dd(DB::select("SELECT * FROM myTable AS mt"))
So, I'm wondering what is exaclty happening, it's still a valid function in Laravel 5.2? it's a really simple query and am not sure in what is failing. Thanks in advance!
I think you are trying to execute raw query. If you execute raw query in laravel please try this way:
$tableData = DB::select( DB::raw("SELECT * FROM table WHERE id = 100 ") );
dd($tableData);
You can set custom function also for printing data in your helper function like
function pr($var){
echo "<pre>";
print_r($var);
echo "</pre>";
}
than you can call pr($tableData);
I think this should work for you. Thank You :)
It's strange but you can check your Query Log to find problem
$users = DB::select('SELECT * FROM myTable AS mt');
print_r(DB::enableQueryLog());
How to enable query log
https://github.com/laravel/framework/commit/e0abfe5c49d225567cb4dfd56df9ef05cc297448
Try this:
$users = DB::table('myTable')->get();
Related
I have this code in a controller (Laravel 5.6):
\DB::enableQueryLog();
$foo->update($data);
dd(\DB::getQueryLog());
The problem is that in the dump there is no update query. I know the update command is running (I can see the updated data in the database). What am I missing?
You might have better luck with:
\DB::connection()->enableQueryLog();
$foo->update($data);
dd(\DB::getQueryLog());
Personal choice, but I would even modify it a bit more to:
\DB::connection()->enableQueryLog();
$foo->update($data);
print_r(\DB::getQueryLog());
die();
You might like the output better.
Try Query Builder:
DB::connection()->enableQueryLog();
DB::connection('your-connection')->update($data);
$queries = DB::getQueryLog();
dd($queries);
If that doesn't work out, you might want to check this out.
I'm trying to call a stored procedure (from mysql) with codeIgniter (3.0.4).
My issue is that I have always the message "Commands out of sync; you can't run this command now".
Here is my code
$this->db->trans_start();
$sql = "CALL setup_resource('{$p1}','{$p2}','{$p3}',#id)";
$this->db->query($sql);
$query = $this->db->query("SELECT #id as my_id");
$this->db->trans_complete();
$my_id = 0;
foreach ($query->result_array() as $row) $my_id = $row['my_id'];
I'm trying to execute with SP and with #id retreive the id returned by my stored procedure => procedure seems that I cannot call another query in the same transaction.
I saw in many topic that CI does not handle correctly multiples query string, and advise tu use "next_results()" function but the result is the same (or I have a message "none next_result() you have to use more_results()").
Also saw that i should use multi_query function from mysqli but did not saw examples of it.
Did anyone have this issue ? What would be the workaround for codeIgniter to handle it?
Thanks for your help ;)
So I have been moving from framework to framework and one feature that I liked about CodeIgniter over Laravel was that it supplys Raw statements.
I did some research on Laravel and the statements that they can supply and I found the DB::raw($sql) statement.
I tried initially to do the statement like so:
$query = DB::raw("SELECT * FROM users WHERE username = 'admin');
foreach ($query as $q) {
$id = $q['id'];
}
but that didnt manage to work, so I did some more research looking at stack overflow questions and I managed to find the structure was a bit different, so I tried:
$query = DB::select(DB::raw("SELECT * FROM users WHERE username = 'admin');
then used the same foreach loop as I did in the first part to try to loop the data.
If this is the wrong way of using this method please let me know, also, I'm not planning on keeping this method for using raw statements and getting user data, I just am trying to know that is it able to be used.
use get() function :
DB::table('users')->select('*')->where('username', '=', 'admin')->get();
I am encountering a strange issue in Laravel.
The below is an index function in one of my controllers.
public function index($merchant_url_text)
{
//
$deals = DB::table('tbl_deal')
-> join ('tbl_merchant', 'tbl_deal.merchant_id', '=', 'tbl_merchant.merchant_id')
-> where ('merchant_url_text', $merchant_url_text) -> toSql();
//return $merchant_url_text.$deal_id;
dd($deals);
//return $merchant_url_text;
}
As you can see I am passing merchant_url_text from route.
Route::get('/testroute/{merchant_url_text}', ['uses' =>'dealsVisibleController#index']);
When I am trying to debug the query by printing it, I am getting
"select * from `tbl_deal` inner join `tbl_merchant` on `tbl_deal`.`merchant_id` = `tbl_merchant`.`merchant_id` where `merchant_url_text` = ?"
This means that the query builder is not reading the $merchant_url_text variable. However, when I return just that variable, it is being printed.
Just can't figure out why the query builder is not able to include the $merchant_url_text variable in the query when it is available in the index function.
Any suggestions.
I am pretty sure that your code is correct. The SQL output function toSql() does not show the values of variables and only prints out a ? for security reasons.
You may access all your queries by using
$queries = DB::getQueryLog();
It is also printing the query parameters as array.
To get the last query:
dd(end($queries));
To disable the log:
DB::connection()->disableQueryLog();
See the docs for further information.
I just asked this question, but have now found that the problem with binding conditions in that method is it's very hard to programatically compile more complex queries...
I think I need to be using the PHP ActiveRecord SQLBuilder but I'm finding the documentation confusing. How do i actually execute a query i've 'built'?
This is their example code, but it doesn't actually show you how to return any data?!
$builder = new ActiveRecord\SQLBuilder($conn, "table_name");
$builder->where("name = ?", "Hemingway");
echo $builder; /* => SELECT * FROM table_name WHERE name = ? */
Any help really appreciated :)
If the model corresponding to table table_name is ModelName then:
$result_set = ModelName::find_by_sql($builder->to_s(), $builder->bind_values());
haven't checked completely but following might be helpful
ADODB Active Records for PHP