Please note: This page is imported from my wiki, which hasn't been updated in over 10 years. Some of the formatting was lost during the import. I'll try to get around to fixing it someday.
The following are examples of how to get started on a database of registrations, called registrations.
mysqladmin -u root -p create registrations
mysql -u root -p
grant all privileges on registrations.* to ‘registrations’@‘localhost’ identified by ‘somepassword’; flush privileges;
show databases;
use registrations;
show tables;
CREATE TABLE registrations ( id int(11) NOT NULL auto_increment, name varchar(255), email varchar(255), address text, PRIMARY KEY (id) ) TYPE=MyISAM;
DESC registrations;
DROP TABLE registrations;
INSERT INTO registrations SET name=‘Andrew McDonough’, email=‘andrew@andrew.com’, address=‘1 High Street’;
SELECT * FROM registrations; SELECT id,name FROM registrations; SELECT id,name FROM registrations where id=1; SELECT * FROM registrations where name like ‘%ndrew%‘;
To connect to MySQL from PHP, you will need to connect to the database and get a database handle. I tend to assign this to the variable $dbh $dbh = @mysqlpconnect(DBHOST, DBUSER, DBPASS);
@mysqlselectdb(DB_NAME,$dbh);
$query = “INSERT INTO registrations SET name=‘Andrew’, email=‘andrew@andrew.com’, address=‘1 South Street’”; $result = @mysql_query($query,$dbh);
$query = “SELECT * FROM registrations WHERE id=‘1’”; $result = @mysqlquery($query,$dbh); $record = @mysqlfetch_array($result);
$query = “SELECT * FROM registrations”; $result=@mysqlquery($query,$dbh); $result = array(); while($row=@mysqlfetcharray($result)){ arraypush($result,$row); }
Andrew McDonough is a consultant CTO and software developer, currently based between Berlin and London.
Follow Andrew on Twitter or Connect on LinkedIn