PHP: Creating a downloadable CSV file from a MySQL table using PHP. 
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// output the column headings
fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));

// fetch the data
mysql_connect('localhost', 'username', 'password');
mysql_select_db('database');
$rows = mysql_query('SELECT field1,field2,field3 FROM table');

// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);

//NEEDS TESTING.

[ view entry ] ( 1966 views )   |  print article
Dropping, Adding or Repositioning a Column (ALTER command) 

Suppose you want to drop an existing column i from above MySQL table then you will use DROP clause along with ALTER command as follows:

mysql> ALTER TABLE testalter_tbl  DROP i;

A DROP will not work if the column is the only one left in the table.

To add a column, use ADD and specify the column definition. The following statement restores the i column to testalter_tbl:

mysql> ALTER TABLE testalter_tbl ADD i INT;

After issuing this statement, testalter will contain the same two columns that it had when you first created the table, but will not have quite the same structure. That's because new columns are added to the end of the table by default. So even though i originally was the first column in mytbl, now it is the last one.

mysql> SHOW COLUMNS FROM testalter_tbl;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| c     | char(1) | YES  |     | NULL    |       |
| i     | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

To indicate that you want a column at a specific position within the table, either use FIRST to make it the first column or AFTER col_name to indicate that the new column should be placed after col_name. Try the following ALTER TABLE statements, using SHOW COLUMNS after each one to see what effect each one has:

ALTER TABLE testalter_tbl DROP i;
ALTER TABLE testalter_tbl ADD i INT FIRST;
ALTER TABLE testalter_tbl DROP i;
ALTER TABLE testalter_tbl ADD i INT AFTER c;

The FIRST and AFTER specifiers work only with the ADD clause. This means that if you want to reposition an existing column within a table, you first must DROP it and then ADD it at the new position.



:)

http://www.tutorialspoint.com/mysql/mys ... ommand.htm

[ view entry ] ( 6985 views )   |  print article
MySQL: CAST example 
SELECT 
CAST(CASE eoy
WHEN 0 THEN 'No Voted'
ELSE category/* Keep Original Value*/
END as CHAR) AS Result
FROM `vote_email`


//see all
SELECT eoy FROM vote_email


[ view entry ] ( 1674 views )   |  print article
MySQL: Case Expression vs Case Statement 
The CASE expression evaluates to a value, i.e. it is used to evaluate to one of a set of results, based on some condition.
Example:

