Can you query a Sql Server View with PHP? - php

So I know that you can query a table using PHP as so:
$projectmanager=DB::table('Table_name')->distinct()->select('Table_Column')->get();
I want to know if you can do the same type of thing with a SQL Server View. I have tried the following:
$view=DB::view('View_Name')->select('View_Column')->get();
But I get the following error:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Database\SqlServerConnection' does not have a method 'view'

Are you using Laravel by chance? Whatever the framework, you are calling a method DB::view that doesn't exist, so some class magic fails.
To answer question, yes. To PHP they are seen the same as a regular table, treat it as such for querying (though you cannot manually update them of course).
$projectmanager=DB::table('View_name')->distinct()->select('View_column')->get();

Related

Understand cakePHP 3.3 nomenclature

New to CakePHP 3.3. I looked across multiple sites (Stack Overflow, W3Schools, etc.) but cannot find an answer to:
What does this piece of code mean/do in a CakePHP tutorial:
public function initialize (array $config)
1) public: I understand this can either be public, private, or protected
2) function: Is this creating a function called "initialize()"?
3) initialize(): I understand this is a method built into CakePHP, but what does it do?
4) array $config: Are these two words combined or are they separate of each other? Why do I need both words, what is the influence of one on the other?
Background: This comes from the CakePHP tutorial and is found in multiple .php files. Not sure what is php code and what is unique CakePHP terminology.
I think This is very good question...!!! This question is applied for all framework not bounded only for CakePHP.
function my_function(array $arr, int $int, $bool $active)
{
/// function body
}
In technical it knows as Type Hinting
Type declarations allow functions to require that parameters are of a
certain type at call time. If the given value is of the incorrect
type, then an error is generated: in PHP 5, this will be a recoverable
fatal error, while PHP 7 will throw a TypeError exception.
To specify a type declaration, the type name should be added before
the parameter name. The declaration can be made to accept NULL values
if the default value of the parameter is set to NULL.
Check about valid types paramater
Reference: http://php.net/manual/en/functions.arguments.php
I see at least one great advantage of using type hinting: readability. It's easier to see what kind of parameter a function/method expects.
Some will see this as a drawback that if you don't respect a type hint and pass some not-type-valid parameter, you'll get a catchable fatal error. However,
I personally think it forces the developer to respect your interfaces more
It allows your code to just work with the received parameters.
The generated error is catchable.
Question of Performance?
Making the decision of using type hints or not using type hints should not be the result of performances considerations. You should use or not use the feature.
Even if there is some performance impact, it should be pretty minimal. I would say there are many parts of your code that you should optimize before considering this.
Conclusion
In conclusion, a personal opinion: I quite often use those type hints, just so:
Other Developers using my code know what they are expected to pass in (they don't always read the documentation...)
If they don't do what is expected, it fails; hard; in a way they immediately notice
My code doesn't have to check if the received parameter is of the right type: I know it is, as PHP enforces that for me.

Cakephp Find Convert to Sql Syntax

Cakephp find data in controller also can with sql syntax,
example :
$this->Post->find('all');
in sql :
select * from posts
convert query to cakephp find syntax is available at http://dogmatic69.com/sql-to-cakephp-find-converter
but whether there is any link for me if I wanna convert my cakephp find to query..
Thanks in advance...
If I got what you are asking, here is the answer
SQL calls that you can’t or don’t want to make via other model
methods can be made using the model’s query() method (though this
should only rarely be necessary).
query() uses the table name in the query as the array key for the returned data, rather than the model name. For example:
$this->POST->query("SELECT* from posts;");
Here is the link for cakephp documentation

Laravel 4 create table from construct

I'm trying to create a table in laravel 4 from a construct function, but it's not working.
I have search online for a proper way to do this which led me to raw. But raw expects a table name, or at least that is what I made of it.
So I looked a little further and found a DB::only function. But it throws this error:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Database\MySqlConnection' does not have a method 'only'
So there you have it, I would really like a solution for this seeing my project requires it.
Well, found it:
DB::statement($sql);

Extend MySQLi Class to EXPLAIN SQL queries on page

how can I extend the MySQLi class to explain all SQL queries on a given page?
Thanks.
There are several ways to do this. Here is one:
To start, you can get an explanation by simply prepending "EXPLAIN " to the mysql statement. See http://dev.mysql.com/doc/refman/5.0/en/explain.html for details on Mysql's explain.
Knowing that Mysql invocation, the mysqli::query and mysqli::prepare methods both take their first parameter as the $query string (side note: just use the Reflection class in a quick test script to get the current method or construct prototypes. You'll find extending mysqli_result class is not actually possible, but that shouldn't be a problem here).
Knowing that,
Store the mysql query string used in the calling of your extended methods as a new property of your extended `mysqli` class
Pass on the parent method's return results as normal
Create a new custom method to call this string property with `explain ` prepended and run a new separate query on that using any parent method or generic Mysqli invocation. This new method will be called any time you need to output the query explanation in your html view

Can I use whereIn in a query with named parameters?

I'm working on a Symfony app that uses Doctrine as the ORM. I want to run a query with an WHERE foo IN (bar) clause, and I'm adding the IN bit like so:
$query->andWhereIn('p.foo', $bar);
where $bar is an array of id numbers. Browsing the docs and trying out a few combinations, I was unable to make Doctrine treat the parameter I'm passing there as a named parameter.
As a result, I'm forced to use positional parameters for the rest of the query, too, as you can't mix the two. What, if anything, am I missing?
I wrote a patch for this http://www.doctrine-project.org/jira/browse/DC-1003?page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel that was never committed but it does handle this scenario..
andWhereIn() is simply a proxy method for whereIn()
Have you tried to use whereIn() or did you skip directly to andWhereIn()?

Categories