Database design for tagging - php

I want to implement a tagging system for entries in my database. I did some research and I guess that this is the best way to do it...however I'm not too experienced with MySQL (or PHP for that matter - I'm just making a sort of web app for fun) and thus I am confused on how to implement this.

Your going to need to create the database in MySQL and then you will need to use PHP to create the user interface (HTML) and the code to access the database. Since you are new I've listed out some getting started resources for each. Good Luck!
Here's some resources for getting started with MySQL:
http://code.google.com/edu/tools101/mysql.html
http://a.parsons.edu/~kosoyj/2009/spring/pgte_5044/2009/03/24/mysql-101/
http://www.killerphp.com/mysql/
http://www.webdevelopersnotes.com/tutorials/sql/mysql_database_introduction_mysql_beginners_tutorial.php3
And some for basics/getting started with PHP:
http://devzone.zend.com/article/627
http://www.homeandlearn.co.uk/php/php.html
http://www.webmonkey.com/2010/02/php_tutorial_for_beginners/
Learning PHP and MySQL

The link you provided is ideal. You should just start in on learning how to make a many-to-many relationship as the link explains. But what are you using to edit PHP or try out queries?

Related

Fetching data through html from a database

enter image description here
I am trying to make a database of passport applicants who will be able to know the status of their passports through the html form that's shown in the image attached above. But, what's the best way to do this and by using which form of database? Thank you.
With just HTML you cannot do it.
Either go with server side apps like PHP and use jQuery so user wont see the page getting refreshed.
Or go with Node JS framework.
Follow the simple CRUD operation.
Its too much to type in this comment box.. so sharing the link.
https://www.taniarascia.com/create-a-simple-database-app-connecting-to-mysql-with-php/
Just use a REST Api to fulfill your requirement.There are lots of tutorials on various websites that you can learn those things.Simply CRUD operation means Create,Read,Update,Delete.
First of all study about databases and their usages.If you are going to use nodejs solution i'm recommending you for go to mongoDb.Or else if you are using PHP based solution, Mysql will be ideal.Hope this will help.
In my cases I use ASP.NET Model View Controller Framework with the Entity Framework, with MSSQL Databases. It does use C# for the server side, but I find it pretty easy to use since it generates the basic CRUD operations for you when you connect the database.
Here is a great tutorial on doing this: https://www.c-sharpcorner.com/UploadFile/4d9083/how-to-connect-ado-net-entity-framework-with-mvc-in-simple-s/
Good luck!

Advice on frameworks for creating web front-ends and reports of MySQL database

I am using a commercial PHP web application that stores information in a mysql database, and find myself needing to create some custom reports on that database information, ideally presented via the web with the ability of exporting the reports to PDF or some external format as well.
I could just slap together some PHP to query the DB and then show the results of SQL queries against that DB, but was hoping there may be a more intelligent framework I could employ to generate these reports faster and easier now as well as in the future. Codeigniter looks like it may be a good starting point, but I'm not in love with it. What do people use when they need to work with an existing SQL db info but dont want to roll it all from scratch?
edit - I know php/python/ruby enough to operate, but I'm rusty so starting from scratch will make the process longer than it probably needs to be. I'm looking to leverage quality frameworks if they exist to give me the best results in the longrun
I would recommend Django, it has a management command that can help automatically generate models from an existing database, inspectdb. You could leverage that to quickly get going and start using Django's powerful ORM to build your reports.

When should I create php files to create tables in my sql database?

