I've grown quite fond of jsfiddle and how easy it is to use.
Does anyone know of something that works with mysql and maybe php mixed in?
You might be interested in my site: http://sqlfiddle.com. I've built it only recently, but it does support a decent range of database types (including MySQL) and has gotten a fair amount of use lately here on StackOverflow (see the mention on the sql wiki). You can build indexes, and views, and do nearly anything you would normally want to do within a database. Be sure to check out some of the sample fiddles, or see how various other SO users are using it:
MySQL query - optimized -> http://sqlfiddle.com/#!2/1fde2/39
Multilevel Users in the Database table -> http://sqlfiddle.com/#!2/0de1f/7
How to compare a value with a csv value in mysql? -> http://sqlfiddle.com/#!2/b642c/4
I guess I should mention that one other potential useful feature for SO would be that each query displays its execution plan, so if multiple people submitted answers to a sql question, you could easily evaluate their efficiency and then upvote/accept accordingly.
Try SQLize.
It has some annoying limitations, like the inability to create views, but overall I find it very useful. (Tip: CREATE INDEX also doesn't work, but you can still create indexes inside CREATE TABLE.)
Related
I am working with a real-estate database in mySql. This is used with a wordpress site.
Each property that is listed has related agents in a joined table. There may be more than 1 agent to a property.
However, multiple agents are listed in a single field. (So, join is 1-1).
Here is an example
a:2:{i:0;s:4:"1995";i:1;s:3:"260";}
So it appears that this is some sort of array implementation. I don't have much experience with this, as it is a bad way of storing data in a relational DB anyway.
In this example, there are 2 agents.
I know that:
- the first "2" probably refers to the number of agents.
- "1995" and "260" are the record-ids of the agent's record.
How would I craft a SQL statement to extract these two record ids (and find related data in the main record) from php?
If this is a cumbersome solution, I'd want to create a new SQL view to make access easier. Is this possible?
If there are any, I can't find any php functions that interprets this field.
Table redesign is not an option.
Edit:
As I was reading through the answers to a similar question as pointed out, I came to the conclusion that this is most probably impossible to do. And frankly ridiculous.
The solution entailed using the SUBSTRING_INDEX repeatedly. How many times, could vary every time. Even if the number of values is specified, it may not be possible to accomplish in MySQL. I will have to work with the limitations that this creates.
The data format you mention is php's serialization format. Before JSON took over the world, Rasmus Lerdorf and the php creators came up with their own way of doing it.
WordPress uses lots of this stuff.
php's unserialize() function converts it from that string representation into an appropriate php object.
If you must, you can probably use MySQL string functions like LOCATE() and SUBSTRING() to extract certain values from those serialized strings. But that will be a huge pain in the neck, and very specific to the kind of object that's serialized.
I know that there is an abundance of articles and questions to related topic and I Have been researching them from a few days now and I am more confused then ever.
I am newbie to website world and I learned and almost fully developed a website using MySQL, PHP and JavaScript. I was adding search feature using elastic search and I got confused if I should stick with MySQL and use it in conjunction with ElasticSearch or should I change to any NoSQL db?
Presently I have interconnected tables like users (this table has lots of optional fields) and customers(which has a column for different role of customer), articles, likes, rating, comments and appointment tables.
The data in these tables might need to change later, and I do require filtering and personalisation. I would be using an external payment gateway.
I have no experience in noSql or newSql. I fear that if I change to noSql the data might be a mess. Also, I would like to realise it quickly. That being said, it would be easier to switch now then later as I don't have any data stored.
Should I change the type of database? If yes to which and can I do that by just changing my sql queries in php to no SQL (how to do that)? How much of the coding will I have to change and how to do it efficiently? Or could/should I add data for personalisation to ElasticSearch?
Thanks for your help and please bear with me if this seems like an obvious choice!
So I have an old website which was coded over an extended period of time but has been inactive for 3 or so years. I have the full PHP source to the site, but the problem is I do not have a backup of the database any longer. I'm wondering what the best solution to recreating the database would be? It is a large site so manually going through each PHP file and trying to keep track of which tables are referenced is no small task. I've tried googling for the answer but have had no luck. Does anyone know of any tools that are available to help extract this information from the PHP and at least give me the basis of a database skeleton? Otherwise, has anyone ever had to do this? Any tips to help me along and possibly speed up the process? It is a mySQL database I'm trying to use.
The way I would do it:
Write a subset of SQLi or whatever interface was used to access the DB to intercept all DB accesses.
Replace all DB accesses with the dummy version of yours.
The basic idea is to emulate the DB so that the PHP code runs long enough to activate the various DB accesses, which in turn will allow you to analyze the way the DB is built and used.
From within these dummy functions:
print the SQL code used
regenerate just enough dummy results to let the rest of the code run, based on the tables and fields mentioned in the query parameters and the PHP code that retrieves them (you won't learn much from a SELECT *, but you can see what fields the PHP code expects to get from it)
once you have understood enough of the DB structure, recreate the tables and let the original code work on them little by little
have the previous designer flogged to death for not having provided a way to recreate the DB programatically
There are currently two answers based on the information you provided.
1) you can't do this
PHP is a typeless language. you could check you sql statements for finding field and table names. but it will not complete. if there is a select * from table, you can't see the fields. so you need to check there php accesses the fields. maybe by name or by index. you could be happy if this is done by name, because you can extract the name of the fields. finally the data types will missing. also missing: where are is an index on, what are primary keys, constrains etc.
2) easy, yes you can!
because your php is using a modern framework with contains a orm. this created the database for you. a meta information are included in the php classes/design.
just check the manual how to recreate the database.
Okay, I know this i kinda a vague question. But I have a requirement where I need to list all the tables and the columns which are not being used in the application. I can do manual code search but that would take so much time. The number of tables to check are 140 and maximum number of fields in a table are 90.
Right now I have started searching the code for table names, and I have created an excel sheet with all the table names, and when I found a table in code I highlight that in green. So tables are bit easier.
My question really is, is there a way to speed up this process? or some methods / techniques can be applied?
Thank you?
All depends on your app size and the coding technique.
If I had a large application, I would enable full mysql log (or hack into any database wrapper I may have, to log the queries), run the application, and then extract the information from the log. However, doing so you are just moving the problem. Instead of worrying to capture all the queries, you now need to ensure to run each and every line of your application code (so you are sure that nothing escaped and that you have analyzed all the possibilities).
This is in fact called "code coverage analysis" and there are tools which will help you with that.
This said, I believe that the manual analysis may be quicker for small applications.
I suggest you to build a script that perform the job. For example, you can optain the table list in a database with a query like this:
show tables from YOUR_DATABASE;
More info: http://dev.mysql.com/doc/refman/5.0/en/show-tables.html
And then you can loop your tables and check for fields using:
show columns from YOUR_TABLE;
More info: http://dev.mysql.com/doc/refman/5.0/en/show-columns.html
Finally you can search (grep for example) your tables and fields in your code and write a log or something similar.
I'm planning a PHP website architecture. It will be a small website with few visitors and small set of data. The data is modified exclusively by a single user (administrator).
To make things easier, I don't want to bother with a real database or XML data. I think about storing all data through PHP serialization into several files. So for example if there are several categories, I will store an array containing Category class instances for each category.
Are there any pitfalls using PHP serialization in those circumstances?
Use databases -- it is not that difficult and any extra time spent will be well learnt with database use.
The pitfalls I see are as Yehonatan mentioned:
1. Maintenance and adding functionality.
2. No easy way to query or look at data.
3. Very insecure -- take a look at "hackthissite.org". A lot of the beginning examples have to do with hacking where someone put the data hard coded in files.
4. Serialization will work for one array, meaning one table. If you have to do anything like have parent categories that have to match up to other data, not going to work so well.
The pitfalls come when with maintenance and adding functionality.
it is a very good way to learn but you will appreciate databases more after the lessons.
I tried to implement PHP serialization to store website data. For those who want to do the same thing, here's a feedback from the project started a few months ago and heavily modified since:
Pros:
It was very easy to load and save data. I don't have to write SQL queries, optimize them, etc. The code is shorter (with parametrized SQL queries, it may grow a lot).
The deployment does not require additional effort. We don't care about what is supported on the web server: if there is just PHP with no additional extensions, database servers, etc., the website will still work. Sqlite is a good thing, but it is not possible to install it on some servers, and it also requires a PHP extension.
We don't have to care about updating a database server, nor about the database server to use (thus avoiding the scenario where the customer wants to migrate from Microsoft SQL Server to Oracle, etc.).
We can add more properties to the objects without having to break everything (just like we can add other columns to the database).
Cons:
Like Kerry said in his answer, there is "no easy way to query or look at data". It means that any business intelligence/statistics cases are impossible or require a huge amount of work. By the way, some basic scenarios become extremely complicated. Let's say we store products and we want to know how much products there are. Instead of just writing select count(1) from Products, in my case it requires to create a PHP file just for that, load all data then count the number of items, sometimes by adding stuff manually.
Some changes required to implement data migration, which was painful and required more work than just executing an SQL query.
To conclude, I would recommend using PHP serialization for storing data of a small website modified by a single person only if all the following conditions are true:
The deployment context is unknown and there are chances to have a server which supports only basic PHP with no extensions,
Nobody cares about business intelligence or similar usages of the information,
There will be no changes to the requirements with large impact on the data structure.
I would say use a small database like sqlite if you don't want to go through setting up a full db server. However I will also say that serializing an array and storing that in a text file is pretty dang fast. I've had to serialize an array with a few thousand records (a dump from a database) and used that as a temp database when our DB server was being rebuilt for a few days.