Connect PHP to the mysql database (solve the garbled problem)

Source: Internet
Author: User

Code

<? Php
// 1. mysql_pconnect ()... connect to the database
$ Conn = mysql_connect ("localhost", "root", "120455637") or die ("cocould not connect:". mysql_error ());
// Solve the garbled problem: @ mysql_query ("SET character_set_connection = utf8, encoding = utf8, character_set_client = binary", $ conn); // 2. mysql_select_db ()... select a data table
Mysql_select_db ('wjbtest', $ conn );
$ SQL = "select * from users ";
// 3. mysql_query ()... execute the SQL language
$ Query = mysql_query ($ SQL );
While ($ myrow = mysql_fetch_array ($ query ))
{
Echo $ myrow ['id'], '+', $ myrow ['dateline '],' <br/> ';
}
// 4. mysql_close ()... close the connection
Mysql_close ($ conn );
?>

 

$ Host = "localhost ";
$ Database = "smsdb ";
$ DbaUser = "root ";
$ DbaPassword = "";

//// Database connection

$ Db = mysql_pconnect ($ Host, $ DbaUser, $ DbaPassword );
If (! $ Db)
{
Echo "Error: cocould not connect to database. Please try again .";
Exit;
}
// Select a database
Mysql_select_db ($ Database, $ db );

/// Database disconnection
Mysql_close ($ db );

/// Database operations
// Query
$ Query = "SELECT * FROM user WHERE ";
// Insert
$ Query = "insert into employees VALUES (2, 'john', 'ots', '45 There St, Townville ', 'telephonist ');"
// Delete
$ Query = "delete from employees WHERE age is null ";
// Modify
$ Query = "UPDATE employees set from where ";
Townville ', 'telephonist ');"
// Result
$ Result = mysql_query ($ query );
If (! $ Result)
{
Echo "Error! ";
Exit;
}
$ Num_reslutlt = mysql_num_rows ($ result );

// Obtain the current system time
$ AddTime = date ("YmdHms ");

/// // 1. enroll. php User Registration
$ Host = "localhost ";
$ Database = "smsdb ";
$ DbaUser = "root ";
$ DbaPassword = "";

// Verify the rationality of the Data filled in by the user
Trim ($ UserMb );
Trim ($ UserPwd );
Trim ($ PwdConfirm );
If (! $ UserMb |! $ UserPwd |! $ PwdConfirm)
{
Echo "Your have not enter your detail message ";
Exit;

}
If ($ UserPwd! = $ PwdConfirm)
{
Echo "Confirm Password not correct to passworld ";
Exit;

}
//// Database connection
$ Db = mysql_pconnect ($ Host, $ DbaUser, $ DbaPassword );
If (! $ Db)
{
Echo "Error: cocould not connect to database. Please try again .";
Exit;
}
// Select a database
Mysql_select_db ($ Database, $ db );
// Obtain the current system time
$ AddTime = date ("YmdHms ");
// Insert
$ Query = "insert into user VALUES ('','". $ UserMb. "','". $ UserPwd. "','". $ AddTime. "');"
// Result
$ Result = mysql_query ($ query );
If ($ result)
{
/// The following operations are successful
Echo "Enroll Success! ";
}
Else
{
Echo "Error! ";
Exit;
}

/// Database disconnection
Mysql_close ($ db );

//////////////////////////////////////// //// User verification code example

// Determine whether the user name is set
If (! Isset ($ PHP_AUTH_USER ))
{
Header ("WWW-Authenticate: Basic realm =" authentication function "");
Header ("HTTP/1.0 401 Unauthorized ");
Echo "authentication failed. You do not have permission to share network resources! ";
Exit ();
}
/* Connect to the database */
$ Db = mysql_connect ("localhost", "root ","");
// Select a database
Mysql_select_db ("XinXiKu", $ db );
// Query whether a user exists
$ Result = mysql_query ("SELECT * FROM user WHERE name = '$ PHP_AUTH_USER' AND password = '$ PHP_AUTH_PW'", $ db );
If ($ myrow = mysql_fetch_row ($ result ))
{
// Perform the following operations after authentication is successful:

}
Else
{
// The authentication fails, and the user is prompted to re-enter
Header ("WWW-Authenticate: Basic realm =" authentication function "");
Header ("HTTP/1.0 401 Unauthorized ");
Echo "authentication failed. You do not have permission to share network resources! ";
Exit ();
}

/// // 2 login. php User login

$ Host = "localhost ";
$ Database = "smsdb ";
$ DbaUser = "root ";
$ DbaPassword = "";

// Verify the rationality of the Data filled in by the user
Trim ($ UserMb );
Trim ($ UserPwd );

