Visit: http://earthhour.in/

It is simple in MySql to query some from a table by its ID and order it using another field say "rating"
here is the php code for that


$data = mysql_query(SELECT * FROM (SELECT * FROM table_name ORDER BY ID DESC LIMIT 50) AS ttbl ORDER BY rating ASC;


ttbl is a virtual table or view (I don't know what exactly it is)

Category : , | Read More...... edit post

One of my client gave me a huge file which contain thousands of web urls, he want that into a html file having html (< a) tags with those site addresses as links.

eg: the input file contain
1. thecoderin.blogspot.com < http://thecoderin.blogspot.com/>
2. keltrondays.blogspot.com < http://keltrondays.blogspot.com/>
3. sahabm.blogspot.com < http://sahabm.blogspot.com/>
4. linuxlight.blogspot.com < http://linuxlight.blogspot.com>

the php code for convert this into format is given below:


<?

$file_to_read = "lines.txt";
$file_to_write = "output.txt";


$fh = fopen("$file_to_read", "r");
$i = 1;
while ($line = fgets($fh)) {
$row[$i] = $line;
$i++;
}
fclose($fh);



$fh = fopen($file_to_write, 'w') or die("can't open file");

$a = 1;
$max = count($row);

while ($a <= $max) {

$var[$a] = explode(" ",$row[$a]);

$data[$a] = "<a href='http://".$var[$a][1]."'>".$var[$a][1]."</a>\n";

echo $data[$a];

/*
fwrite($fh, $data[$a]);
*/



$a++;
}

#print_r($row);
?>

Category : , | Read More...... edit post

Consider a html page which having several comments; which is not good for mobile webpages, as its size must be very low. So better rebuild the html page.

Here a script written using php, where it removes the html comments (including the matter inside <!-- and -->

single line code for that


$str = preg_replace('/<!--*?-->/i', '', $str);

Category : | Read More...... edit post

Wondered ? Yes it is possible using proc_open command for running terminal operations. But I am not sure about Windows Machines.

The Code goes here:


$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "filename.txt", "w") // stderr is a file to write to
);
$command = "g++ cplusplusfile.cpp";
proc_open($command,$descriptospec,$output);

Category : , | Read More...... edit post

To take backup:


All database of a user : mysqldump -Q -u[User] -A -p > foo.sql
particular Database : mysqldump -Q -u[dbUser] -B[dbName] -p > bar.sql


Restore

mysql -u[UserName] -p
mysql > source foo.sql;

mysql -u[UserName] -p
mysql > create database user_bar; # for restoring single database
mysql > source bar.sql;


To dump all database from a remote server to local server The best option is
follows

------------------------------------------------------------------------------------------------------------------------------------------------
#mysqldump --all-databases --add-drop-table --host=10.10.10.1 -p > foo.dump
--------------------------------------------------------------------------------------------------------------------------------------------------

To Restore the dump
#mysql < foo.dump

Category : | Read More...... edit post

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Causes of Access Denied:
1. Make sure that the server is running. If it is not, clients cannot connect to it.
2. It might be that the server is running, but you are trying to connect using a TCP/IP port, named pipe, or Unix socket file different from the one on which the server is listening. To correct this when you invoke a client program, specify a --port option to indicate the proper port number, or a --socket
option to indicate the proper named pipe or Unix socket file. To find out where the socket file is, you can use this command:


shell> netstat -ln | grep mysql

There is many more reasons, which are enlisted on the MySql site

Solution: You can easily override this problem by using the following commands on the terminal.



# /etc/init.d/mysql stop
# mysqld --skip-grant-tables --user=root&
# mysql
mysql> use mysql
mysql> update user set Password='' where User='root';
mysql> exit
# killall -9 mysqld
# /etc/init.d/mysql start
# mysql -p
Enter password:
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');
mysql> FLUSH PRIVILEGES;
mysql> exit

This will flush out all the previlage and set a new root password.

I got busted with some easy mySql questions; anyway I expect this heck again and again. So I buried my self in google to find out the answers:

and here we go!

1. What is the difference between primary key and unique key?

Primary Key:

This is an index that cannot be NULL, Primary Keys are used in building relationships between tables in a database. (an index is automatically created on the primary key). The difference between primary and ordinary keys is that there can be multiple keys, but only one primary key.


Unique Key:

Unique and Index are same, the difference is, in Unique, duplicate are not allowed in any circumstances and that is enforced by database server.


2. How can you get rows in between of two keys ?

It can be done by using sql between.


Syntax:
********** SELECT <fields> FROM <table> WHERE <key_field> BETWEEN <key1> AND <key2>


Example : select f_name, l_name, age from employee_data where age BETWEEN 32 AND 40;

Followers

FeedCount

Latest Posts

thecoderinc

Subscribe Now