set auto_increment value to old table items - php

i have in one table two fields (title, content) and i inserted some values...:
+--------------------------------------+--------------------------+
| title | content |
+--------------------------------------+--------------------------+
| hello word | this is my first content |
+--------------------------------------+--------------------------+
| smoke Smoking is bad for your health | But i love it |
+--------------------------------------+--------------------------+
if I add a auto_increment field called ID, the previously input values ​​take ID = 0:
+---+--------------------------------------+--------------------------+
|ID | title | content |
+---+--------------------------------------+--------------------------+
| 0 | hello word | this is my first content |
+---+--------------------------------------+--------------------------+
| 0 | smoke Smoking is bad for your health | But i love it |
+---+--------------------------------------+--------------------------+
and i need this:
+---+--------------------------------------+--------------------------+
|ID | title | content |
+---+--------------------------------------+--------------------------+
| 1 | hello word | this is my first content |
+---+--------------------------------------+--------------------------+
| 2 | smoke Smoking is bad for your health | But i love it |
+---+--------------------------------------+--------------------------+
The only way to set correct ID to old values is php+foreach? or i forgot something to do with sql/mysql to takes faster and automatically the correct ID values?
Thanks for help.

Well,now its my time to help you :P.
You puts SQL code in a php page or in phpmyadmin online code editor? Sometimes phpmyadmin online editor fails, try it in your php page and it works fine.

ALTER TABLE MyTable
ADD ID INT IDENTITY

Related

Further specifying MySQL table data using Fetch_Array in PHP

I not that knowledgeable with PHP. I've spent a good portion of my day researching and I resort to you for help. I will try my best to articulate my setup and issue. I am creating a table that will display certain data from my database and I am a using 'tablesorter' jquery plugin.
+-----+------------+---------+
| id | m_key | m_value |
+-----+------------+---------+
| 262 | last_name | Bore |
| 262 | first_name | Dan |
| 261 | last_name | Bore |
| 261 | first_name | Dan |
| 255 | last_name | Nez |
| 255 | first_name | Bob |
| 250 | stock | 50 |
| 250 | price | 102 |
+-----+------------+---------+
^ Database Table
mysql_query("SELECT id,m_value FROM table WHERE m_key='last_name'")
The database table I am working with isn't as clean as I hoped for. The query line here does the filtering. It grabs any ID and m_value which has last_name as one of the values from m_key. Here is proof I have it working http://cl.ly/image/47461i2e412j.
while($row = mysql_fetch_array($rs)) {
echo "<tr><td>$row[id]</td><td>$row[m_value]</td><td>$row[m_value]</td></tr>\n";
}
This is the part I am stumped on. Most of my important data is under the m_value column. Easy enough the first $row[id] from the array successfully displays the ID and the next $row[m_value] seems to attribute itself to last_name. I would like the proceeding $row[m_value] to attribute the first_name. How can I go about specifying this through code?

MySQL Empty Cells In Normalised Table

