|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
Manipulating MySQL database using PHP
MySQL Connection: Here is how to connect to your MYSQL database using PHP. Be careful in filling out the connection variables. The slightest mistake and the connection will be refused. The login details are given by the host: server, password, database name and user. Here we will cover the following three points - MySQL : Connection to the server database - MySQL : Select the database - MySQL : Connection and transaction in the database Mysql - connection to the server database: mysql_connect () This function takes 3 input arguments: the host name, user name, password. The function returns a link that must stored in a variable for use later. Here is the code for it Code: <? php $ conn = mysql_connect("localhost","root","password"); if (! $ conn ) die ("conn impossible"); ?> Here is the code for it Code: <? php mysql_close($ connection); ?> This function takes as argument name database and a variable returned by mysql_connect () in our example $ conn. If this variable is not supplied, then the user uses the latest according to established connection. Here is the code for it Code: <? php $ Mydatabase="Client"; mysql_select_db($ Mydatabase) Or die ("no connection"); ?> This function takes as argument a string containing an SQL statement and a variable returned by mysq_connect (). If the latter is absent, then the variable takes the value returned by the last connection. Here is the code for it Code: $ Mydatabase="dbname"; $ conn = mysql_connect("localhost","root","password"); / / Test conn if (! $ conn ) die ("conn impossible"); / / Connect the base mysql_select_db($ Mydatabase) Or die ("no conn"); $ query="SELECT * from customer"; / / Request mysql_query($ query,$ conn); / / Send the query ?> |
#2
| |||
| |||
Re: Manipulating MySQL database using PHP MySQL - Create, Alter and Drop Here we will see to create a table, modify and delete fields. All these commands can be run online. I've set an example with the CREATE command to show you one of many possible torque PHP-MySQL. We will cover the following topics here - MySQL : Create Table - MySQL : Different types of data - MySQL : Modifying a table - MySQL : Deleting a table MySQL - Create Table: CREATE TABLE To create a table, use the command CREATE Syntax of the query is as follows Code: CREATE table tablename ( att1 type [NOT NULL] [AUTO_INCREMENT] att2 type [not null] . . attrNameN type [not null] . primary key (att1 ,..., attrNameN) ; Code: <? php / / Example 1 / / Create a table CREATE TABLE Member ( no tinyint(4) unsigned NOT NULL auto_increment, em varchar(80) NOT NULL, pass varchar(32), PRIMARY KEY (no) ); ?> Code: $ ln = mysql_connect('localhost', 'root', 'pass'); if (!$ ln) { die ('Could not connect:' . mysql_error()); } / / Make the database foo the current basis $ db_sel = mysql_select_db('dbname', $ ln); if (!$ db_sel) { die ('Unable to select database:' . mysql_error()); } $ s = "CREATE TABLE IF NOT EXISTS VOTRETABLE ( no tinyint (4) unsigned NOT NULL auto_increment, em varchar (80) NOT NULL, pass varchar (32) PRIMARY KEY (no) )"; mysql_query($ s,$ ln); mysql_close(); ?> - smallint: 16-bit integers - int: 32-bit integers - decimal (n, m): M with n digits after the decimal point - float - char (n): String of fixed length equal to n - varchar (n): String of at most n characters (n <= 256) - longvarchar (n): Idem with (n <= 32768) [NOT NULL] implies that the field can not be zero. [AUTO_INCREMENT] the whole will be increased by 1 at each insertion. primary key (att1 ,..., attrNameN) att1 attrNameN ,..., denotes the name of the Body attribute key. foreign key (att1 ,..., attrNameN) references (tablename); att1, attrNameN ,..., denotes the name of the Body attribute foreign key. The keyword Reference to specify the table name that contains these attributes. If the table has multiple foreign keys, you must repeat the previous steps. Some additional ideas on foreign keys to maintain consistency in the database, you can add conditions when you declare a foreign key. Examples on delete cascade on delete set null ON UPDATE CASCADE These options allow you to maintain consistency because when you delete a referenced key (or if you modify this key) on the repercussions spread through these options. return table1: foreign key (name1) References Table2 on delete set null. If you delete a row in the table1, the lines that reference in table2 will last no. Declaration of table1: foreign key (name1) References Table2 ON UPDATE CASCADE. If you change the key of a row of table1 then the changes will be reflected in Table2. Here is an example of this Code: <? php create table Customer ( no tinyint(3) unsigned not null AUTO_INCREMENT, nm varchar(10) not null default '', frnm varchar(10) not null default '', add varchar(50) not null default '', primary key (no) ); ?> Here is the syntax Code: ALTER TABLE TableName RENAME AS NewName Here are some examples add (attribute type [null / NotNull]) : Adds a field named "attribute" type "type" or DROP attribute1, attribute2 ... attributN : You can remove one or more attributes or MODIFY (attribute type [null / not null]) : Changes the type of attribute Mysql - Deleting a table: DROP To delete a table using the statement DROP Here is the syntax Code: DROP table tablename [restrict / cascade]; restrict : (By default clause, it checks that the destruction of the table is not involved in the coherence of the table. cascade : Destroyed the affected table and all its dependencies. Reminder: Attribute Is the title of the column in a table. Definition of key Is the key attribute (or attribute group) which is the smallest possible, and which designates and defined uniquely, one element of the relationship. The keys are conventionally said. Definition of foreign key Matches a set of attributes of a relation which denotes the primary key of another relation. Foreign keys are by convention a '#' before their names. A SQL statement always ends with a semicolon. |
![]() |
|
Tags: columns, database, database connection, fields, mysql, php, records, rows, scripting language |
Thread Tools | Search this Thread |
|
![]() | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
mysqldump in MySQL database | Caden Fernandes | Software Development | 4 | 04-03-2010 11:02 PM |
Need MySQL database directory | Osman84 | Software Development | 4 | 27-02-2010 05:18 AM |
Rename mysql database | Antarjot | Software Development | 3 | 11-11-2009 11:39 AM |
Mysql Error : Can't connect to local mysql server through socket ' var lib mysql mysql.sock' 2 | roshan45 | Software Development | 3 | 07-11-2009 09:36 PM |
How to Connect MySQL database from PHP | Booth | Software Development | 3 | 21-01-2009 09:12 PM |