php如何屏蔽错误警告提示信息
方法一:
在有可能出错的函数前加@,然后or die(""),如:
@mysql_connect(…) or die("Database Connect Error")
方法二:
编辑php.ini ,查找“display_errors”,将“=”后面的值改为“off”。
方法三:
在php脚本前加error_reporting(0),屏蔽所有错误提示。
其中,error_reporting 配置错误信息回报的等级。
方法一:
在有可能出错的函数前加@,然后or die(""),如:
@mysql_connect(…) or die("Database Connect Error")
方法二:
编辑php.ini ,查找“display_errors”,将“=”后面的值改为“off”。
方法三:
在php脚本前加error_reporting(0),屏蔽所有错误提示。
其中,error_reporting 配置错误信息回报的等级。
方法1:
1 2 3 4 5 6 7 8 |
$url = "https://www.baidu.com/"; $ch = curl_init(); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch,CURLOPT_CAINFO, dirname(__FILE__).'/cacert.pem'); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); |
方法2:
通过设置php.ini文件,可以一劳永逸
openssl.cafile= “C:/WebServ/php7.4.4/cacert.pem”
cacert.pem下载地址:http://curl.haxx.se/ca/cacert.pem
get_headers()是PHP中一个系统级函数,它可以返回一个包含有服务器响应一个HTTP请求所发送的标头的数组。如果失败则返回 FALSE 并发出一条 E_WARNING 级别的错误信息,利用它我们可以判断远程文件是否存在或者判断页面链接是否可以打开。
1 2 3 4 5 6 7 8 9 10 11 12 |
$url = 'http://learning.sohu.com/20061129/n246690362.shtml'; if ($arr = @get_headers($url, 1)) { if (preg_match('/200/',$arr[0])) { echo '<pre>'; print_r($arr); echo '</pre>'; } else { echo "无效网页1"; } } else { echo "无效网页2"; } |
遇到的问题:
1. 由于有些站点对无效链接进行了技术处理,有些页面是无效的但get_headers获得的反馈是有效的,如:
http://news.china.com.cn/mts/2021-05/28/content_1171556.htm
2. 有些是有效的但get_headers反馈无效,如:
https://mbd.baidu.com/newspage/data/landingshare?preview=1&pageType=1&isBdboxFrom=1&context=%7B%22nid%22%3A%22news_9913548542233559421%22%2C%22sourceFrom%22%3A%22bjh%22%7D
http://news.tom.com/202105/4717053864.html(浏览器访问后地址自动变为https://news.tom.com/202105/4717053864.html)
在Windows11+Apache2.4.55+PHP5.1.4+MySQL5.0.15环境下,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
<?php //error_reporting(0); function db_dump($host, $user, $pwd, $db) { $mysqlconlink = mysql_connect($host, $user, $pwd); if (!$mysqlconlink) exit(sprintf('No MySQL connection: %s', mysql_error())); $mysqldblink = mysql_select_db($db, $mysqlconlink); if (!$mysqldblink) exit(sprintf('No MySQL connection to database: %s', mysql_error())); $tabelstobackup = array(); $result = mysql_query("SHOW TABLES FROM `$db`"); if (!$result) exit(sprintf('Database error %1$s for query %2$s', mysql_error(), "SHOW TABLE STATUS FROM `$db`;")); while ($data = mysql_fetch_row($result)) { $tabelstobackup[] = $data[0]; } if (count($tabelstobackup) > 0) { $result = mysql_query("SHOW TABLE STATUS FROM `$db`"); if (!$result) exit(sprintf('Database error %1$s for query %2$s', mysql_error(), "SHOW TABLE STATUS FROM `$db`;")); while ($data = mysql_fetch_assoc($result)) { $status[$data['Name']] = $data; } if ($file = fopen("$db.sql", 'wb')) { fwrite($file, "-- ---------------------------------------------------------\n"); fwrite($file, "-- Database Name: $db\n"); fwrite($file, "-- ---------------------------------------------------------\n\n"); fwrite($file, "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n"); fwrite($file, "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n"); fwrite($file, "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n"); fwrite($file, "/*!40101 SET NAMES '".mysql_client_encoding()."' */;\n"); fwrite($file, "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;\n"); fwrite($file, "/*!40103 SET TIME_ZONE='".mysql_result(mysql_query("SELECT @@time_zone"),0)."' */;\n"); fwrite($file, "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n"); fwrite($file, "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n"); fwrite($file, "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n"); fwrite($file, "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n\n"); foreach($tabelstobackup as $table) { echo sprintf('Dump database table "%s"',$table) . "<br />"; _db_dump_table($table,$status[$table], $file); } fwrite($file, "\n"); fwrite($file, "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;\n"); fwrite($file, "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;\n"); fwrite($file, "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;\n"); fwrite($file, "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n"); fwrite($file, "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n"); fwrite($file, "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n"); fwrite($file, "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n"); fwrite($file, "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;\n"); fclose($file); echo 'Database dump done!<br />'; } else { echo 'Can not create database dump!<br />'; } } } function _db_dump_table($table, $status, $file) { fwrite($file, "\n"); fwrite($file, "--\n"); fwrite($file, "-- Table structure for table $table\n"); fwrite($file, "--\n\n"); fwrite($file, "DROP TABLE IF EXISTS `" . $table . "`;\n"); fwrite($file, "/*!40101 SET @saved_cs_client = @@character_set_client */;\n"); fwrite($file, "/*!40101 SET character_set_client = '".mysql_client_encoding()."' */;\n"); $result = mysql_query("SHOW CREATE TABLE `".$table."`"); if (!$result) { echo sprintf('Database error %1$s for query %2$s', mysql_error(), "SHOW CREATE TABLE `".$table."`")."\n"; return false; } $tablestruc = mysql_fetch_assoc($result); fwrite($file, $tablestruc['Create Table'].";\n"); fwrite($file, "/*!40101 SET character_set_client = @saved_cs_client */;\n"); $result = mysql_query("SELECT * FROM `".$table."`"); if (!$result) { echo sprintf('Database error %1$s for query %2$s', mysql_error(), "SELECT * FROM `".$table."`")."<br />"; return false; } fwrite($file, "--\n"); fwrite($file, "-- Dumping data for table $table\n"); fwrite($file, "--\n\n"); if ($status['Engine']=='MyISAM') fwrite($file, "/*!40000 ALTER TABLE `".$table."` DISABLE KEYS */;\n"); while ($data = mysql_fetch_assoc($result)) { $keys = array(); $values = array(); foreach($data as $key => $value) { if ($value === NULL) $value = "NULL"; elseif ($value === "" or $value === false) $value = "''"; elseif (!is_numeric($value)) $value = "'".mysql_real_escape_string($value)."'"; $values[] = $value; } fwrite($file, "INSERT INTO `".$table."` VALUES ( ".implode(", ",$values)." );\n"); } if ($status['Engine']=='MyISAM') fwrite($file, "/*!40000 ALTER TABLE ".$table." ENABLE KEYS */;\n"); } db_dump('localhost', 'root', '1234', 'youyoubao'); ?> |
在Windows11+Apache2.4.55+PHP8.0.0+MySQL5.0.15环境下,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
<?php //error_reporting(0); function db_dump($host, $user, $pwd, $db) { $con = mysqli_connect($host, $user, $pwd, $db); if (!$con) exit(sprintf('No MySQL connection: %s', mysqli_connect_error())); $tabelstobackup = Array(); $result = mysqli_query($con, "SHOW TABLES FROM `$db`"); if (!$result) exit(sprintf('Database error %1$s for query %2$s', mysqli_error(), "SHOW TABLE STATUS FROM `$db`;")); while ($data = mysqli_fetch_row($result)) { $tabelstobackup[] = $data[0]; } if (count($tabelstobackup) > 0) { $result = mysqli_query($con, "SHOW TABLE STATUS FROM `$db`"); if (!$result) exit(sprintf('Database error %1$s for query %2$s', mysqli_error(), "SHOW TABLE STATUS FROM `$db`;")); $status= Array(); while ($data = mysqli_fetch_assoc($result)) { $status[$data['Name']] = $data; } if ($file = fopen("$db.sql", 'wb')) { fwrite($file, "-- ---------------------------------------------------------\n"); fwrite($file, "-- Database Name: $db\n"); fwrite($file, "-- ---------------------------------------------------------\n\n"); fwrite($file, "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n"); fwrite($file, "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n"); fwrite($file, "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n"); fwrite($file, "/*!40101 SET NAMES '".mysqli_character_set_name($con)."' */;\n"); fwrite($file, "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;\n"); fwrite($file, "/*!40103 SET TIME_ZONE='".mysqli_fetch_array(mysqli_query($con, "SELECT @@time_zone"))[0]."' */;\n"); fwrite($file, "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n"); fwrite($file, "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n"); fwrite($file, "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n"); fwrite($file, "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n\n"); foreach($tabelstobackup as $table) { echo sprintf('Dump database table "%s"',$table) . "<br />"; need_free_memory(($status[$table]['Data_length']+$status[$table]['Index_length'])*3); _db_dump_table($con, $table, $status[$table], $file); } } } } function _db_dump_table($con, $table, $status, $file) { fwrite($file, "\n"); fwrite($file, "--\n"); fwrite($file, "-- Table structure for table $table\n"); fwrite($file, "--\n\n"); fwrite($file, "DROP TABLE IF EXISTS `" . $table . "`;\n"); fwrite($file, "/*!40101 SET @saved_cs_client = @@character_set_client */;\n"); fwrite($file, "/*!40101 SET character_set_client = '".$status["Collation"]."' */;\n"); $result = mysqli_query($con, "SHOW CREATE TABLE `".$table."`"); if (!$result) { echo sprintf('Database error %1$s for query %2$s', mysqli_error(), "SHOW CREATE TABLE `".$table."`")."\n"; return false; } $tablestruc = mysqli_fetch_assoc($result); fwrite($file, $tablestruc['Create Table'].";\n"); fwrite($file, "/*!40101 SET character_set_client = @saved_cs_client */;\n"); $result = mysqli_query($con, "SELECT * FROM `".$table."`"); if (!$result) { echo sprintf('Database error %1$s for query %2$s', mysqli_error(), "SELECT * FROM `".$table."`")."<br />"; return false; } fwrite($file, "--\n"); fwrite($file, "-- Dumping data for table $table\n"); fwrite($file, "--\n\n"); if ($status['Engine']=='MyISAM') fwrite($file, "/*!40000 ALTER TABLE `".$table."` DISABLE KEYS */;\n"); while ($data = mysqli_fetch_assoc($result)) { $keys = array(); $values = array(); foreach($data as $key => $value) { if ($value === NULL) $value = "NULL"; elseif ($value === "" or $value === false) $value = "''"; elseif (!is_numeric($value)) $value = "'".mysqli_real_escape_string($con, $value)."'"; $values[] = $value; } fwrite($file, "INSERT INTO `".$table."` VALUES ( ".implode(", ",$values)." );\n"); } if ($status['Engine']=='MyISAM') fwrite($file, "/*!40000 ALTER TABLE ".$table." ENABLE KEYS */;\n"); } function need_free_memory($memneed) { if (!function_exists('memory_get_usage')) return; $needmemory = @memory_get_usage(true)+inbytes($memneed); if ($needmemory>inbytes(ini_get('memory_limit'))) { $newmemory=round($needmemory/1024/1024)+1 .'M'; if ($needmemory>=1073741824) $newmemory = round($needmemory/1024/1024/1024) .'G'; if ($oldmem=@ini_set('memory_limit', $newmemory)) echo sprintf(__('Memory increased from %1$s to %2$s','backwpup'),$oldmem,@ini_get('memory_limit'))."<br />"; else echo sprintf(__('Can not increase memory limit is %1$s','backwpup'),@ini_get('memory_limit'))."<br />"; } } function inbytes($value) { $multi = strtoupper(substr(trim($value),-1)); $bytes = abs(intval(trim($value))); if ($multi=='G') $bytes = $bytes*1024*1024*1024; if ($multi=='M') $bytes=$bytes*1024*1024; if ($multi=='K') $bytes=$bytes*1024; return $bytes; } db_dump('localhost', 'root', '1234', 'youyoubao'); ?> |