I have been up to recently creating all of my tables through php codes rather than using something like phpMyAdmin. (that's how it was done in the book I was learning so I just assumed that was the proper way until I started looking through more online tutorials) I have the feeling that this is a bad habit and have started using phpMyAdmin, but was wondering in what kind of situations it would actually be good practice to writing php coded instructions in creating sql tables.
There is no inherent danger to creating tables with php, as long as you are properly doing it. In fact, that's all phpMyAdmin is - it's just a php script that does the same thing (only with a fancier interface).
The only real danger here is if you have a habit of leaving your creation scripts laying around and not properly secured. But the same can be said about any script that has access to your database.
But anyways, the advantage of using a tool like phpMyAdmin is that it automates a lot of stuff for you that you probably aren't otherwise thinking about.
And to answer your question about when is it a good time to do it in general... installation scripts (like cms or forum scripts) usually create tables during setup/install process.
IMO only if you want to create an installer for your web applications, installers like some other apps have, like: joomla,drupal, wordpress,etc. Allowing to the user to create everything automatically and not create all tables by hand.
That is mainly a good idea if you want to populate your database with a big amount of data. You could just create a php program that would load your database with data. However, this can be done, and even more effectively using a language like python or ruby.
Generally speaking, just creating tables with php code does not seem like a good idea to me. You could just create the tables with mysql client and then dump the database to an sql file.
For example cms create tables from script in order to simplify all the procedure even for people without programming experience. This is a real scenario.

Connect Rails model to non-rails database

I'm creating a new web application (Rails 3 beta), of which pieces of it will access data from a legacy mysql database that a current php application is using.
I do not wish to modify the legacy db schema, I just want to be able to read/write to it, as well as the rails application having it's own database using activerecord for the newer stuff. I'm using mysql for the rails app, so I have the adapter installed.
How is the best way to do this? For example, I want contacts to come from the old database. Should I create a contacts controller, and manually call sql to get the variables for the views?
Or should I create a Contact model, and define attributes that match the fields in the database, and am I able to use it like Contact.mail_address to have it call "SELECT mailaddr FROM contacts WHERE id=Contact.id".
Sorry, I've never done much in Rails outside of the standard stuff that is documented well. I'm not sure of what the best approach would be. Ideally, I want the contacts to be presented to my rails application as native as possible, so that I can expose them RESTfully for API access.
Any suggestions and code examples would be much appreciated
This really depends on how esoteric your legacy db is. This affects the solution considerably. If your legacy db is quite similar to Rails conventions then using a model with a few customizations will probably prove as the best approach. However I've heard of people who wrote a script that constantly reimported data from the legacy db into a new db - the whole structure of the db was so wrong that approach was worth it.

PHP/MySQL: The right way creating a big website database based

I'm creating a movies website, IMDB.com like.. I'm really new to PHP and programming at all but I have some books and StackOverflow of course :)
I have already done lot of the work, but now I have more than 600 lines of code (PHO only) per page and more than 20 database tables only for storing and geting the movie data (many-to-many relationships)
Most of the code are MySQLi queries (prepared statements) to insert/get data and loops. Nothing fancy, just basic PHP/MySQL.
I have some questions:
It is recommended to use MySQLi prepared statements for every SQL query or is better just to use old MySQL for internal PHP/database queries and leave prepared statements for user input only?
To get all the movie data and show it I need to get the data from more than 16 different tables. I use one MySQL query per table (somethimes more) and lots of loops. This is the correct way a website need to work?? I mean.. this is normal???
How can I simplify the code to simplify the reading? Can I separete all those queries to external files?? Can I create functions to handle each table query??
Hope you can give me a hand and maybe you have some more suggestions for correctly building such a website.
Thanks!!
As mentioned in the anwsers above I would also point you to using a framework that impliments the MVC design pattern. Along with that most of the frameworks have an ORM built in but if they do not you can look at Symphony or EZPDO is another good ORM to tie into your Model in (M)VC.
CodeIgniter is a very fast and light weight MVC framework that would allow you to bootstrap pretty quickly but you may also want to look into ZF (ZendFramework). ZF has a great framework feature set and is pretty flexible overall.
Beyond that be sure to seperate your reads and your writes in either or Model or your calls to the ORM of choice. This will allow you to slave data to multiple MySQL Boxes for bigger performance but allows you to use one database engine to start out.
Add in the ability to use Memcached so that you can cache your data/objects vs hitting the database.
When using cache put some thought into how you would expire cache for a database update. In other words if your selecting data from a database to display in a view and that data is has not changed you should not need to hit the database every time but rather pull it from memory. Once the data actually does change you'd want to invalidate that cache so it's not stale and then re-cache the new data.
memcached: http://www.danga.com/memcached/'
-facebook also has a version
CodeIgniter - http://codeigniter.com/
EZPDO - http://www.ezpdo.net/blog/?p=2
ZendFramework -http://framework.zend.com/
Consider looking at or using a web framework for your website.
symfony
CakePHP
CodeIgniter
Are some of the more mainstream ones. Learn from them if anything.
Prepared statements are just fine for your own internal queries to. You'll have a strutured approach for all queries.
Well it depends on what you're showing. But I would say that you normally could use joins to get the data you need from more tables. You'll have a lot less quering to get the correct data, and it sounds like all your data is connected somehow to the one movie you're showing.
As Peter D comments, I would recommend using a web framework to learn how to seperate out the database handling from the view. Are you using an object oriented approach now? Look at the MVC pattern that some of these frameworks implement, that'll get you going.
Like Peter D mention before add this one to the list of framework to use. Zend Framework http://framework.zend.com
Open source and free.
It is recommended to use MySQLi ...
Definitely MySQLi, but it's a big question by itself, if you start coding you'll need to grasp the basic of T-SQL to understand the difference.
To get all the movie data ...
It depends on alot of things. Size of database. Wanted results i.e. the information that need to be displayed, response time of the queries vs displaying in user view. Do you know about JOIN, UNION?
How can I simplify the code to ...
Yes to all theses questions. www.w3schools.com/php/ if it can be of any help and learn the MVC pattern. Useful to alot of programming language these days. Maybe a framework would help you here
To this questions:
"To get all the movie data and show it
I need to get the data from more than
16 different tables. I use one MySQL
query per table (somethimes more) and
lots of loops. This is the correct way
a website need to work?? I mean.. this
is normal???"
No. If I understand you correctly, you should be using some type of JOIN depending on the data you're retrieving from the database. Getting results for huge amounts of data, and then picking out only the pieces you want in PHP is much slower than letting the database do the work of sorting/retrieving only the records/info you want to show.
I highly recommend a somewhat dated but very easy to grasp book that covers PHP and MySQL/Databases in general: http://www.dmcinsights.com/phpmysql2/ - It covers a lot of "in practice" techniques along with the code, so it'd be great to learn from.
There is a apparently a third edition with updated info, but I have not looked at it.

Categories