** 呼叫MySql的 function 範例 PHP

討論區:

PHP 呼叫MySql的 function 範例

$dbNAME = "資料庫名稱";
$dbUSER = "資料庫的user";
$dbPASS = "資料庫的密碼";
$dbHOST = "資料庫的主機"; //localhost , IP, URL
function myerr($link){ echo "<p><center>MySql Error(".mysql_errno($link)."):".mysql_error($link)."<br></center>\n"; }
function mylink(){
  global $dbNAME, $dbUSER, $dbPASS, $dbHOST; 
  $plink = mysql_connect($dbHOST, $dbUSER, $dbPASS);
mysql_query("set names 'utf8'", $plink); //UTF-8編碼
  $result = mysql_select_db($dbNAME);
  if($result) {
    return $plink;
  } else { 
    myerr($plink);
    return 0; 
  }
}
function debug($msg){ echo "<P>DEBUG:".$msg."<BR>".$endl; }
 
//先宣告一次就可以
$plink = mylink();
 
//用法範例一:
$str = "SELECT aa,bb,cc,dd FROM dbTABLE WHERE 1 ";
$q = mysql_query($str, $plink);
list($aa,$bb,$cc,$dd ) = @mysql_fetch_row($qq);
 
//用法範例二:
$str = "SELECT * FROM dbTABLE WHERE 1 ";
$q = mysql_query($str, $plink);
while($x= @mysql_fetch_array($qq)){
echo $x['aa'].$x['bb'];
}