当前位置:Gxlcms > mysql > PHP,MYSQL常见问题与解答_MySQL

PHP,MYSQL常见问题与解答_MySQL

时间:2021-07-01 10:21:17 帮助过:11人阅读

===> register_globals[全局变量]的相关说明
很多入门的朋友被全局变量搞的很糊涂,可以参考以下的说明:

CODE
  1. 决定是否将 EGPCS(Environment,GET,POST,Cookie,Server)变量注册为全局变量。例如,如果 register_globals = on,那么 URL http://www.example.com/test.php?id=3 将产生 $id。或者从 $_SERVER['DOCUMENT_ROOT'] 得来 $DOCUMENT_ROOT。如果不想用户数据把全局域弄乱的话可以将此选项关闭。自 PHP 4.2.0 开始,本指令默认为 off。推荐使用 PHP 的预定义变量来替代,例如超全局变量:$_ENV,$_GET,$_POST,$_COOKIE 和 $_SERVER。



===> 一个写文本数据的函数,可以有效防止清零
自己用在计数器中,在linux下日ip8万没有发现清零的现象

CODE
  1. // Write Something to a Text
  2. function writetofile($filename,$data,$method="rb+"){
  3. touch($filename);
  4. $handle=fopen($filename,$method);
  5. flock($handle,LOCK_EX);
  6. $fettle=fwrite($handle,$data);
  7. if($method=="rb+"){ftruncate($handle,strlen($data));}
  8. fclose($handle);
  9. return $fettle;
  10. }
  11. //test
  12. writetofile("test.txt","Some data!");
  13. ?>

===> Client does not support authentication protocol
好多朋友升级了mysql为4.10以上会发生此错误,php4.x下phpmyadmin之类的程序连接不上数据库,是因为mysql4.10以上改变了用户密码验证协议,php的连接模块也需要更换新的,php5默认就是这种新的连接模块...

不想更换可以这样:

CODE
  1. # SET PASSWORD FOR 'some_user'@'some_host' = OLD_PASSWORD('newpwd');
  2. # FLUSH PRIVILEGES;


官方说明文档:
CODE
  1. Client does not support authentication protocol
  2. MySQL 4.1 and up uses an authentication protocol based on a password hashing algorithm that is incompatible with that used by older clients. If you upgrade the server to 4.1, attempts to connect to it with an older client may fail with the following message:
  3. shell> mysql
  4. Client does not support authentication protocol requested
  5. by server; consider upgrading MySQL client
  6. To solve this problem, you should use one of the following approaches:
  7. Upgrade all client programs to use a 4.1.1 or newer client library.
  8. When connecting to the server with a pre-4.1 client program, use an account that still has a pre-4.1-style password.
  9. Reset the password to pre-4.1 style for each user that needs to use a pre-4.1 client program. This can be done using the SET PASSWORD statement and the OLD_PASSWORD() function:
  10. mysql> SET PASSWORD FOR
  11. -> 'some_user'@'some_host' = OLD_PASSWORD('newpwd');
  12. Alternatively, use UPDATE and FLUSH PRIVILEGES:
  13. mysql> UPDATE mysql.user SET Password = OLD_PASSWORD('newpwd')
  14. -> WHERE Host = 'some_host' AND User = 'some_user';
  15. mysql> FLUSH PRIVILEGES;
  16. Substitute the password you want to use for ``newpwd'' in the preceding examples. MySQL cannot tell you what the original password was, so you'll need to pick a new one.
  17. Tell the server to use the older password hashing algorithm:
  18. Start mysqld with the --old-passwords option.
  19. Assign an old-format password to each account that has had its password updated to the longer 4.1 format. You can identify these accounts with the following query:
  20. mysql> SELECT Host, User, Password FROM mysql.user
  21. -> WHERE LENGTH(Password) > 16;
  22. For each account record displayed by the query, use the Host and User values and assign a password using the OLD_PASSWORD() function and either SET PASSWORD or UPDATE, as described earlier.

===> 如果在数字前面强制加0
格式化数字的位数,未满位的加前导0

CODE
  1. $num = 12;
  2. $var = sprintf("%09d", $num);
  3. echo $var;
  4. ?>



===> 取得当前脚本的路径
请不要用 $_SERVER['PHP_SELF']取路径,$_SERVER['PHP_SELF']取出来的在某些情况下是错误的.也不要用$_SERVER['SERVER_NAME'],$_SERVER['SERVER_NAME']取得的主机名不包含端口号,而且有时候取得的是服务器名,非你的虚拟主机名.
脚本路径:

CODE
  1. $url = 'http://'.dirname($_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']).'/';
  2. ?>


地址栏信息:

CODE
  1. $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  2. ?>



===> url传递参数需要这样编码
与 HTML 实体相匹配的变量。像 &、© 和 £ 都将被浏览器解析,并使用实际实体替代所期待的变量名。解决办法是使用 & 代替 & 作为分隔符。使用 htmlentities(urlencode($data)) 对你的 URL 进行编码。

CODE
  1. echo "";
  2. ?>


接受页面直接使用,不需要解码.

===> 数据存入MySQL需要注意
入库:

CODE
  1. $str = addslashes($str);
  2. ?>


出库:

CODE
  1. $str = stripslashes($str);
  2. ?>



===> 返回MySQL数据库上一步 INSERT 操作产生的 ID
mysql_insert_id() 返回给定的 link_identifier 中上一步 INSERT 查询中产生的 AUTO_INCREMENT 的 ID 号。如果没有指定 link_identifier,则使用上一个打开的连接。

CODE
  1. $last_id = mysql_insert_id();
  2. ?>



===> 如何解决表单出错返回从填的时候以前填写的东西全部消失
因为使用了session,可以用session_cache_limiter强制数据流从新生效.

CODE
  1. // session start
  2. session_cache_limiter("private, must-revalidate");
  3. session_start();
  4. ...
  5. ?>



===> 如何一劳永逸的解决Cannot send session cache limiter - headers already sent 这种错误
当用header()转向或者session_start()之前有输出,会有Cannot send session cache limiter - headers already sent的错误,可以用ob_start(),这样会在页面全部执行完毕才输出.

CODE
  1. session_start();
  2. ob_start();
  3. ...
  4. ?>



===> 如何表单填写错误时返回后以前填写的内容消失?
当使用session后会出现这种情况,我们可以使用session_cache_limiter();强制生效.

CODE
  1. session_cache_limiter("private, must-revalidate");
  2. session_start();
  3. ...
  4. ?>



===> 判断邮件地址是否合法!
判断邮件地址是否合法!

CODE
  1. if(!ereg("^[-a-zA-Z0-9_\.]+\@([0-9A-Za-z][0-9A-Za-z-]+\.)+[A-Za-z]{2,3}$",$mail_from)){
  2. echo "输出.
    CODE
    1. // pic_database_output.php By Bleakwind
    2. header( "content-type: image/gif");
    3. $Host = "localhost";
    4. $User = "bleakwind";
    5. $Password = "bleakwind";
    6. $Database = "test";
    7. $id = 1;
    8. mysql_connect($Host,$User,$Password) or die("Could not connect:" . mysql_error());
    9. mysql_select_db($Database) or die("Could not select database!");
    10. $sql = "SELECT * FROM `image_table` WHERE `id`=" . $id;
    11. $result = mysql_query($sql) or die("Could not perform query!");
    12. $row = mysql_fetch_array($result);
    13. echo $row['image'];
    14. ?>

    人气教程排行