php 连接操作mysql数据库
2017-05-04 17:55
309 查看
PHP示例代码:
html 向php通过表单提交数据且不打开php页面(需jquery.js)
aaa.php 取数据代码:
<?php $funtype=$_POST['funtype']; switch($funtype) { case 'returnvalue': echo "value: $value" ; break; case 'searchMessage': LoginMySql() ; searchMessage() ; break; case 'InsertMessage': LoginMySql(); InsertMessage(); break; case 'UpdateMessage': LoginMySql(); UpdateMessage(); break; case 'DeleteMessage': LoginMySql(); DeleteMessage(); break; } function LoginMySql() { $host = 'localhost'; $database = 'tablename'; $username = 'xxxxxx'; $password = '********'; $connection = mysql_connect($host, $username, $password);//连接到数据库 mysql_query("set names 'utf8'");//编码转化 if (!$connection) { die("could not connect to the database.\n" . mysql_error());//诊断连接错误 } $selectedDb = mysql_select_db($database);//选择数据库 if (!$selectedDb) { die("could not to the database\n" . mysql_error()); } //echo "ok"; } function searchMessage() { $selectName = $_POST['value']; $selectName = mysql_real_escape_string($selectName);//防止SQL注入 $query = "select * from buycount where id = '$selectName'";//构建查询语句 $result = mysql_query($query);//执行查询 if (!$result) { die("could not to the database\n" . mysql_error()); } while ($row = mysql_fetch_array($result)) { //取出结果并显示 $clothid = $row["clothid"]; $price = $row["price"]; $resultvalue = "{\"clothid\":\"$clothid\"}"; echo "$resultvalue"; } //while ($row = mysql_fetch_row($result)) { //取出结果并显示 //$id = $row[0]; //$buyurl = $row[1]; //$buystr = $row[2]; //$resultvalue = "{\"buyurl\":\"$buyurl\",\"buystr\":\"$buystr\"}"; //echo "$resultvalue"; //} } function InsertMessage() { $InsertValue = $_POST['value']; $json_InsertValue=json_decode($InsertValue); $infostr = $json_InsertValue->info; $ipstr = getIP(); $datetimestr =date("Y-m-d H:i:s"); $portstr = $_SERVER['REMOTE_PORT'];//端口号 $insert = "INSERT INTO couponct (datetime,ip,port,info) VALUES ('$datetimestr ','$ipstr','$portstr ','$infostr')";//构建插入语句 $resultInsert = mysql_query($insert);//执行插入 if (!$resultInsert) { die("could not to the database\n" . mysql_error()); }else{ echo "InsertOK"; } } function UpdateMessage() { $UpdateValue = $_POST['value']; $json_InsertValue=json_decode($UpdateValue); $id = $json_InsertValue->id; //prints foo $buyurl = $json_InsertValue->buyurl; //prints foo $buystr = $json_InsertValue->buystr; mysql_query("update buycount SET buyurl = '$buyurl' WHERE buystr = '$buystr' ") or die('set Error: ' . mysql_error()); echo "UpdateOK"; } function DeleteMessage() { $DeleteValue = $_POST['value']; $json_InsertValue=json_decode($DeleteValue); $id = $json_InsertValue->id; // //删除 fname='wenjian' and lname='31' mysql_query("delete from buycount where id='$id' ") or die('delete Error: ' . mysql_error()); echo "DeleteOK"; } function getIP() { if (getenv('HTTP_CLIENT_IP')) { $ip = getenv('HTTP_CLIENT_IP'); }elseif (getenv('HTTP_X_FORWARDED_FOR')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); }elseif (getenv('HTTP_X_FORWARDED')) { $ip = getenv('HTTP_X_FORWARDED'); }elseif (getenv('HTTP_FORWARDED_FOR')) { $ip = getenv('HTTP_FORWARDED_FOR'); }elseif (getenv('HTTP_FORWARDED')) { $ip = getenv('HTTP_FORWARDED'); }else { $ip = $_SERVER['REMOTE_ADDR']; } return $ip; } ?>
html 向php通过表单提交数据且不打开php页面(需jquery.js)
function tophp(curtype) { var userName = getQueryString("uid"); if(userName ==""){ userName = "null"; } $poststring = "uid="+ userName +"&type="+ curtype +"&info="+ "null"; $.ajax({ //alert("aaaaaaaaaaa"); //拿到结果 type: "post", url: "http://xxx/aaa.php", //data: "name=" + $("#你要传输的文本框id").val(), data: $poststring, success: function (result) { $("#returncontent").html(result); // alert(result.msg); //拿到结果 } }); }
aaa.php 取数据代码:
$uidstr = $_POST['uid']; $typestr =$_POST['type']; $infostr = $_POST['info'];
相关文章推荐
- php连接和操作mysql数据库
- php实现mysql数据库连接操作及用户管理
- PHP连接和操作MySQL数据库基础教程
- 细谈php mysql数据库连接操作
- PHP连接和操作MySQL数据库基础教程
- php如何连接MYSQL数据库并进行添加、修改、删除操作
- php操作mysql数据库的连接语句以及最简单的增删改查语句
- PHP连接和操作MySQL数据库基础教程
- PHP连接到MYSQL数据库的相关操作
- php实现mysql数据库连接操作及用户管理
- php连接mysql数据库的增删改查操作
- php连接mysql数据库代码
- php 连接MySQL数据库
- php adodb操作mysql数据库
- php adodb操作mysql数据库
- Flex 3 和PHP连接mysql数据库
- 通过PHP连接MYSQL数据库 创建数据库 创建表
- PHP 操作mysql数据库
- PHP远程连接MYSQL数据库非常慢的解决方法
- PHP同时操作多个MySQL连接