Archive | Download RSS feed for this section

MySQL RAND function

6 Nov

The begining of this story is a PHPNuke module. The module has a block that picks a random item from a database table and displays it. The content of the table never changes. Here is how the programmer of the block does it:

He knows that there are 6899 rows because the last id in the table has a value of 6899. So with the “rand” PHP function, he picks a random number from 1 to 6899 and then queries the database table with a WHERE clause that checks if the value of the id is equal to the picked number:

srand(time());
$random = (rand(1,6899));
$res = mysql_query ("select * from my_table where id=$random");

The problem here is that because of some deleted rows (he has forgotten he deleted them) in the table, the id is not a continuous sequence from 1 to 6899 and that’s why some times the picked number (as a value of the id) not exists in the table and the PHPNuke block displays an empty box.

The RAND function in MySQL is precisely there to prevent this kind of problem. Instead of picking a random id with a PHP code, let the server do it for you:

SELECT * FROM my_table ORDER BY RAND() LIMIT 1;

This query returns a random row from the table my_table, no matter how many rows there are and no matter if the id is a continuous sequence of numbers or not. With this query you are sure you get a random row every single time.

Top 10 free MySQL tools

6 Sep

Here are some of the best free MySQL tools out there. The description is not my review of the product, it is just what the developers say about their tools. If you know some other good ones, you can drop a comment.

From MySQL:
MySQL Migration Toolkit: The MySQL Migration Toolkit is a graphical tool provided by MySQL AB for migrating schema and data from various relational database systems to MySQL. MySQL Migration Toolkit is designed to work with MySQL versions 5.0 and higher.

MySQL Administrator
: MySQL Administrator is a program for performing administrative operations, such as configuring your MySQL server, monitoring its status and performance, starting and stopping it, managing users and connections, performing backups, and a number of other administrative tasks.

MySQL Query Browser: The MySQL Query Browser is a graphical tool provided by MySQL AB for creating, executing, and optimizing queries in a graphical environment. Where the MySQL Administrator is designed to administer a MySQL server, the MySQL Query Browser is designed to help you query and analyze data stored within your MySQL database.

(more…)

phpMySQLConsole v0.1

25 Aug

phpMySQLConsole is a powerful and most original way to operateĀ  your MySQL databases via a simple web interface. You wil sense power of SQL language with phpMySQLConsole.

It’s made with PHP/AJAX/CSS/HTML. It’s Open Source.

Benefits

  • Dont require shell account on server.
  • No any changing on firewall configuration to cause posible security weakness.
  • It is platform independent.
  • It can operate on all operation systems and web browsers that have support for javascript.
  • Its usefull for dump and load operations on large databases.

Features

  • Support for all posible SQL query sentences.
  • Fastest command interaction with server. Thanks to AJAX.
  • Auto select&copy function similar to behaviour of *nix terminal window.
  • Detailed help for console commands (type help command)
  • Command history. Remembers previously entered commands (use up/down arrow keys).
  • Full customizable interface with CSS support.
  • Allows to log commands to text file.

(more…)