PHP Script to populate MySQL tables - php

Is anyone aware of a script/class (preferably in PHP) that would parse a given MySQL table's structure and then fill it with x number of rows of random test data based on the field types?
I have never seen or heard of something like this and thought I would check before writing one myself.

What you are after would be a data generator.
There is one available here which i had bookmarked but i haven't got around to trying it yet.

Related

is there a tool or any quick way to extract db tables automatically from my php codes?

I recently started to re-engineer a PHP-mysql project which was created about 7 years ago. I have only php and html codes and no mysql database or any document which shows the database structures.
Is there any tool which help me extract the tables of my database using php files? in the php files i have insert queries and select queries and also update.
I think of a tool (such a crawler) which takes my php files as input and create some sql create table queries as output.
I'm afraid there is no such tool. There's no guarantee that your PHP code even makes reference to every table or every column. You might see code like this:
mysqli_query("INSERT INTO mytable VALUES ('1234', 'abc', NULL, DEFAULT)");
What are the column names? What are their data types? Is the first column an integer, or is it just the developer's habit to put numbers inside string delimiters? What's the default value referenced by the fourth column? How could an automated tool infer these things by scanning this code?
Many details such as triggers, constraints, and indexes, are not referenced at all by PHP code, but they're necessary to make the application work.
If the database has any stored procedures, the PHP code wouldn't have any knowledge of the logic inside the procedure.
mysqli_query("CALL myprocedure(1234, 'north')");
The same problem exists for the query in VIEW definitions.
Reverse engineering this project is going to be a time-consuming forensic task, plus a lot of guesswork.
It really illustrates the importance of including the current schema dump with the source code of a project.

Recreate a database using existing php code

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.

Search mysql if json key not empty

I've got a json string stored in a mySQL DB and now I'm trying to find a way to check if a specific key contains a value at all or not.
Been googling around, but most solutions point to finding a specific value, where instead I want to check if there's a value there or not.
This is to be implemented into some sort of php check function, and if there's a way to get all results from mysql instead of doing multiple queries, that'd be great.
Example:
row 1 {"Name":"Jane","Group":"","customernumber":"12345"}
row 2 {"Name":"Mike","Group":"Sales","customernumber":"23456"}
row 3 {"Name":"Steve","Group":"","customernumber":"34567"}
The resulting array would contain Mike with details and so on.
A little help, please?
EDIT
I didn't choose to store the data like this. It's the CMS I'm working with that stores custom form like this.
I've got about 400 db entries and I thought of letting MySQL do the processing since I don't know if storing that many results in an array would be bad for performance since a couple of users are going to view pages that uses these results causing quite frequent requests.

Autocomplete concept

I'm programming a search engine for my website in PHP, SQL and JQuery. I have experience in adding autocomplete with existing data in the database (i.e. searching article titles). But what about if I want to use the most common search queries that the users type, something similar to the one Google has, without having so much users to contribute to the creation of the data (most common queries)? Is there some kind of open-source SQL table with autocomplete data in it or something similar?
As of now use the static data that you have for auto complete.
Create another table in your database to store the actual user queries. The schema of the table can be <queryID, query, count> where count is incremented each time same query is supplied by some other user [Kind of Rank]. N-Gram Index (so that you could also auto-complete something like "Manchester United" when person just types "United", i.e. not just with the starting string) the queries and simply return the top N after sorting using count.
The above table will gradually keep on improving as and when your user base starts increasing.
One more thing, the Algorithm for accomplishing your task is pretty simple. However the real challenge lies in returning the data to be displayed in fraction of seconds. So when your query database/store size increases then you can use a search engine like Solr/Sphinx to search for you which will be pretty fast in returning back the results to be rendered.
You can use Lucene Search Engiine for this functionality.Refer this link
or you may also give look to Lucene Solr Autocomplete...
Google has (and having) thousands of entries which are arranged according to (day, time, geolocation, language....) and it is increasing by the entries of users, whenever user types a word the system checks the table of "mostly used words belonged to that location+day+time" + (if no answer) then "general words". So for that you should categorize every word entered by users, or make general word-relation table of you database, where the most suitable searched answer will be referenced to.
Yesterday I stumbled on something that answered my question. Google draws autocomplete suggestions from this XML file, so it is wise to use it if you have little users to create your own database with keywords:
http://google.com/complete/search?q=[keyword]&output=toolbar
Just replacing [keyword] with some word will give suggestions about that word then the taks is just to parse the returned xml and format the output to suit your needs.

Php / MySql advice

I need to have a button to fire an action to copy all records from a defined client from one database to another with php.
The template database has 12 tables (diferent rows on each) but all with the row client_id to make the WHERE clausule work properly.
The question is, how do I do this?
Thanks,
Pluda
Since PHP is a Server-side programming language, you can't copy something from the client. You can however upload Data (like XML), parse it and then insert it into your MySQL Database.
If you want to copy records from one to another database, you might want to read from the Database and save them in a format like SQL. Then, you could send those querys to the second Database.
An advise at this point: If you need to make the same Query (with different values) over and over again, you should use a PreparedStatement. It will be compiled in the Database and then just filled out with new values. This is way faster then using an Insert every time.

Categories