If (! $ UserMb |! $ UserPwd)
{
Echo "Your have not enter your detail message ";
Exit;

}
//// Database connection
$ Db = mysql_pconnect ($ Host, $ DbaUser, $ DbaPassword );
If (! $ Db)
{
Echo "Error: cocould not connect to database. Please try again .";
Exit;
}
// Select a database
Mysql_select_db ($ Database, $ db );

// Query whether a user exists
$ Query = "SELECT * FROM user WHERE name = '$ UserMb' AND password = '$ userpwd '";
$ Result = mysql_query ($ query );
If ($ myrow = mysql_fetch_row ($ result ))
{
// Perform the following operations after authentication is successful:

}
Else
{
// The authentication fails, and the user is prompted to re-enter
Header ("WWW-Authenticate: Basic realm =" authentication function "");
Header ("HTTP/1.0 401 Unauthorized ");
Echo "authentication failed. You do not have permission to share network resources! ";
Exit ();
}

/// Database disconnection
Mysql_close ($ db );

//// // 3. Log On As the administrator.

$ Host = "localhost ";
$ Database = "smsdb ";
$ DbaUser = "root ";
$ DbaPassword = "";

// Verify the rationality of the Data filled in by the user
Trim ($ SysUser );
Trim ($ SysUserPwd );

If (! $ SysUser |! $ SysUserPwd)
{
Echo "Your have not enter your detail message ";
Exit;

}
//// Database connection
$ Db = mysql_pconnect ($ Host, $ DbaUser, $ DbaPassword );
If (! $ Db)
{
Echo "Error: cocould not connect to database. Please try again .";
Exit;
}
// Select a database
Mysql_select_db ($ Database, $ db );

// Query whether a user exists
$ Query = "SELECT * FROM sysuser WHERE SysUser = '$ sysuser' AND SysUserPwd =' $ SysUserPwd '";
$ Result = mysql_query ($ query );
If ($ myrow = mysql_fetch_row ($ result ))
{
// Perform the following operations after authentication is successful:

}
Else
{
// The authentication fails, and the user is prompted to re-enter
Header ("WWW-Authenticate: Basic realm =" authentication function "");
Header ("HTTP/1.0 401 Unauthorized ");
Echo "authentication failed. You do not have permission to share network resources! ";
Exit ();
}

/// Database disconnection
Mysql_close ($ db );

/// // 4. Text Message Management
********************/
$ Host = "localhost ";
$ Database = "smsdb ";
$ DbaUser = "root ";
$ DbaPassword = "";

//// Database connection

$ Db = mysql_pconnect ($ Host, $ DbaUser, $ DbaPassword );
If (! $ Db)
{
Echo "Error: cocould not connect to database. Please try again .";
Exit;
}
// Select a database
Mysql_select_db ($ Database, $ db );
// Query text messages
$ Query = "SELECT * FROM sms WHERE SmsType = '$ SmsType ';
$ Result = mysql_query ($ query );
If ($ myrow = mysql_fetch_row ($ result ))
{
// Perform the following operations after extraction is successful:

}
Else
{
Echo "no record! ";
Exit ();
}

/// Database disconnection
Mysql_close ($ db );

************************ *****/

$ Host = "localhost ";
$ Database = "smsdb ";
$ DbaUser = "root ";
$ DbaPassword = "";

//// Database connection

$ Db = mysql_pconnect ($ Host, $ DbaUser, $ DbaPassword );
If (! $ Db)
{
Echo "Error: cocould not connect to database. Please try again .";
Exit;
}
// Select a database
Mysql_select_db ($ Database, $ db );
// Obtain the current system time
$ AddTime = date ("YmdHms ");
// Add
$ Query = "insert into sms VALUES ('', $ SmsType, $ SmsContent, $ AddTime )";
$ Result = mysql_query ($ query );
If ($ myrow = mysql_fetch_row ($ result ))
{
// Perform the following operations after successful addition:

}
Else
{
Echo "Add fail! ";
Exit ();
}

/// Database disconnection
Mysql_close ($ db );

************************* ********/
$ Host = "localhost ";
$ Database = "smsdb ";
$ DbaUser = "root ";
$ DbaPassword = "";

//// Database connection

$ Db = mysql_pconnect ($ Host, $ DbaUser, $ DbaPassword );
If (! $ Db)
{
Echo "Error: cocould not connect to database. Please try again .";
Exit;
}
// Select a database
Mysql_select_db ($ Database, $ db );
// Delete
$ Query = "delete from sms WHERE SmsId = $ SmsId ";
$ Result = mysql_query ($ query );
If ($ myrow = mysql_fetch_row ($ result ))
{
// Perform the following operations after the deletion is successful:

}
Else
{
Echo "Delete fail! ";
Exit ();
}

/// Database disconnection
Mysql_close ($ db );

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.