SELECT CASE
WHEN type = 1 THEN 'foo'
WHEN type = 2 THEN 'bar'
ELSE 'baz'
END AS name_for_numeric_type
FROM sometable`


The CASE statement executes one of a set of statements, based on some condition.
Example:

CASE
WHEN action = 'update' THEN
UPDATE sometable SET column = value WHERE condition;
WHEN action = 'create' THEN
INSERT INTO sometable (column) VALUES (value);
END CASE


You see how they are similar, but the statement does not evaluate to a value and can be used on its own, while the expression needs to be a part of an expression, e.g. a query or an assignment. You cannot use the statement in a query, since a query cannot contain statements, only expressions that need to evaluate to something (the query itself is a statement, in a way), e.g. SELECT CASE WHEN condition THEN UPDATE table SET something; END CASE makes no sense.

http://stackoverflow.com/questions/1243 ... -statement

[ view entry ] ( 1418 views )   |  print article
MySQL: User Management 
// view all users
SELECT user,host FROM mysql.user;

//drop a user
DROP USER 'dev-webuser'@'localhost';

//create a user and assign password
GRANT ALL PRIVILEGES ON db_name.* TO 'webuser'@'localhost' IDENTIFIED BY 'secret123'

// disply logged in grants
SHOW GRANTS;



//
CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass';

//
GRANT SELECT,INSERT,UPDATE,DELETE ON my_db.* TO 'michael'@'localhost';

//
GRANT ALL ON my_db.* TO 'michael'@'localhost';

//
GRANT ALL ON my_db.* TO 'my_user'@localhost IDENTIFIED BY 'my_pass';


//update user's password:
SET PASSWORD FOR 'webuser'@'localhost' = PASSWORD('dev123');

// MySQL 8 :
root@5f288b2166ba:/# mysql -p
...
mysql>
mysql> CREATE USER 'coolUser'@'%' IDENTIFIED WITH mysql_native_password BY 'YYYYYY';
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> grant all on *.* to 'coolUser'@'%';
Query OK, 0 rows affected (0.01 sec)

#Starting with MySQL 8 you no longer can (implicitly) create a user using the GRANT command. Use CREATE USER instead, followed by the GRANT statement:
mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'PASSWORD';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;


[ view entry ] ( 1742 views )   |  print article
RPM Basic Tasks 
//package info
rpm -qi "package"

//Listing all installed packages
rpm -qa

//remove a package
rpm -e "package"
rpm -e "package" "package" //dependencies

// install a package
rpm -Uvh package_file.rpm

[ view entry ] ( 1870 views )   |  print article
Basic Tasks: memcached 
[acool@acool ~]$ telnet localhost 11211
Trying ::1...
Connected to localhost.
Escape character is '^]'.
stats
...
stats slabs
...
stats items
...
stats cachedump 15 0
ITEM memc.sess.key.tb7mgf58nb0pnn7ag840q5r5k6 [1875 b; 1467144640 s]
ITEM memc.sess.key.9nt5nrcptemi2m0d9g3k95vl76 [1911 b; 1467144640 s]
ITEM memc.sess.key.5k8d2qpko4lg4rn6fue1ces0j6 [230 b; 1467144640 s]
...
get memc.sess.key.5k8d2qpko4lg4rn6fue1ces0j6
...
delete memc.sess.key.5k8d2qpko4lg4rn6fue1ces0j6
DELETED

set MyJson 0 100 16
{"name":"angel"}
STORED

flush_all
OK

quit
[acool@acool ~]$



Also see memcached-tool utility.


//flush memcached
echo 'flush_all' | nc localhost 11211//or:
echo 'flush_all' | netcat localhost 11211


/*****Telnet Examples******/

//connect to memcached
telnet localhost 11211

//storing data in memcached server, syntax: set KEY META_DATA EXPIRY_TIME LENGTH_IN_BYTES
(LENGHT_IN_BYTES must match content!!!)
set Test 0 100 16
Angel is Testing

//retrieve data
get Test

//getting stats
stats

//stats shortcut
[webmaster@dev ~]$ echo stats | nc localhost 11211

/*****PHP Examples******/
Coming.... hoping sooner rather than later ...but coming, for sure coming.

10/4/2013 ...there:
http://angelcool.net/sphpblog/comments. ... 004-174818

[ view entry ] ( 1814 views )   |  print article
Basic Tasks with: rsync & scp  
//scp copy a remote file to local machine
scp username[at]10-network.com:~/html/3.gif .

//copy a remote directory to local machine
scp -r username[at]10-network.com:~/html .


//rsync uses ssh by default since version 2.6 (2004)

//rsync over ssh using port 222
rsync -v --rsh='ssh -p222' user[at]example.10-network.net:~/image.jpg .
//...or:
rsync -v -e 'ssh -p222' user[at]example.10-network.net:~/image.jpg .


//OK
rsync -avhe 'ssh -p22' --progress payless[at]paylesstosell.net:~/html/* .

[ view entry ] ( 1710 views )   |  print article
Counting Table Columns 
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'database_name'
AND table_name = 'table_name'

[ view entry ] ( 1691 views )   |  print article
Adding DNS Servers in CentoOS 
!!!!CentOS 6.4 !!!!

//the following command overwrites /etc/resolv.conf (or when rebooting)
service network restart
//instead, add the following to /etc/sysconfig/network-scripts/ifcfg-eth0
DNS1=4.2.2.2
DNS2=4.2.2.1

!!!!!!!!!!!!!!!!!!!


//open this file
vim /etc/resolv.conf

//add entries
nameserver 4.2.2.2
nameserver 4.2.2.1

//test
nslookup barney.com

[ view entry ] ( 1836 views )   |  print article

<<First <Back | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | Next> Last>>


2024 By Angel Cool