Re: 实用的mysql+ajax+php 三级级联下拉框
[code]<?php
//指定编码
header('Content-Type:text/html;charset=GB2312');
//连接、选择数据库,设置字符集
$conn = mysql_connect('localhost', 'root', '') or die('Could not connect: ' . mysql_error());
mysql_select_db('fitment', $conn) or die ('Can\'t use database : ' . mysql_error());
mysql_query("set names 'gb2312'");
//如果传递过来q
if($_GET["q"]){
$strs = "SELECT * FROM fitment_sort WHERE supid=' ".$_GET["q"]."' ";
//echo $str;
$results = mysql_query($strs) or die("Invalid query: " . mysql_error());
if($results)
{
while ($arrs = mysql_fetch_array($results)){
$strings .= $arrs["id"]."|".$arrs["name"].",";
}
echo substr($strings,0,strlen($strings)-1);
}
}
//如果传递过来q2
if($_GET["q2"]){
$str = "SELECT * FROM fitment_sort WHERE supid= '".$_GET["q2"]."'";
//echo $str;
$result = mysql_query($str) or die("Invalid query: " . mysql_error());
if($result)
{
while ($arr = mysql_fetch_array($result)){
$string .= $arr["id"]."|".$arr["name"].",";
}
//去掉字串最后一个字符","
echo substr($string,0,strlen($string)-1);
}
}
mysql_close($conn);
?>[/code]