socrata ny data service not taking $where? - socrata

https://health.data.ny.gov/resource/u4ud-w55t.json?hospital_county=Suffolk$limit=1
works but
https://health.data.ny.gov/resource/u4ud-w55t.json?$where=hospital_county=Suffolk
does not. Why is this ? since $limit is working why is that $where is not?

When using the $where clause you must surround string values with quotes. So the correct query would be:
https://health.data.ny.gov/resource/u4ud-w55t.json?$where=hospital_county='Suffolk'

Related

how to search by letter in column in database (laravel)

I want to find by letter customer's first_name on my 'customers' table.
And the only thing that I get is an empty array.
For instance, I put in the parameter 'q' value 'E' to get customer Elena from my database by I get an only empty array.
I use the following code to get first_name :
$search = Input::get('q');
if($search)
{
$customers = Customer::all()->where('full_name', 'LIKE', "%{$search}%");
return ($customers);
}
Can someone help me?
Your query don't work because you are calling the all() method before the where(). That's actually not wrong, but it have different behavior.
When you call all(), it actually does the SQL query. After that, any chained methods are being called into a Eloquent Collection class, and it also have a where method, but that's simpler since it runs on PHO instead of running on SQL.
Since the collection's where() method doesn't support LIKE operator, it's probably searching for a value that is exactly %E%.
Hope it can help you understanding why your query doesn't work as expected.
Try this
$customers = Customer::where('full_name', 'LIKE', "%{$search}%")->get();
Laravel Eloquent

How to insert Lat and Lng to point type column in mysql with laravel?

this is my code
$map_point->map_id = $id;
$map_point->point = GeomFromText('POINT('.$lat .' '.$lng.')');
$map_point->save();
it gives me
Call to undefined function GeomFromText()
so what is the problem here ?
GeomFromText is actually a MySQL function that is executed in SQL. If you need to use it you will have to use DB::raw("GeomFromText('POINT('.$lat .' '.$lng.')')")
I have never tried using DB::raw() on a model yet, but, in theory it could work. But, this may mean you will need to use a Query builder query to do this instead of Eloquent.
Also, be VERY CAREFUL about including variables directly when using DB::raw or query builder. These are possible injection points.

Using simple and double quotes in an sql query

I have some issue with a sql query using quotes with variables. (In general I use "bind" so I don't have this kind of problem). Here's the query :
$myquery = mysql_query("SELECT * FROM mytable ORDER BY id ASC WHERE var='".$var."'");
The syntax seems not to be correct, can anybody help ?
well you can try something like this:
$query = sprintf("SELECT * FROM mytable WHERE var='%s' ORDER BY id ASC",mysql_real_escape_string($var));
$result = mysql_query($query) or die("Error:" . mysql_error());
Also note that ORDER BY is at wrong place.
It is more readable and you don't need to bother with single qoute concating.
Also it is safe for mysql injection.
Hope this helps!
In general you should use the parameter binding features provided by your DBD (Database Driver for Perl) or other language and driver combination. I gather that you're using PHP (though you should tag your questions accordingly to remove the ambiguity.
Here's a StackOverflow thread on How to bind SQL parameters in PHP (using PDO). Note there are limitations to the PHP PDO::bindParam method as compared to similar features in other languages. So read the linked thread for caveats.
Here's another discussion about Binding Parameters to Statements ... for Perl (but conceptually applicable to other programming languages and their SQL libraries/drivers).
You can use it like
$myquery = mysql_query("SELECT * FROM mytable ORDER BY id ASC WHERE var='$var'");

MySQL query with regexp not working in Drupal

I have the following query, courtesy of SO:
SELECT field_website_value FROM field_data_field_website WHERE field_website_value NOT REGEXP('^(https?://|www\\.)[\.A-Za-z0-9\-]+\\.[a-zA-Z]{2,4}(/\S*)?') AND field_website_value!=''
When executing this query directly in the MySQL client, it works (shows the values that don't match the pattern).
However when putting it in Drupal, it stops working, it just returns the rows which are not empty.
$query = "SELECT field_website_value FROM field_data_field_website WHERE field_website_value NOT REGEXP('^(https?://|www\\.)[\.A-Za-z0-9\-]+\\.[a-zA-Z]{2,4}(/\S*)?') AND field_website_value!=''";
$res = db_query($query)->fetchAll();
echo count($res);
echo "<pre>";print_r($res);die();
Is there any way I can use Regexp in Drupal?
Note: getting all rows and applying the regex in PHP isn't an option.
I'm no drupal expert but I bet db_query function is doing a mysql_real_escape_string() call which will mess up the regular expression, are there any other functions you can pass that won't do this?
Actually it is the {} brackets causing the issue, you need to pass the data as a variable,
$query = "SELECT field_website_value FROM field_data_field_website WHERE field_website_value NOT REGEXP('%s') AND field_website_value!=''";
$regexp = '^(https?://|www\\.)[\.A-Za-z0-9\-]+\\.[a-zA-Z]{2,4}(/\S*)?';
db_query($query, $regexp);

mysql perl placeholder rules

MySQL, Perl
The following select works fine with no placeholders, but doesn't with placeholders. It doesn't generate any SQL errors, but it returns all blanks/zeros - not the same counts as the same statement without placeholders.
my $sql="SELECT ?, SUM(IF(H1='1',1,0)) AS banner1 FROM table_name WHERE (?!='' and ? IS NOT NULL) GROUP BY ?";
my $sth = $dbh->prepare($sql);
my $variable = "Q1";
$sth->execute($variable, $variable, $variable, $variable);
What am I doing wrong?
Am I trying to use placeholders in ways not intended? It works when I only use placeholders in the WHERE clause. It does not work when I use a placeholder in the SELECT or GROUP BY clause. Is that the issue - placeholders can only be used in the WHERE clause?
You can't use placeholders in the SELECT portion of an SQL statement. This is described in the documentation:
With most drivers, placeholders can't be used for any element of a statement that would prevent the database server from validating the statement and creating a query execution plan for it. For example:
"SELECT name, age FROM ?" # wrong (will probably fail)
"SELECT name, ? FROM people" # wrong (but may not 'fail')
You can't use placeholders to substitute a column or table name. Even in your WHERE clause, it's not doing what you think it's doing. When you substitute Q1 for the placeholder, you get:
WHERE ('Q1'!='' and 'Q1' IS NOT NULL)
i.e. an expression that is always true.