OK, Last post on this subject (I hope). I've been trying to look into normalisation for tables in a website that I've been building and I have to be honest that I've struggled with it, however after my last post it seems that I may have finally grasped it and set my tables properly.
However, one question remains. If I create a table that is seemingly in 3rd normal form, is it acceptable to have areas of white space or empty cells if the data is relevant to that specific table? Let me give you an example:
On a news website I have an Authors_Table
+----+-----------+----------+-----------------+-------------------+---------+----------+---------+
| ID | FIRSTNAME | SURNAME | EMAIL | BIO ( REQUIRED ) | TWITTER | FACEBOOK | WEBSITE |
+----+-----------+----------+-----------------+-------------------+---------+----------+---------+
| 01 | Brian | Griffin | brian#gmail.com | About me... | URL | | URL |
| 02 | Meg | Griffin | meg#gmail.com | About me... | URL | | |
| 03 | Peter | Griffin | peter#gmail.com | About me... | | URL | URL |
| 04 | Glen | Quagmire | glen#gmail.com | About me... | URL | URL | |
+----+-----------+----------+-----------------+-------------------+---------+----------+---------+
This would be used on the article page to give a little details about who has written it, which is very common in newspapers and on modern blogs. Now the last 3 columns Facebook, Twitter, Website are obviously relevant to the Author & therefore to the PK (ID). As you know though, not everyone has either twitter or a wesbite or facebook so the content of these cells is rather flexible so obviously empty cells will occur in some cases.
It was suggested to do it another way so I produced:
Links
+----+-------------------+
| ID | TYPE |
+----+-------------------+
| 01 | Facebook |
| 02 | Twitter |
| 03 | Website |
+----+-------------------+
Author_Links
+----------+--------+------+
| AUTHOR | TYPE | LINK |
+----------+--------+------+
| 01 | 01 | URL |
| 01 | 02 | URL |
| 01 | 03 | URL |
| 02 | 02 | URL |
| 02 | 03 | URL |
| 03 | 01 | URL |
+----------+--------+------+
Now I understand the concept of this however isn't it just as "correct" to have and to use the original table. Updates can be made using a form & php to say:
$update_link_sql = "UPDATE authours SET facebook = ' NEW VALUE ' WHERE id = '$author_id'";
$update_link_res = mysqli_query($con, $update_links_sql);
As for me Authors_Table is correct.
| ID | FIRSTNAME | SURNAME | EMAIL | BIO ( REQUIRED ) | TWITTER | FACEBOOK | WEBSITE |
The only reason to have three tables:
Authors
| ID | FIRSTNAME | SURNAME | EMAIL | BIO ( REQUIRED ) |
Link_types
| ID | TYPE |
Author_links
| AUTHOR_ID | LINK_TYPE_ID | URL |
...is that your authors could have more than one link of specific type (for example two twitter accounts, btw, is it legal?)
If we suppose that any author can have no more than one account of each type - your version with single table is correct.
Either way is acceptable depending on functional requirements.
If you need to dynamically add more url types/fields to profile then use latter.
If there is ever going to be only 3 then former is better.
No need to over-engineer.
Yes, it's "correct" to store "optional" attributes as columns in the entity table. It's just when we have repeated values, e.g. multiple facebook pages for an author, for example, that we'd want to implement the child table. (We don't want to store "repeating" attributes in the entity table.)
As long as there's a restriction in the model, that an attribute will be limited to a single value (a single facebook page, a single twitter, etc.) those attributes can be stored in the entity table. We'd just use a NULL value to indicate that a value is not present.
One benefit of the separate table approach (outlined in your post) is that it would be "easier" to add a new "type" of URL. For example, if in the future we want to store a blogspot URL, or an instagram URL, instead of having to modify the entity table to add new columns, we can simply add rows to the "link_type" table and "author_link" table. That's the big benefit there.

how to maintain a continuous id number row count in mysql

i have an id coloumn which is integer and auto incremented type.
The problem is when ever i delete a row the continuity of the number breaks.
+----------------------+----+
| name | id |
+----------------------+----+
| mashable | 1 |
| Behance | 2 |
| Techcrunch | 3 |
| flipkart | 4 |
+----------------------+----+
FOR EXAMPLE if i delete the row with id=2, then i output in id will be
+----------------------+----+
| name | id |
+----------------------+----+
| mashable | 1 |
| Techcrunch | 3 |
| flipkart | 4 |
+----------------------+----+
but i want it to be like :
+----------------------+----+
| name | id |
+----------------------+----+
| mashable | 1 |
| Techcrunch | 2 |
| flipkart | 3 |
+----------------------+----+
How to do it ??
To directly answer your question, here's how you fix those gaps in sequential numeric fields: Fixing gaps in mysql table row id after we delete some of them
But let's be careful here for a moment.
Let's assume id is your primary key. ID's are usually the point of reference to an object, because auto-generated ID's are unique. Call it a convention.
That means that If ANY part of your code depends on the id column, your application will break.
If you NEED to do this, then use some other field as main reference. Perhaps an unique name field or something similar.
If ID is NOT your primary key, then you probably should've chosen another name for it to begin with. Anyway, in this case, the chances of you breaking anything are much smaller.
Notice that I said smaller, but not zero. We don't know your application, so it's possible that your code uses id for something important, and that'll mean trouble for you.

Grabbing individual elements in MySQL Table

