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.
Related
based on my question, I have a table named tbl_programme. That table has 3 columns which are ID, session, and class. Now, I want to create an MYSQL query for a PHP website that can display the data based on the 3 columns, which are session, class, and course.
From that 2 columns, it's not compulsory to use them where clause for all 2 columns, it can be two columns or one column only. Below is my current query:
SELECT * from tbl_programme where session = '$session' AND class = '$class';
If I run the query with only put data for class and course, it still does not display the specific data that I want. I try to change the use of OR, but still the same. Can anyone help me to solve this?
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.
I am trying to learn php databases and I have a question. How do I store an array in a database? I saw an answer on stackoverflow and there were they doing something with type double and in an other answer they were talking about creating a table for every user but I can't find a solution.
So summarized. I want to store an array in a database. I have acces to phpmyadmin so from there can I set the value type. and I would like to store that array in one column.
Can somebody help me solving the problem?
edit one: The things I want to store are music tags. So in code it would be something like this:
array('pop','rock','country');
I want to store It in one column to make it easy searchable
Rather than storing arrays, try this:
Table 'genres' :
id | name
1 | pop
2 | rock
Table 'songs' :
id | ...
1
Table 'songs_genres' :
song_id | song_genre
1 | 1
1 | 2
And use JOIN's to get the genres for each song (or whatever)
Usually you shouldn't store arrays in a single column in a db. It is preferable to have a table with tags and another one that links your entity with its tags. So, before try to store an array in a table just think if it is really the right thing to do in your specific case (usually not).
If you are sure you want to do that then you have serialize and unserialize functions.
UPDATE (after five years)
serialize and unserialize methods are inherently unsecure and should be avoided. It is preferable to store data in JSON format using json_encode and json_decode.
serialize() is one option here. You can serialize a PHP array into a string to store in the database, and when you return the string from the database you can unserialize() to convert it back from a string to an array.
[ Edit ]
After you've updated the question with an example of the data you plan to store, using a MANY:MANY relationship in the actual database structure is the correct way to go, as mentioned in #Alex M's answer
You can use json_encode to make a json string from the array, like so :
$jsonarray = json_encode($array);
then after retrieving the information you decode it.
$array = json_decode($jsonarray, true); // the true will turn it into an array,
otherwise it's an object.
but I'd advice against it. try making a database which has the proper columns and store your data trough there.
Every user can have one or more music tags. In later time they want to add or remove those tags. If you store all of their tags in one column you are pretty much left with string operation rather than database operation. create new table tbl_user_music_Tags and save each tag along with the user ID. this way you have full flexibility of adding, removing, updating and reading tags for a specific user.
You can store it as an string such as 'rock, pop, foo, ...'.
But if you want to manage tags, i think you should store tags in other table as #Alex M suggested.
it is just a simple question but i want to solve it as best as possible.
There will be a table in html, which would be filled with data from a mysql query,
for example:
name | street | zip
What i want is to make this changeable, so that user can directly change the results -> click on save -> Update via mysql.
My plan was now, to display the data in input fields which are named like this:
name_id | street_id | zip_id
By clicking on save i would perform a while-loop to get all names, streets, and zips of the id and perform an mysql update afterwords. I know that this would be possible that way (i already used this 1 time).
But: is there a more easier and better solution for this problem?
BR
If you plan to use javascript this is the way to do it!
I was just working with this a few hours ago.
http://www.jeasyui.com/extension/edatagrid.php
Updating all elements could cause heavy load on your server, depending on the amount of data you have stored. A more performance-friendly solution would be to "save" the row keys of the data that you changed. If you do this, you would only update the rows that actually changed, and not all rows!
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.