I have a database like this:
id | link | someotherstuff
---------------------------------------------------------
1 | index.php?video=123 | blah
2 | index.php?video=4567&other=variable | blah
3 | site.com/index.php?video=video=1 | blah
and so on.
I would like to use URL parameters to find the right video link.
When I visit: www.mysite.com/index.php?video=1 I want the database to find the "video=1" part of the link column. I have everything working except when searching the link column it finds both id 1 and id 3 since they both have the number 1 in the link.
I'm using Piwik to get statistics for my pages. I use a session to get the video number from the url and then use this:
$videostring = "video%3D".$_SESSION['video']."";
with this:
$resulting = file_get_contents("http://www.site.com/piwik/?module=API&method=Actions.getPageUrls&idSite=1&period=range&date=2013-04-05,today&format=php&expanded=1&token_auth=$token_auth&filter_pattern=". $videostring . "");
How do I use the filter_pattern so I can limit the search to just the number I want?
I think there is no useful way except than using id value in your link address, like site.com/index.php?video?video=1&id=3. So you don't need to search database for finding id.it's in your address
select id from table where link like '%video=1' OR link like '%video=1?%'
With The Edit:
You cannot do that if your server puts % before and after the value of filter_pattern to do filtering.Check for server side code to see what it does to filter_pattern variable just before calling the mysql.
Related
I hope I can explain well enough what I am looking for. I tried doing some searches on here, but wasn't finding what I want. I want to do this with strictly mysql if I can with no PHP involved.
So I have a table with terms in it with URL links for each term, we'll call that terms. I have another table with short paragraphs of text we'll call paragraphs. What I want to do is select a paragraph from the table and search it for any of the words contained in the terms table. If the term is in the text, I want to replace it with a concatenated html anchor tag using data from the terms table. I then want the paragraph returned as a formatted string with the anchor tags in place. This way if I add or remove terms to or from the table, I always get a paragraph formatted with the most up to date terms.
Let me know if this is too confusing and I'll see if I can explain it better. Also, if I missed something in my searching that is already posted for this, feel free to guide me in that direction.
Thanks in advance for any help.
-------------------------------------------------
Edit: Here is an example.
-------------------------------------------------
`terms` table
-------------------------------------------------
ID | term | URL
---|----------|----------------------------------
1 | printer | http://t-shooting.link/printers
2 | scanner | http://t-shooting.link/scanners
3 | copier | http://t-shooting.link/copier
-------------------------------------------------
`problems` table
ID | problem
---|---------------------------------------------
1 | the two printers (side by side) need cleaning as the
| copies from them come out with streaks.
2 | My computer won't recognize scanner when I try to use
| it. I was able to use the copier to scan for now, but
| it would be nice to use the scanner that I have in my
| room.
So if I select ID 2 from the problems table I need it to search for any words contained in the terms table and replace them with a mysql "CONCAT('', terms.term, '')" or however I decide to format that. This would make the paragraph look like this:
My computer won't recognize scanner when I try to use it. I was able to use the copier to scan for now, but it would be nice to use the scanner that I have in my room.
The more I do on the mysql side, the less overhead I have on the PHP side. I know ways to do this in PHP, I am just wondering if there is a way to do it in mysql.
Does that help a bit?
About
I have this table in my database holding some information saved with a user id and time.
| CONTENT | USER ID | TIME |
| text | 1 | 1405085592 |
| hello | 2 | 1405085683 |
| hey | 1 | 1405086953 |
This example could be a data dump from my database, now as you can count there is "three" rows. However I only need to know how many users there have some information in my database. Therefor the result I'm really looking for is "two", because only two users have information in the database. User ID 1 is owning both "text"(1) & "hey"(3) where user ID 2 haves "hello"(2).
In short
I want to count how many users (regardless how many rows of information they have) there are inside my database.
** What I tried **
I tried to fetch every single row into an array and then using array_unique to count them together, works fine but I do not see this as a clean and best way to do this.
Then what?
I could use the array_unique and just use count to see how many rows there are, but I'm looking for something more clean. I tried to search for this, but I'm not actually sure what I should search for in term to hit something I'm looking for. After being stuck and though I wanted to learn something new, I wanted to post this problem here.
Note
I hope you guys can help me, I have tried to make it clear what I'm looking for and what I tried. If not please let me know. Sorry if some of the above contains misspelled words, incorrect grammar or is badly explained. I do not speak English daily, but I try my best.
You are looking for the DISTINCT keyword. It returns the count of unique values of a column:
SELECT COUNT(DISTINCT user_id)
FROM your_table
See example on SQL Fiddle.
This query:
SELECT DISTINCT user_id FROM table
will return just one row for every user in the table.
have a string($string) and a Column(DOMAIN) in a table like this:
$string="'domain3','domain2,'domain1'";
-----------------
| DOMAIN |
|---------------|
| domain1 |
| domain2 |
|domain1,domain2|
-----------------
And I am trying to create a sql query that return a result if the domain is included in the string. What I found is that I have to split the entries in the column, so I have something like this:
SELECT * FROM TABLE1 WHERE SUBSTRING_INDEX(DOMAIN, ',', 1) IN ('$string')
But this gives me a result only if the first index match, I want to do a kind of loop.
I don't know if my question is clear enough, but to explain the context, I am trying to filter results using checkboxes.
Thanks!
What you get as results is right. You only substring from the 1st index. What you are looking for an explode functionality in mysql.
You can read the comments on this page for a plethora of solutions to the string-splitting problem: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html.
I'm trying to write a query in php that will select a row from a database that partially matches a string.
Better explained via an example:
Say my DB has a field called numberString, with value 'abcd'. I want to write a query that, given "123abcd", will return a field of that row of the database.
SELECT mood
FROM USERS
WHERE numberString like '%$giveNumberString'"
$giveNumberString is the functions parameter (i.e. the string that I want to look for)
But I think those two numberString and '%$givenNumberString'" should be like the other way around in order for the query to work as expected. Is there a way to do this?
Ok the table looks like this:
id | username | numberString | mood
-------------------------------------
1 | myUsrNam | abcd | happy
Now I want, given "123abcd", to retrieve the mood of that person. In other words to match 123abcd against abcd.
I'm not sure this is a legal syntax, but let me try a wild guess: have you tried
SELECT mood from users where '$giveNumberString' like '%' || numberString || '%'
?
For work, I am working on a site that has a feature that lets users make custom instances of the site depending on who they want to show it to. So if they want to show their potential employer a little bit about themselves, they can send them a custom url, that has a uid on the end of it that tells the database what to show and what not to when the site is loaded up.
Now, I need to be able to take the value that is on the end of the url, this unique uid that corresponds with their preferences in the db, accessable on run time. Meaning that when someone types in this custom url, I need to be able to show the name of the person whose portfolio is being viewed BEFORE password authentication takes place...
If I could get the URL, I suppose I could parse through it to find the necessary UID...but how do I get the url? Maybe I am just missing something here, but if anyone has any thoughts or ideas, it would be appreciated!! Thanks
Sorry, to further clarify, I am using PHP to handle calls to the database...
Did you know dynamic web site technologies do just that?
There are lots of them and it is fairly easy to get the parameters from the URL.
Examples :
PHP:
http://www.example.com/show.php?uid=123
$_GET['uid'];
JSP:
http://www.example.com/show.jsp?uid=123
request.getParameter("uid");
There are so many of them it's pointless to enumerate them here
If you are using a server-side language, you can get any part of the url easily, for example, in php, it can be gotten through $_GET array and other arrays. If you want it with javascript, you can do like:
JS Example:
var url_array = document.location.href.split('/');
echo url_array[0]; // part one
echo url_array[1]; // part two
echo url_array[2]; // part three
You can even get the host part with:
alert(document.location.host);
PHP Example:
www.mysite.com?id=100
echo $_GET['id'];
This would depend on what language you are using, but typically that uid at the end of the URL would be passed as a request parameter, like this:
www.mysite.com/info?uid=dkafdojoapjdopakd
You would then access it in your code using the request parameters. So, in PHP, it would be:
$uid = $_GET['uid'];
// get the appropriate data for this uid, write out the page contents, etc.
First store each user's preferences as a piece of css within html style tags
+-----------+---------------+
| UID | style |
+-----------+---------------+
| 0z65dbr | <style>h1{ |
| | font-family: |
| | arial, |
| | sans-serif |
| | }</style> |
+-----------+---------------+
Then setting the get variable 'uid' in the url eg. www.example.com/page?uid=0z65dbr you can extract the value form the uid variable in php with $_GET['uid'] and pass this value to your sql query:
$sql = "SELECT style FROM styles WHERE UID = '".mysql_real_escape_string($_GET['uid'])."'";
Then in the put the result of executing the query into the head section of the html.