最大的网站源码资源下载站,
[回顾]:上集介绍了"调试程序","如何使用session","规范sql语句"等15个问题(php高手带路--问题汇总解答[1])。本集继续作出16条常见问题的解答。
16:我想修改mysql的用户,密码
首先要声明一点,大部分情况下,修改mysql是需要有mysql里的root权限的,
所以一般用户无法更改密码,除非请求管理员.
方法一
使用phpmyadmin,这是最简单的了,修改mysql库的user表,
不过别忘了使用password函数。
方法二
使用mysqladmin,这是前面声明的一个特例。
mysqladmin -u root -p password mypasswd
输入这个命令后,需要输入root的原密码,然后root的密码将改为mypasswd。
把命令里的root改为你的用户名,你就可以改你自己的密码了。
当然如果你的mysqladmin连接不上mysql server,或者你没有办法执行mysqladmin,
那么这种方法就是无效的。
而且mysqladmin无法把密码清空。
下面的方法都在mysql提示符下使用,且必须有mysql的root权限:
方法三
mysql> insert into mysql.user (host,user,password)
values('%','jeffrey',password('biscuit'));
mysql> flush privileges
确切地说这是在增加一个用户,用户名为jeffrey,密码为biscuit。
在《mysql中文参考手册》里有这个例子,所以我也就写出来了。
注意要使用password函数,然后还要使用flush privileges。
方法四
和方法三一样,只是使用了replace语句
mysql> replace into mysql.user (host,user,password)
values('%','jeffrey',password('biscuit'));
mysql> flush privileges
方法五
使用set password语句,
mysql> set password for [email protected]"%" = password('biscuit');
你也必须使用password()函数,
但是不需要使用flush privileges。
方法六
使用grant ... identified by语句
mysql> grant usage on *.* to [email protected]"%" identified by 'biscuit';
这里password()函数是不必要的,也不需要使用flush privileges。
注意: password() [不是]以在unix口令加密的同样方法施行口令加密。
17:我想知道他是通过哪个网站连接到本页
php代码:
<?php
//必须通过超级连接进入才有输出
echo $_server['http_referer'];
?>
18:数据放入数据库和取出来显示在页面需要注意什么
入库时
$str=addslashes($str);
$sql="insert into `tab` (`content`) values('$str')";
出库时
$str=stripslashes($str);
显示时
$str=htmlspecialchars(nl2br($str)) ;
<?php
//$content来自数据库
$content=nl2br(htmlspecialchars($content));
$content=str_replace(" "," ",$content);
$content=str_replace("/n","<br>/n",$content);
?>
19:如何读取当前地址栏信息
php代码:
<?php
$s="http://{$_server['http_host']}:{$_server["server_port"]}{$_server['script_name']}";
$se='';
foreach ($_get as $key => $value) {
$se.=$key."=".$value."&";
}
$se=preg_replace("/(.*)&$/","$1",$se);
$se?$se="?".$se:"";
echo $s."?$se";
?>
20:我点击后退按钮,为什么之前填写的东西不见
这是因为你使用了session.
解决办法:
php代码:
<?php session_cache_limiter('private, must-revalidate');session_start();
.....................?>
21:怎么在图片里显示ip地址
php代码:
<? header("content-type: image/png");
$img = imagecreate(180,50);
$ip = $_server['remote_addr'];
imagecolortransparent($img,$bgcolor);
$bgcolor = imagecolorallocate($img, 0x2c,0x6d,0xaf); // 背景颜色
$shadow = imagecolorallocate($img, 250,0,0); // 阴影颜色
$textcolor = imagecolorallocate($img, oxff,oxff,oxff); // 字体颜色
imagettftext($img,10,0,78,30,$shadow,"d:/windows/fonts/tahoma.ttf",$ip);
//显示背景
imagettftext($img,10,0,25,28,$textcolor,"d:/windows/fonts/tahoma.ttf","your ip is".$ip);
// 显示ip
imagepng($img);
imagecreatefrompng($img);
imagedestroy($img);
?>
22:如何取得用户的真实ip
php代码:
<? function iptype1 () {
if (getenv("http_client_ip"))
{
return getenv("http_client_ip");
}
else
{
return "none";
}
}
function iptype2 () {
if (getenv("http_x_forwarded_for"))
{
return
getenv("http_x_forwarded_for");
}
else {
return "none";
}
}
function iptype3 () {
if (getenv("remote_addr"))
{
return getenv("remote_addr");
}
else {
return "none";
}
}
function ip() {
$ip1 = iptype1();
$ip2 = iptype2();
$ip3 = iptype3();
if (isset($ip1) && $ip1 != "none" && $ip1 != "unknown")
{
return $ip1;
}
elseif (isset($ip2) && $ip2 != "none" && $ip2 != "unknown")
{
return $ip2;
}
elseif (isset($ip3) && $ip3 != "none" && $ip3 != "unknown")
{
return $ip3;
}
else
{ return "none"; }
}
echo ip();
?>
23:如何从数据库读取三天内的所有记录
首先表格里要有一个datetime字段记录时间,
格式为'2003-7-15 16:50:00'
select * from `xltxlm` where to_days(now()) - to_days(`date`) <= 3;
24:如何远程链接mysql数据库
在增加用户的mysql表里有一个host字段,修改为"%",或者指定允许连接的ip地址,这样,你就可以远程调用了。
$link=mysql_connect("192.168.1.80:3306","root","");
25:正则到底怎么用
点击这里
正则表达式中的特殊字符
26:用apache后,主页出现乱码
方法一:
adddefaultcharset iso-8859-1 改为 adddefaultcharset off
方法二:
adddefaultcharset gb2312
27:为什么单引号,双引号在接受页面变成(/'/")
解决方法:
方法一:在php.ini中设置:magic_quotes_gpc = off
方法二: $str=stripcslashes($str)
28:怎么让程序一直运行下去,而不是超过30秒就停止
set_time_limit(60)//最长运行时间一分钟
set_time_limit(0)//运行到程序自己结束,或手动停止
29:计算当前在线人数
例子一:用文本实现
php代码:
<?php
//首先你要有读写文件的权限
//本程序可以直接运行,第一次报错,以后就可以
$online_log = "count.dat"; //保存人数的文件,
$timeout = 30;//30秒内没动作者,认为掉线
$entries = file($online_log);
$temp = array();
for ($i=0;$i<count($entries);$i++) {
$entry = explode(",",trim($entries[$i]));
if (($entry[0] != getenv('remote_addr')) && ($entry[1] > time()))
{
array_push($temp,$entry[0].",".$entry[1]."/n"); //取出其他浏览者的信息,并去掉超时者,保存进$temp
}
}
array_push($temp,getenv('remote_addr').",".(time() + ($timeout))."/n");
//更新浏览者的时间
$users_online = count($temp); //计算在线人数
$entries = implode("",$temp);
//写入文件
$fp = fopen($online_log,"w");
flock($fp,lock_ex); //flock() 不能在nfs以及其他的一些网络文件系统中正常工作
fputs($fp,$entries);
flock($fp,lock_un);
fclose($fp);
echo "当前有".$users_online."人在线";
?>
30:什么是模板,怎么用
我用的是phplib模板
下面是其中几个函数的使用
$t->set_file("随便定义","模板文件.tpl");
$t->set_block("在set_file中定义的","<!-- 来自模板 -->","随便定义");
$t->parse("在set_block中定义的","<!-- 来自模板 -->",true);
$t->parse("随便输出结果","在set_file中定义的");
设置循环格式为:
<!--(多于一个空格) begin $handle(多于一个空格)-->
如何将模板生成静态网页
php代码:
<?php
//这里使用phplib模板
............
............
$tpl->parse("output","html");
$output = $tpl->get("output");// $output 为整个网页内容
function wfile($file,$content,$mode='w') {
$oldmask = umask(0);
$fp = fopen($file, $mode);
if (!$fp) return false;
fwrite($fp,$content);
fclose($fp);
umask($oldmask);
return true;
}
// 写到文件里
wfile($file,$output);
header("location:$file");//重定向到生成的网页
}
?>
phplib下载地址 smarty下载地址
31:怎么用php解释字符
比如:输入2+2*(1+2),自动输出8 可以用eval函数
php代码:
<form method=post action="">
<input type="text" name="str"><input type="submit">
</form>
<?php
$str=$_post['str'];
eval("/$o=$str;");
echo "$o";
?>
到此,php的问题解答就为大家介绍完毕,希望能对各位有所帮助。
新闻热点
疑难解答