PHP链接Mysql数据库的小知识
1:在PHP写隔行变色有三种写法(但是最好的是用js+css)
(1)CSS:
tr:nth-child(even){
background: #cad9ea;
}
even:代表偶数;
ood:代表奇数;
(2)PHP:
<?php
if($n %2 ==
0){
?>
<tr style="background: #cad9ea">
<?php
}else{
?>
<tr>
<?php
}
?>
(3)JS+CSS:
<script type="text/javascript">
function changeColor(){
var trs = document.getElementsByTagName("tr");
for(var i=0;i<trs.length;i++){
if( i%2 == 0){
trs[i].style.background="#d9e9fa";
}else{
trs[i].style.background="#d3f4d1";
}
}
}
window.onload=changeColor;
</script>
2: mysql_num_rows:取得结果集中行的数目
例:
$sql1 = "select * from movies";
$result1 = mysql_query($sql1) or die(mysql_error());
$count = mysql_num_rows($result1);
3:(int)与intal()强制性转换成整形
例:$totalPage = $count % $pageSize == 0 ? (int)($count / $pageSize ): (int)($count / $pageSize + 1);
$totalPage = $count % $pageSize == 0 ? intval($count / $pageSize) : intval($count / $pageSize) + 1;
4:经常重复的代码我们可以封装,方便使用时直接调用
例:
function swiftType($type){
$t = "";
switch($type){
case 1:
$t = "爱情";
break;
case 2:
$t = "科幻";
break;
case 3:
$t = "恐怖";
break;
}
return $t;
}
//调用时:
<?php
echo swiftType($row[‘type‘]);
?>
5:注意:传值时千万不要用中文,最好用数字和字母
6:隐藏域表单:(<input type="hidden">)
例: <input type="hidden" value="<?php echo $row[‘id‘] ?>" name="id" readonly="readonly"/>
7:limit:从第几天开始(n-1),显示几条
例: $sql = "select * from movies limit 0,3";
解释:
limit (0 , 3)这是第一条显示到三条
第几条开始 显示几条
0 3
limit (4 , 5)这是第五条显示到第九条
第几条开始 显示几条
5-1 5