how to search string stored serialized - php

I want to search the computer from this table and when i have stored this in database so now can find the string and pass to the query so it this string present in database then show result
i am doing
i am getting the value form the url my url looks like localhost\demo\article.php?filter=computer
$_GET['filter']
my database is
id | name |tag
1 |java |a:2:{i:0;s:8:"Computer";i:1;s:4:"Code";
2 |dbms |a:2:{i:0;s:8:"Computer";i:1;s:4:"dbms";
3 |c |a:2:{i:0;s:8:"elect";i:1;s:4:"Code";
now i am want to fetch all the row where the computer is present or code
for that i am using the query
$query1 = "SELECT * FROM $tableName WHERE tag='%".$_GET['filter']."%' ";
but its not showing the row having the computer or code..
its like the stackoverflow tag filtration

Solving your problem is by replacing = with like so your query should be
$query1 = "SELECT * FROM $tableName WHERE tag like '%".$_GET['filter']."%' ";
a better way of doing this, is to create extra two tables. One will contain your tags (optional) and the other will contain tagging information.
This article will help you so much

Normalize your database and you will no longer need to turn a nail with a screwdriver.

Related

Needing only 1 row from PHP MySQL database

I'm sure I've done this in the past, but it's a few years ago and I don't remember how it's done and the online tutorials aren't helping.
I have a MySQL database. It has 1 table in it called 'data'. In the 'data' table, there are about 15,000 rows, and 31 columns. I need to extract the data from only 1 of these rows, based on a lookup referencing the string in column 1. When the mysql query finds the correct row, I need every single item read into variables that I can show on my page.
I believe this line is the problem:
$sql = "SELECT Mark,Manufacturer,Model FROM data";
Could someone please let me know what it needs to be changed to, to get the desired result? TIA! :)
you can set options of select query
$sql = "SELECT Mark,Manufacturer,Model FROM data WHERE Model (or manufacturer,mark) = 'some text'";
As my colleges have Explained "where' is your friend!
So you can always query as follows :
Select * from Data
Where column_1 = 'Your Desired String'
Alternatively you could use
Select Discinct Limit 1 Mark,Manufacturer,Model FROM data
Order By Mark Asc

How to select a persons lastname in php from mysql table and put it in a $_Session variable

I want a straightforward answer with an example (NO LINKS please) to see how I can retrieve a specific person's last name from a table in a MySQL database using PHP and save it into a variable (i.e. $_Session). I have been looking for this question and I don't get anything related to one row in PHP. If this can be done better with mysqli* functions then I would be glad to see it.
Example
$getData = "SELECT lastname FROM person WHERE name='$name'";
$getData_q = mysql_query($getData) or die('Error');
Then i want something like this:
$_SESSION['lastame']=$getData_q; <- Please correct me,
I'm not asking to get a lastname from an input, I'm asking to get it from the table person in the database with the SELECT.
Use
$getData_q = mysqli_fetch_object($getData_q);
$_SESSION['lastame']=$getData_q->lastame;
But ensure that that num rows is greater than 0 and data would return a single row.

get row in mysql using data from friendly url and using regular expression

I need to write code for getting data from a database by a friendly url.
I have a table company with the field title for storing some info about a company. I want to get data by title using friendly url. E.g. example.com/company/aurum-1. First I tried to change some defined symbols to - :
function seoUrl2CompanyName($string) {
$string = preg_replace("/[-ecsui]/","%",$string);
return $string."%";
}
[-ecsui] is used because my native language has non-standard symbols like šįėęų which I cant use in my friendly url, so I tried to change them to % and use the following mysql to find the company by title:
$SQL = "SELECT * FROM company
"WHERE title LIKE '".seoUrl2CompanyName($_GET['company'])."';
But if I use this logic I meet with some difficulties when select return more than one row. E.g
example.com/company/aurum -> seoUrl2CompanyName('aurum')-> a%r%m% ->
like a%r%m% -> 24 rows in my table for match this pattern
My goal is to create the fastest way to find the company from company table by name using data from url.
I would take the suggest from #AgeDeO but expand your SQL like this, that you take the data the company-name reflects AND the ID you get from your URL:
$SQL = "SELECT *
FROM company
WHERE title LIKE '".seoUrl2CompanyName($_GET['company'])."'
AND ID = ".$myId.";
With these 2 factors, you should only get one row and can be sure, that no one just replace his 1 with a 2 and gets other companys data.
NEVER EVER DO THIS:
I want get data by title
What are you going to do when the company name changes? What when two companies have the same name? What happens with spaces, special characters etc...
I understand that you want friendly urls and that is possible, just add the company name as dummy data in the url. Show the company name but do not use it.
Use example.com/1/company/aurum-1 instead, where the 1 is the actual company id.
Beware that it is fairly easy to guess other companies like this. When I change the 1 in a 2 I could have access to the other company like this. If you do not want this, make sure you check for permissions on page load.

Retrieve Values for User from Database

I'm currently trying to retrieve values from a database.
New Users are given a UNIQUE ID once they register on my website:
Keys are stored into my database with a link such as:
website.com/file.php?id=4&key=123abcd123
What I was planning on having was the website pulling the keys ONLY related to the ID of the user.
Lets say User 4 inputs a key into the database, I want to have a universal .php file that will retrieve HIS codes (codes with the ID of 4) ONLY.
I'm sorry if this doesn't really make sense, English isn't my first language.
For finding particular record in mysql query pass the id value in the place of 4 below.
SELECT * FROM tablename WHERE UserID = '4'
if you want to get particular value then use like this
$query = "SELECT * FROM tablename WHERE UserID = '".$_SESSION['ID']."'";
Refference
If you use SQL, it is easy to search for all keys for a user:
select cdkey
from YourTable
where UserID = 1
Note that anyone can edit a URL. So unless you take additional measures, any user could ask for the cdkeys of any other user, just by modifying the id parameter in the URL.

How to store configuration variables in mysql?

M'y script's variables are currently stored in a php File as an array. I want to store them in mysql database, however I don't if I should store them as rows or columns. If I use columns it would be easier to retrieve them (only one query), but if I have too many variables the page would have to scroll horizontally and I think it would be hard to find data in phpmyAdmin. If I use rows then how would I retrieve all of them using a single query and store them in the $config array?
Any ideas or suggestions?
Create config table, with 2 columns
Name | Value
and select it with
SELECT * FROM config
so it will look like
Name | Value
offline | 1
message | Hello guys! This is my custom message
to get them into $config, use
$result = (mysql_query("SELECT * FROM config"));
while($row = mysql_Fetch_assoc($result){
$config[$result['Name']] = $result['Value'];
}
that's it!
Depends.. Are the variables user-based? Do you need to search them? One way to store is in a serialized format (string data in a text field) -- this will suffice if you don't need to search the variables. Otherwise, just store one row per (user-)key-value combination.

Categories