我有一个简单的登录和注册表单,有一些额外的插入和选择查询各种不同的东西。我真的需要MySQL查询的类吗?
我已经完成了本教程(http://code.tutsplus.com/tutorials/real-world-oop-with-php-and-mysql--net-1918),该教程创建了一个PHP类,该类很容易选择,插入,断开连接,更新到MySQL服务器。
事情是,现在我已经完成了教程,并开始实施一些从静态选择和插入查询到引用类内函数的新查询的更改,我看到新代码更长,更复杂,我觉得这是课堂的目标。
有什么想法和建议吗?
例如,一般插入查询云:
mysqli_query("INSERT INTO statuses(User_ID, Status)VALUES('$userid', '$statusupdate')") or die(myself_error());
然而,上面提到的教程所需的以下:
$db->insert('mysqlcrud',array(3,"Name 4","[email protected]<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>"));
类文件表示:
<?php
/**
* Created by PhpStorm.
* User: marshall
* Date: 01/03/14
* Time: 21:34
*/
namespace MySQL\lib;
class Database {
private $db_host = 'localhost';
private $db_user = 'root';
private $db_pass = 'pass';
private $db_name = 'database';
public function connect() {
if(!$this->con) {
$myconn = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
if(myconn) {
$seldb = mysql_select_db($this->db_name,$myconn);
if($seldb) {
$this->con = true;
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return true;
}
}
public function disconnect() {
if($this->con)
{
if(mysql_close())
{
$this->con = false;
return true;
}
else
{
return false;
}
}
}
private $result = array();
private function tableExists($table)
{
$tablesInDb = mysql_query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"');
if($tablesInDb)
{
if(mysql_num_rows($tablesInDb)==1)
{
return true;
}
else
{
return false;
}
}
}
public function select($table, $rows = '*', $where = null, $order = null)
{
$q = 'SELECT '.$rows.' FROM '.$table;
if($where != null)
$q .= ' WHERE '.$where;
if($order != null)
$q .= ' ORDER BY '.$order;
if($this->tableExists($table))
{
$query = mysql_query($q);
if($query)
{
$this->numResults = mysql_num_rows($query);
for($i = 0; $i < $this->numResults; $i++)
{
$r = mysql_fetch_array($query);
$key = array_keys($r);
for($x = 0; $x < count($key); $x++)
{
// Sanitizes keys so only alphavalues are allowed
if(!is_int($key[$x]))
{
if(mysql_num_rows($query) > 1)
$this->result[$i][$key[$x]] = $r[$key[$x]];
else if(mysql_num_rows($query) < 1)
$this->result = null;
else
$this->result[$key[$x]] = $r[$key[$x]];
}
}
}
return true;
}
else
{
return false;
}
}
else
return false;
}
public function insert($table,$values,$rows = null)
{
if($this->tableExists($table))
{
$insert = 'INSERT INTO '.$table;
if($rows != null)
{
$insert .= ' ('.$rows.')';
}
for($i = 0; $i < count($values); $i++)
{
if(is_string($values[$i]))
$values[$i] = '"'.$values[$i].'"';
}
$values = implode(',',$values);
$insert .= ' VALUES ('.$values.')';
$ins = mysql_query($insert);
if($ins)
{
return true;
}
else
{
return false;
}
}
}
public function delete($table,$where = null)
{
if($this->tableExists($table))
{
if($where == null)
{
$delete = 'DELETE '.$table;
}
else
{
$delete = 'DELETE FROM '.$table.' WHERE '.$where;
}
$del = mysql_query($delete);
if($del)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public function update($table,$rows,$where)
{
if($this->tableExists($table))
{
// Parse the where values
// even values (including 0) contain the where rows
// odd values contain the clauses for the row
for($i = 0; $i < count($where); $i++)
{
if($i%2 != 0)
{
if(is_string($where[$i]))
{
if(($i+1) != null)
$where[$i] = '"'.$where[$i].'" AND ';
else
$where[$i] = '"'.$where[$i].'"';
}
}
}
$where = implode('=',$where);
$update = 'UPDATE '.$table.' SET ';
$keys = array_keys($rows);
for($i = 0; $i < count($rows); $i++)
{
if(is_string($rows[$keys[$i]]))
{
$update .= $keys[$i].'="'.$rows[$keys[$i]].'"';
}
else
{
$update .= $keys[$i].'='.$rows[$keys[$i]];
}
// Parse to add commas
if($i != count($rows)-1)
{
$update .= ',';
}
}
$update .= ' WHERE '.$where;
$query = mysql_query($update);
if($query)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public function getResult()
{
return $this->result;
}
}
开始类的代码可能会更长,复杂但肯定会简化这个过程,当你真正开始使用的类对象您的代码 – anurupr
请分享您的代码。也许你需要在现有的类中创建新的方法......而且,是的,面向对象的方法应该在更复杂的事情中展示它的优点... – sinisake
你的第二个片段中的JavaScript是什么?它是否意外? –