Need to know what is Mid([GTIN],2,10) in MySQL - php

I've been given a query to use in some PHP code but the problem is, the person that gave me the query is using MS Access and I'm using MySQL so everything isn't quite translating. In his query there is this
... Mid([GTIN],2,10) AS items_bar ...
MySQL doesn't like this part and I'm not sure what it is supposed to be doing. Is there an easy translation for this to MySQL?

It is selecting a sub-string of the GTIN element.
Instead of using this:
Mid([GTIN],2,10) AS items_bar
Use this:
substring(GTIN,2,10) as items_bar

Related

How to autoload data from a database in laravel PHP

I have a question on how to achieve something from a development standpoint in Laravel. I am looking for ways to use autoload to return data from a database by just supplying a few character entry. For example, if I need to search for the word 'Johnson' which is already stored in a table in the database, supplying the word: 'Joh' in the search bar should bring up Johnson and other names like Johnson. I am very new to PHP and laravel, does anyone have any suggestions?
You are going to have to use Ajax and php to do this.
Basically the logistics will be as such:
a key is typed into an input which will load a javascript function that does a post to a php file. That php file will do a query for "Joh%". All records will come back matching Joh%. Print them out with php and it will be delivered back to the javascript function which called the php file in the first place. Now that javascript function will have to manage adding and subtracting entries from the dropdown on the input box you are typing in.
The question your asking is basically a lot of code and you should take it step by step. First start by getting a php query to work where you get records matching "Jon%", then continue on to another step.

PHP, MySQL Dynamic Maths

I am trying to find out a way to do maths dynamically. I want to be able to add a formula to MySQL and then solve it in PHP and display the answer.
So I'll save it like getItemPrice(10) * getItemPrice(50) However I'm unsure how to execute functions in PHP if its saved like that.
I thought about splitting the date but I want to be able to do more advanced formulas then that. I have no idea how to do this to be honest...
If the text you stored in db is a valid php code you can use
eval — Evaluate a string as PHP code
http://php.net/manual/en/function.eval.php
otherwise if the text is a valid mysql code you can generate a dinamic sql statementes and the execute as a normal sql statemets ..

Getting content from search box finding it in database

I know this is a broad question, but seriously.. I really couldnt find and answer for this, only the LIKE keyword that checks if it contains it..
I want to look for something in a db if it contains it, starts with it, or ends with it and by order by the most populaur result?
I have a search box in bootstrap and when the content is changed, an ajax request is sent for the new content..
It there a way to do this.
Using LIKE is nothing. Use full text search in mysql. There are disadvantages: myisam required, for char/varchar/text fields only. See more info here:
https://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
A great alternative is using a real search engine such as Elasticsearch, built on the top of Apache Lucene: fast, relevant, flexible, adaptable, works with json api. See more info here: https://www.elastic.co/products/elasticsearch

Multiple Solr filter queries in PHP

I am trying to use facet in solr, i want to make a search on my database and i need to get articles that belong to specific date and specific publisher.
i used this url on the browser:
localhost:8888/solr//collection1/select/?q=:&version=2.2&start=0&rows=10&indent=on&facet=true&fq=publisher_name:"Saudi Press Agency (SPA)"&fq=datecreated:20110725
and it works fine. I am using (search()) function in apache_solr_service class in my php code. and i set the array as below:
array('facet'=>'true','fq'=>"datecreated:".$date,'fq'=>"publisher_name:\"".$publisher.'"')
I know it wont give me the expected results because of fq index, it will overwrite the value of fq into publisher_name
but how can i set this query with two facet queries
This might help:
http://code.google.com/p/solr-php-client/wiki/FAQ#How_Can_I_Use_Additional_Parameters_%28like_fq,_facet,_etc%29
This is what the code should look like (please check for syntax errors, my PHP is rusty):
array('facet'=>'true','fq'=>array('datecreated:'.$date,'publisher_name:"'.$publisher.'"')
You could also write it like this:
array('fq'=>'+datecreated:'.$date.' +publisher_name:"'.$publisher.'"')
http://wiki.apache.org/solr/CommonQueryParameters#fq
use AND operator:
array('fq'=> 'datecreated:'.$date.' AND publisher_name:"'.$publisher.'"');

How can I store PHP code inside of a mysql table

I am working on building a small php/mysql script that will act something like a wordpress blog but will just be a small site for my eyes only to store PHP code snippets. So I will have categories and then pages with sample code that I write with a javascript syntax highlighter. Instead of storing my php code snippets in the file I am wanting to save them to mysql DB. So what is the best way to save PHP into mysql and to get it out of mysql to show on the page?
My end result will be something like this
alt text http://img2.pict.com/c1/c4/69/2516419/0/800/screenshot2b193.png
Update:
I just wasn't sure if I needed to do something special to the code before sending it to mysql since it has all different kinds of characters in it
Just store in a text field, as is. Not much more beyond that.
If you're not using some kind of database abstraction layer, just call mysql_real_escape_string on the text.
Do you want to be able to search the php code? If so, I recommend using the MyISAM table type as it supports full text indexes (InnoDB does not). Your choices for column type when it comes to a fulltext index are char, varchar and text. I would go with text as your code snippets might get too long for the other types.
Another point worth mentioning, is make sure you properly escape all php code (or any value for that matter) before you insert it. The best way to do this is by using parameterized queries.
Unless I'm missing part of the problem, you should be safe storing it as a TEXT field in a MySQL database. Just make absolutely sure you sanitize the code snippets, as PHP code in particular is quite likely to contain the characters that will escape out of an SQL string. (If you're already using an SQL framework, odds are the framework is doing this for you.)
Store as text (varchar) in the database.
Use cascading style sheet (css) to format code.
http://qbnz.com/highlighter/
Try this:
mysql select ...
eval('?>' . $row['phpcode'] . '<?php ');

Categories