I'm attempting to add Breadcrumbs to my website using a MySQL table, and I'm having difficulty at the moment.
I have a table named 'includes' created that stores information about the category, page, subpage, the title, and the ref (url) of the page. Category, Page, and Subpage are all php parameters passed from the page the user is on
My table is laid out like this:
|----------------------------------------------------------------|
| ID | Category | Page | Subpage | Title | Ref |
|----------------------------------------------------------------|
| 0 | | | | Home | ... |
| 1 | Software | | | Software | ... |
| 2 | Software | Desktop | | Desktop Software | ... |
| 3 | Software | Mobile | | Mobile Software | ... |
| 4 | Software | Desktop | Blah | Blah Blah | ... |
| ...
|----------------------------------------------------------------|
What I'm trying to do is make a query that will return only the required steps back to home for the breadcrumbs.
In other words, if the user is on "example.com/software/desktop/blah", the query will return rows 0,1,2, and 4. Or if I was on /software/mobile, it would only return rows 0,1, and 3.
My current attempts have been things like the following:
SELECT * FROM `includes` WHERE
`category` IS NULL AND `page` IS NULL AND `subpage` IS NULL OR
`category`='$category' AND `page` IS NULL AND `subpage` IS NULL OR
`category`='$category' AND `page`='$page' AND `subpage` IS NULL OR
`category`='$category' AND `page`='$page' AND `subpage`='$subpage'
Which not only don't work, but also seem more complex than it should have to be.
I'm probably overcomplicating this, or possibly just doing an entirely wrong method, which is why I've turned here.
Does anyone have a possible solution to this? Should I be looking at a more complex query? (admittedly, SQL is not my forte) Or should I be looking at a new SQL table, or possibly an entirely different method?
What you have is a hierarchical structure. The data is set up with parent-child relationships. There is a good description on how to work with hierarchical data here: http://explainextended.com/2009/03/17/hierarchical-queries-in-mysql/
You can make a self relation table like this
id | parent_it | title | Ref
1 | 0 | Home | ...
2 | 1 | Software | ...
3 | 2 | Desktop | ...
4 | 2 | Mobile | ...
5 | 3 | Blah | ...
So your query should get the last element
SELECT * FROM includes WHERE
tilte = 'Blah'
And then get the parent ID title and so on , like this the table structure will be better from my point of view & experience
OR
Generate your query based on the values you get , with simple loop count the arguments and based on that generate the query string then execute it
I hope this can help :)

Compare two MySQL tables to filter results

I have a MySQL database with the following structure:
Table1
UniqueID (int) (PrimaryKey & AutoIncrement)
TitleID (varchar)
DescriptionID (varchar)
ContentRating (varchar)
Table2
UID (int) (PrimaryKey & AutoIncrement)
Activity (varchar)
ContentLimit (varchar)
What I want to do is take the value from ContentLimit (in Table2) and compare it against (ContentRating) in Table1, if they match then show all the rows that match. I'm using PHP and MySQL to achieve this.
Below is an Example:
Table1
UniqueID | TitleID | DescriptionID | ContentRating
------------------------------------------------------
1 | Hello | I Am Text | Universal
2 | Again | Yet More Text | Universal
3 | This | Yet More Text | Universal
4 | Is | Yet More Text | Parental Guidance
5 | Some | Yet More Text | Universal
6 | Dummy | Yet More Text | Parental Guidance
7 | Text | Yet More Text | Parental Guidance
8 | I | Yet More Text | Parental Guidance
9 | Think | Yet More Text | Parental Guidance
Table2
UID | Name | Activity | ContentLimit
---------------------------------------------
1 | John Smith | IsActive | Universal
2 | Jane Smith | IsActive | Universal
3 | Felix Tiger | IsActive | Parental Guidance
4 | Spring Load | InActive | Universal
If "Felix Tiger" was logged in then he would be able to see anything submitted with a "Parental Guidance" rating as well as anything with a "Universal" Rating.
But If "Jane Smith" was logged in then she would only be able to view anything submitted with a "Universal" rating
I apologise if I am unclear and will make clear anything that may be mis-read or hard to understand.
Thank you in advance for any help given.
Try this:
SELECT
a.*,
b.*
FROM table1 a
JOIN table2 b ON a.ContentRating = b.ContentLimit

Categories