Php OOP选择包括连接
我已经有了我正在使用的OOP选择,我想将JOIN添加到功能中。但我的新代码没有生成数组。有人能帮忙吗 这是我最初的简单选择,它工作起来就像一个梦Php OOP选择包括连接,php,mysql,oop,Php,Mysql,Oop,我已经有了我正在使用的OOP选择,我想将JOIN添加到功能中。但我的新代码没有生成数组。有人能帮忙吗 这是我最初的简单选择,它工作起来就像一个梦 public function select($table, $rows = '*', $where = null, $order = null) { $q = 'SELECT '.$rows.' FROM '.$table; if($where != null) $q .= ' WHERE '.$where;
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;
$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;
}
}
公共函数选择($table,$rows='*',$where=null,$order=null)
{
$q='从'.$table'中选择'.$rows';
if($where!=null)
$q.='WHERE'.$WHERE;
如果($order!=null)
$q.='ORDER BY'.$ORDER;
$query=@mysql\u query($q);
如果($query)
{
$this->numResults=mysql\u num\u rows($query);
对于($i=0;$i<$this->numResults;$i++)
{
$r=mysql\u fetch\u数组($query);
$key=array\u key($r);
对于($x=0;$x1)
$this->result[$i][$key[$x]]=$r[$key[$x]];
else if(mysql_num_rows($query)<1)
$this->result=null;
其他的
$this->result[$key[$x]]=$r[$key[$x]];
}
}
}
返回true;
}
其他的
{
返回false;
}
}
这是我尝试添加一种使用join的方法,但它不返回数组
public function select($table, $rows = '*', $join = null, $where = null, $order = null){
// Create query from the variables passed to the function
$q = 'SELECT '.$rows.' FROM '.$table;
if($join != null){
$q .= ' JOIN '.$join;
}
if($where != null){
$q .= ' WHERE '.$where;
}
if($order != null){
$q .= ' ORDER BY '.$order;
}
// Check to see if the table exists
if($this->tableExists($table)){
// The table exists, run the query
$query = @mysql_query($q);
if($query){
// If the query returns >= 1 assign the number of rows to numResults
$this->numResults = mysql_num_rows($query);
// Loop through the query results by the number of rows returned
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; // Query was successful
}else{
array_push($this->result,mysql_error());
return false; // No rows where returned
}
}else{
return false; // Table does not exist
}
}
公共函数选择($table,$rows='*',$join=null,$where=null,$order=null){
//从传递给函数的变量创建查询
$q='从'.$table'中选择'.$rows';
如果($join!=null){
$q.=“加入”。$JOIN;
}
if($where!=null){
$q.='WHERE'.$WHERE;
}
如果($order!=null){
$q.='ORDER BY'.$ORDER;
}
//检查该表是否存在
如果($this->tableExists($table)){
//如果表已存在,请运行查询
$query=@mysql\u query($q);
如果($query){
//如果查询返回>=1,则将行数分配给numResults
$this->numResults=mysql\u num\u rows($query);
//按返回的行数循环查询结果
对于($i=0;$i<$this->numResults;$i++){
$r=mysql\u fetch\u数组($query);
$key=array\u key($r);
对于($x=0;$x1){
$this->result[$i][$key[$x]]=$r[$key[$x]];
}else if(mysql_num_rows($query)<1){
$this->result=null;
}否则{
$this->result[$key[$x]]=$r[$key[$x]];
}
}
}
}
return true;//查询成功
}否则{
数组_push($this->result,mysql_error());
return false;//没有返回的行
}
}否则{
return false;//表不存在
}
}
如果要调用此函数,则应如下声明:
public function select($table, $rows, $where, $order)
{
// your statements
}
select('your_table','rows_you_want_select','where_conditions','column_you_want_to_sort');
public function select($table, $rows, $join, $where, $order)
{
// your statements
}
select('your_table','rows_you_want_select','join_conditions','where_conditions','column_you_want_to_sort');
那么就这样称呼它:
public function select($table, $rows, $where, $order)
{
// your statements
}
select('your_table','rows_you_want_select','where_conditions','column_you_want_to_sort');
public function select($table, $rows, $join, $where, $order)
{
// your statements
}
select('your_table','rows_you_want_select','join_conditions','where_conditions','column_you_want_to_sort');
另一方面,如果您试图放入join语句,请按如下方式声明:
public function select($table, $rows, $where, $order)
{
// your statements
}
select('your_table','rows_you_want_select','where_conditions','column_you_want_to_sort');
public function select($table, $rows, $join, $where, $order)
{
// your statements
}
select('your_table','rows_you_want_select','join_conditions','where_conditions','column_you_want_to_sort');
那么就这样称呼它:
public function select($table, $rows, $where, $order)
{
// your statements
}
select('your_table','rows_you_want_select','where_conditions','column_you_want_to_sort');
public function select($table, $rows, $join, $where, $order)
{
// your statements
}
select('your_table','rows_you_want_select','join_conditions','where_conditions','column_you_want_to_sort');
例如:
select('table1','id',' table2 on table1.id = table2.id','id = 1','id');
您仍有很多需要了解的内容,请避免使用
mysql\uu
。而是使用mysqli\uu
或PDO
。如果您真的想要OOP,那么我强烈建议。。它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果您选择PDO,。这方面的OOP是什么?您是否检查了您的查询???。请尝试echo$query;并执行以检查查询是否正确not@MadaraUchiha谢谢你。完全忽略了这一点。为什么不使用已经内置OOP的PDO呢?