Rumah > Artikel > pembangunan bahagian belakang > 一段查询代码多处使用可以吗
$bliang='这是个变量'$sql=SELECT * FROM A表 WHERE cart LIKE '%.$bliang.%'$query=mysql_query($sql);while($row=mysql_fetch_array($query)){ …………}
function foo($x){global $con;$sql=SELECT * FROM A表 WHERE cart LIKE '%.$x.%'mysql_query($sql,$con);// some codereturn $query;}
function foo($x){global $con;$sql=SELECT * FROM A表 WHERE cart LIKE '%.$x.%'mysql_query($sql,$con);// some codereturn $query;}
function foo($x){global $con;$sql=SELECT * FROM A表 WHERE cart LIKE '%.$x.%'mysql_query($sql,$con);// some codereturn $query;}
写成函数啊,我也是刚学php,我给你一个我自己做的类参考下吧,所有数据库操作都在这里了
<?php// | PHP version 5.3// 数据库操作类 Order by phuai007 // Date 2014/2class my_sql { public $dsn = 'mysql:host=localhost;dbname=lif2';//host为数据库连接地址,dbname为数据库名 public $user = 'root'; //数据库连接用户名 public $pass = '123456'; //对应的密码 public $names = 'SET NAMES UTF8'; //数据库查询编码 //查询数据库返回结果 public function sql_select($sql) { try { $dbh = new PDO($this->dsn, $this->user, $this->pass); $dbh->query($this->names); return $dbh->query($sql); $dbh = null; } catch(Exception $e) { echo 'error: ' . $e->getMessage(); } } //操作单条数据(更新/删除/插入),无返回结果 public function sql_one($sql) { try { $dbh = new PDO($this->dsn, $this->user, $this->pass); $dbh->exec($this->names); $dbh->exec($sql); $dbh = null; } catch(Exception $e) { echo 'error: ' . $e->getMessage(); } } //操作多条数据(更新/删除),无返回结果 public function sql_more($sql, $str) { try { $dbh = new PDO($this->dsn, $this->user, $this->pass); $dbh->exec($this->names); foreach ($str as $arrs) { $dbh->exec($sql . $arrs); } $dbh = null; } catch(Exception $e) { echo 'error: ' . $e->getMessage(); } } //参数化查询数据库返回结果(单条) public function cs_sql_select($sql,$str) { try { $dbh = new PDO($this->dsn, $this->user, $this->pass); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $dbh->query($this->names); $stmt = $dbh->prepare($sql); $stmt->bindValue(":key", $str ,PDO::PARAM_INT); $stmt->execute(); return $stmt; $dbh = null; } catch(Exception $e) { echo 'error: ' . $e->getMessage(); } } //参数化查询操作多条数据(删除/更新),无返回结果 public function cs_sql_more($sql, $str) { try { $dbh = new PDO($this->dsn, $this->user, $this->pass); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $dbh->exec($this->names); foreach ($str as $arrs) { $stmt = $dbh->prepare($sql); $stmt->bindValue(":key", $arrs,PDO::PARAM_INT); $stmt->execute(); } $dbh = null; } catch(Exception $e) { echo 'error: ' . $e->getMessage(); } } //参数化查询操作单条数据(删除/更新),无返回结果 public function cs_sql_one($sql, $str) { try { $dbh = new PDO($this->dsn, $this->user, $this->pass); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $dbh->exec($this->names); $stmt = $dbh->prepare($sql); $stmt->bindValue(":key", $str,PDO::PARAM_INT); $stmt->execute(); $dbh = null; } catch(Exception $e) { echo 'error: ' . $e->getMessage(); } } ////////}?>
<?phpinclude './conn.php';$mysql = new my_sql;if($_REQUEST["action"] == 'del'){if($_REQUEST['id']!= ''){//$mysql->sql_one("DELETE FROM `user_type` WHERE `user_id` = ".$_REQUEST['id']."");//无参数化查询操作$mysql->cs_sql_one("DELETE FROM `user_type` WHERE `user_id` = :key",$_REQUEST['id']);//参数化查询操作}else{echo '非法操作';}} //多选删除else if($_REQUEST["action"] == 'delCheck'){$id=$_POST['delAll'];if(isset($id)){//$mysql->sql_more("DELETE FROM `user_type` WHERE `user_id` = ",$id);//无参数化查询操作$mysql->cs_sql_more("DELETE FROM `user_type` WHERE `user_id` = :key",$id);//参数化查询操作}}//读取数据库?><div id="main" class="table_div_1280"> <div style="display:block" id="info"> <form id="form2" name="form2" action="?action=delCheck" method="post"> <table> <tr id="title"> <td>编号</td> <td>ID</td> <td>姓名</td> <td>性别</td> <td>电话</td> <td>QQ</td> <td>地址</td> <td>email</td> <td>删除</td> <td>选择</td> </tr> <?php $aa=$mysql->sql_select('SELECT * FROM user_type order by user_id'); foreach ($aa as $row) {//输出内容echo '<tr>';echo '<td>'.$row['user_id'].'</td><td>'.$row['user_name'].'</td><td>'.$row['user_real_name'].'</td><td>'.$row['user_sex'].'</td><td>'.$row['user_tel'].'</td><td>'.$row['user_qq'].'</td><td>'.$row['user_address'].'</td><td>'.$row['user_email'].'</td><td><a href="?action=del&id='.$row['user_id'].'" title="删除">删除</a></td><td><input name="delAll[]" class="c" type="checkbox" value="'.$row['user_id'].'" /></td>';echo '</tr>';}?> <tr> <td colspan="7"></td> <td colspan="2"><input type="button" onclick="check(0)" value="全选"/> <input type="button" onclick="check(1)" value="反选"/> <input type="button" onclick="check(2)" value="清空"/></td> <td><input type="button" onClick="delall()" value="删除所选" /></td> </tr> </table> </form> </div> <?php $dbh = null;?>
function foo($x){global $con;$sql=SELECT * FROM A表 WHERE cart LIKE '%.$x.%'mysql_query($sql,$con);// some codereturn $query;}
写成函数啊,我也是刚学php,我给你一个我自己做的类参考下吧,所有数据库操作都在这里了
我是这样的意思
$b='这是个变量'$sql=SELECT * FROM A表 WHERE cart LIKE '%.$b.%'$query=mysql_query($sql);while($row=mysql_fetch_array($query)){ $new="$row['title']." - ".$row['con']." - ".$row['num'].";}
写成函数啊,我也是刚学php,我给你一个我自己做的类参考下吧,所有数据库操作都在这里了
我是这样的意思
$b='这是个变量'$sql=SELECT * FROM A表 WHERE cart LIKE '%.$b.%'$query=mysql_query($sql);while($row=mysql_fetch_array($query)){ $new="$row['title']." - ".$row['con']." - ".$row['num'].";}
$new="$row['title']." - ".$row['con']." - ".$row['num'].";
改写为
$new[$row['title']] = $row['title']." - ".$row['con']." - ".$row['num'];
function foo($x){global $con;$sql=SELECT * FROM A表 WHERE cart LIKE '%.$x.%'mysql_query($sql,$con);// some codereturn $query;}
function foo($x){global $con;$sql=SELECT * FROM A表 WHERE cart LIKE '%.$x.%'mysql_query($sql,$con);// some codereturn $query;}
$tagxs_lx="select * from ".DB_MUSICDATA." left join ".DB_SINGER." on ".DB_MUSICDATA.".singer_id=".DB_SINGER.".singer_id left join ".DB_TAG." on ".DB_MUSICDATA.".tag_id=".DB_TAG.".tag_id and ".DB_TAG.".tag like '%标签1%' order by ".DB_MUSICDATA.".music_id desc limit 10";$query_lx=mysql_query($tagxs_lx);$num=0;$tag1="";while($row=mysql_fetch_array($query_lx)){$num++;if($row['singer_id']==""){$singer="";}else{$singer='<a title="'.$row['singer_name'].'" href="'.Domain.'geshou/'.$row['singer_id'].'.html" target="_2">'.$row['singer_name'].'</a>';} $tag1 .='<li><span class="num">'.sprintf("%02d", $num).'</span><span class="singer_title"><a title="'.$row['music_name'].'" href="'.Domain.'yinyue/'.$row['music_id'].'.html" target="_1">'.$row['music_name'].'</a></span><span class="songer">'.$singer.'</span><span class="add"><a title="加入播放列表" class="add" onclick="set_List(\''.$row['music_id'].'|'.$row['music_name'].'|'.$row['singer_name'].'\');">加入播放列表</a></span></li>';}
然后我页面调用的时候 直接 就可以了
然后我页面调用的时候 直接 就可以了
取回的是数组 你这种需求反人类...难道你要在function里把格式都写好,那也是反人类设计.
所以 我觉得你还是按照你原来的额写法写算了,别搞function了....
function你能看懂 剩下的不就是循环出数据 拼接成你要的样子吗?这个地方也不熟悉的话 我建议你先弄清楚返回的数据结构...
不知道怎么说了
查找别的问题 偶然解决了这个, 回来做个标记 顺便吧解决方法贴上来留着
function index($title,$limit){$text="select * from test wheretag like '%$title%' order by test.id desc limit $limit"; return $text;}echo index('条件','可以多个');