PHP operation MySQL and encapsulation common functions use outer joins to connect 3 tables case

Source: Internet
Author: User
Tags joins

<?php
Header ("Content-type;text/html;charset=utf-8");

Database connection
Define (' db_host ', ' localhost ');
Define (' Db_user ', ' root ');
Define (' Db_password ', ' admin ');
Define (' db_name ', ' php2016 ');
Connect to MySQL server
function _connect () {
Global represents the meaning of the globals, with the intention of accessing the variable outside of the function
Global $_conn;
if (!$_conn=mysql_connect (Db_host,db_user,db_password)) {
Echo ' failed to connect to MySQL server ';
Echo ' Error number: ', Mysql_errno (), ' <br/> ';
Echo ' Error message: ', mysql_error (), ' <br/> ';
}
}

Connect to the specified database
function _select_db () {
if (!mysql_select_db (db_name)) {
Echo ' cannot find the specified database ';
Echo ' Error number: ', Mysql_errno (), ' <br/> ';
Echo ' Error message: ', mysql_error (), ' <br/> ';
}
}

Set character sets
function _set_names () {
if (!mysql_query (' SET NAMES UTF8 ')) {
Echo ' character set failed ';
Echo ' Error number: ', Mysql_errno (), ' <br/> ';
Echo ' Error message: ', mysql_error (), ' <br/> ';
}
}

Executes the SQL statement, returning a result set $_sql means an SQL statement
function _query ($_sql) {
if (!$_result=mysql_query ($_sql)) {
Exit (' SQL execution failed ');
}
return $_result;
}

_query ($_sql) is the result set
/*
* _fetch_array only one data set for the specified data set
*/
function _fetch_array ($_sql) {
Return Mysql_fetch_array (_query ($_sql), MYSQL_ASSOC);
}
/*
* _fetch_array_list can return all data for a specified dataset
*/
function _fetch_array_list ($_result) {
return mysql_fetch_array ($_RESULT,MYSQL_ASSOC);
}

/*
* The _html () function denotes HTML filtering of strings, as arrays, displayed as arrays, as strings, as strings
*/

function _html ($_string) {
if (Is_array ($_string)) {
foreach ($_string as $_key =>$_value) {
$_string[$_key]=htmlspecialchars ($_value);
}
}else {
$_string=htmlspecialchars ($_string);
}
return $_string;
}
Initializing the database
_connect (); Connect to MySQL server
_select_db (); Connect to the specified database
_set_names (); Set character sets

$sql = "Select Goods.id,goods.name as goods_name,colors.name as colors_name,size.name as size_name from goods left JOIN CO Lors on goods.id=colors.id left join size on goods.id=size.id; ";
$res =_query ($sql);
$_html=array ();
while (!! $_rows=_fetch_array_list ($res)) {
$_html[' id ']=$_rows[' id '];
$_html[' goods_name ']=$_rows[' goods_name '];
$_html[' colors_name ']=$_rows[' colors_name '];
$_html[' size_name ']=$_rows[' size_name '];
$_html=_html ($_html);

echo $_html[' id '];echo $_html[' goods_name ']; echo $_html[' colors_name '];echo $_html[' size_name ']. ' <br/> ';
}
?>

PHP operation MySQL and encapsulation common functions use outer joins to connect 3 tables of cases

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.