PHP 调用 MySQL 存储过程并获得返回值的代码
html页面
<form method="post" >
<p>Enter Department ID:
<input type="text" name="dept_id" size="4">
<input type="submit" name="submit" value="submit"><p>
</form>
php代码
<?php
$hostname = "localhost";
$username = "root";
$password = "secret";
$database = "prod";
if (IsSet ($_POST['submit'])) {
$dbh = new mysqli($hostname, $username, $password, $database);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit ();
}
$dept_id = $_POST['dept_id'];
if ($result_set = $dbh->query("call employee_list( $dept_id )")) {
print ('<table border="1" width="30%"> <tr> '.
'<td>Employee_id</td><td>Surname</td><td>Firstname</td></tr>');
while ($row = $result_set->fetch_object()) {
printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n",
$row->employee_id, $row->surname, $row->firstname);
}
} else {
printf("<p>Error:%d (%s) %s\n", mysqli_errno($dbh),
mysqli_sqlstate($dbh), mysqli_error($dbh));
}
print ("</table> ");
$dbh->close();
}
?>
将明文密码批量导入进mysql数据库的代码
<?php
set_time_limit(13600); //设置时间
$conn=mysql_connect()('localhost','root',''); //连接数据库
mysql_select_db('csdn',$conn);
mysql_query()("SET NAMES utf8");
$file=file('csdn.txt');
foreach($file as $line)
{
$u=explode()('# ', $line);
mysql_query("INSERT INTO `user` (username,password,email)VALUES('".trim($u[0])."','".trim($u[1])."','".trim($u[2])."')",$conn);
}
echo 'OK';
?>
本文介绍php自动删除MYSQL死连接(Sleep)的方法,供大家学习参考。
define('MAX_SLEEP_TIME', 120);
$hostname = "localhost";
$username = "root";
$password = "password";
$connect = mysql_connect()($hostname, $username, $password);
$result = mysql_query()("SHOW PROCESSLIST", $connect);
while ($proc = mysql_fetch_assoc($result)) {
if ($proc["Command"] == "Sleep" && $proc["Time"] > MAX_SLEEP_TIME) {
@mysql_query("KILL " . $proc["Id"], $connect);
}
}
mysql_close($connect);
?>
将以上代码中的$password修改改成你的数据库密码,死连接的时间也可以修改。
然后加入计划任务就可以了。比如用 crontab -e 命令加入: