I have searched everywhere but can't find any answer.My question is that is it possible to execute an eloquent query that is stored in a variable?
In sql its so simple to execute the query,but I'm facing problems from 2 days in searching a way of executing an eloquent query that is assigned to a variable.
e.g $query="modelname::find(1);I have tried eval() but its not working.Actually I'm pretty new to laravel.So, I'm looking forward for your valuable answers.
Related
I'm having trouble with a single query to one of my databases, and I think it's because the data I'm passing isn't being inserted correctly. The easiest way I can think of to try and check this would be to see the actual sql generated by my fairly simple query, which I call like so:
query = ActiveRecord::Base.connection.raw_connection.prepare(query_string)
result = query.execute(start_time, end_time)
This query, unfortunately, does not seem to log anything like queries made through activerecord - I have logged queries from before and after it, but the one made with execute says nothing at all. I know it's running the query because it does return results, just the wrong ones.
Is it failing to log the actual query because it's an execute statement? What do I need to do in order to see the actual sql query being sent out to the table here?
This question is quite open ended. Maybe it doesn't belong here, so sorry in advance if that's the case.
I'm using SQL in a real project for the first time, and I'm having some issues. I'm also using php and javascript.
In the web app I'm currently writing, i do a lot of SQL query to my MySQL database. Each one of these query do a different thing, on different tables, etc.
At the moment, I'm doing the following : each time i do a query in the client side code, I'm passing a parameter I defined myself to the server.
On the server, I wrote a switch on this parameter. So it looks like this :
if (parameter == 1)
/*Query 1*/
else if (parameter == 2)
/*Query 2*/
etc.
It's not very convenient. However, if i use a function writing a SQL query by taking parameters from the client, it would be subject to SQL injection.
So to sum it up : how do i create a SQL query in the most "modular" way possible and still avoid SQL injection ?
I already know about prepared statement, but if i get it right, i can't write a full SQL query with only prepared statement.
Hi I have an Excel sheet with quite a number of data inside, and now I want to perform queries directly in the sheet using VBA. One query is to find the recurring users, so when I implemented it in MySQL I used the DATEDIFF function, so is it possible to do this in Excel? I've tried to embed the SQL statement into VBA, but it failed. Thank you!
My original SQL statement is like this:
select max(logtimestamp), min(logtimestamp), count(logtimestamp), username
from report
group by username
having datediff(max(logtimestamp),min(logtimestamp))>=14
order by count(logtimestamp) DESC;
And it works in MySQL.
EDIT:
I'm currently using ADODB in VBA to embed the SQL queries. Some simple queries have already been implemented, so I suppose my configuration is correct; the problem now is I don't know how to get this SQL query into VBA. It's now giving an error, and it might be related to the DATEDIF (different from DATEDIFF in SQL) function in Excel.
In one of my database services, I create a hibernate query as follows:
createSQLQuery("SELECT * FROM documentheaders order by LEN(header) DESC").addEntity(Documentheaders.class);
This works great as long as I am using MS SQL. If I try to run this query with MYSQL I get a sql error because in MYSQL, the correct function is LENGTH. Is there any way to create a single sql query that will cover both dialects (I know I can check the database type and use separate queries for each kind of database but I'm hoping for something more elegant.
Thanks,
Elliott
My thanks to frictionlesspulley for alerting me to the HQL function. That solved the problem.
When you run Linq to Sql or Linq to Entites to get a list of records it runs query to select all fields from a table. Is it an efficient solution. Lets say: I run this LINQ
dim lstCustomers = from c in db.Customers select c
it run query to get all fields from a table whether i need all fields or not. I am using asp.net with MVC so should i write this query in view (where i only need CustomerID and name)
dim lstCustomers = from c in db.Customers _
select new Customer with { c.CustomerID, c.Name }
If i have to use 2nd query then whats the advantage of LINQ and Entity Framework. This thing i can do with SQL query (with different syntax)
Anyone can help?
First of all, LINQ queries are evaluated lazily. That means that single line doesn't do anything but itself, so I assume you actually iterate the results with For Each.
The answer to your first question is yes, all fields are retrieved from the database with the first statement.
Yes, but in order to use SQL directly, you'll have to manually create entity classes, manually retrieve data using SqlDataReader or something to achieve the level of abstraction LINQ provides in that line. That's lots of more work on your behalf. With LINQ to SQL, you don't even need to explicitly write code to open a connection to database.
actuly linq have different sets of advantages over writing normal writing sql queries:
if you wrote sql queries then overloaded steps:
1. you need a sql connection class
2. you need a sql command or sqldataadapter.
3.then you need a container like datatable and dataset.
so while using linq you dont need all those steps. just write the queries as you wrote above.
also incase you wrote something incorrect in your sqlquery then there is no compile time error. error only generates when you execute the query during runtime.
but unlike sql queries, linq provides you the compile time error.
also linq is best of strongly type collections.