如何使查询和php值更容易获得?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使查询和php值更容易获得?相关的知识,希望对你有一定的参考价值。
我对我的代码有一个不够快的问题,我相信。我注意到注入此代码后,所有进程的速度都降低了:
if (!empty($data['price1']) && !empty($data['price2']) && !empty($data['price3']) && !empty($data['price4']) && !empty($data['price5']) && !empty($data['price6']) && !empty($data['price7']) && !empty($data['price8']) && !empty($data['price9']) && !empty($data['price10']))
$query=$this->db->query("SELECT product_id from ".DB_PREFIX."product WHERE model='".$data['model']."' ");
if($query->num_rows)
$this->db->query("UPDATE ".DB_PREFIX."product_prices SET price='".$this->db->escape($data['price1'])."' WHERE product_id='".$query->row['product_id']."' and customer_group_id='2' ");
$this->db->query("UPDATE ".DB_PREFIX."product_prices SET price='".$this->db->escape($data['price2'])."' WHERE product_id='".$query->row['product_id']."' and customer_group_id='3' ");
$this->db->query("UPDATE ".DB_PREFIX."product_prices SET price='".$this->db->escape($data['price3'])."' WHERE product_id='".$query->row['product_id']."' and customer_group_id='4' ");
$this->db->query("UPDATE ".DB_PREFIX."product_prices SET price='".$this->db->escape($data['price4'])."' WHERE product_id='".$query->row['product_id']."' and customer_group_id='5' ");
$this->db->query("UPDATE ".DB_PREFIX."product_prices SET price='".$this->db->escape($data['price5'])."' WHERE product_id='".$query->row['product_id']."' and customer_group_id='6' ");
$this->db->query("UPDATE ".DB_PREFIX."product_prices SET price='".$this->db->escape($data['price6'])."' WHERE product_id='".$query->row['product_id']."' and customer_group_id='7' ");
$this->db->query("UPDATE ".DB_PREFIX."product_prices SET price='".$this->db->escape($data['price7'])."' WHERE product_id='".$query->row['product_id']."' and customer_group_id='8' ");
$this->db->query("UPDATE ".DB_PREFIX."product_prices SET price='".$this->db->escape($data['price8'])."' WHERE product_id='".$query->row['product_id']."' and customer_group_id='9' ");
$this->db->query("UPDATE ".DB_PREFIX."product_prices SET price='".$this->db->escape($data['price9'])."' WHERE product_id='".$query->row['product_id']."' and customer_group_id='10' ");
$this->db->query("UPDATE ".DB_PREFIX."product_prices SET price='".$this->db->escape($data['price10'])."' WHERE product_id='".$query->row['product_id']."' and customer_group_id='11' ");
else
$this->db->query("INSERT INTO ".DB_PREFIX."product_prices SET product_id='".$product_id."',price='".$data['price1']."',customer_group_id='2'");
$this->db->query("INSERT INTO ".DB_PREFIX."product_prices SET product_id='".$product_id."',price='".$data['price2']."',customer_group_id='3'");
$this->db->query("INSERT INTO ".DB_PREFIX."product_prices SET product_id='".$product_id."',price='".$data['price3']."',customer_group_id='4'");
$this->db->query("INSERT INTO ".DB_PREFIX."product_prices SET product_id='".$product_id."',price='".$data['price4']."',customer_group_id='5'");
$this->db->query("INSERT INTO ".DB_PREFIX."product_prices SET product_id='".$product_id."',price='".$data['price5']."',customer_group_id='6'");
$this->db->query("INSERT INTO ".DB_PREFIX."product_prices SET product_id='".$product_id."',price='".$data['price6']."',customer_group_id='7'");
$this->db->query("INSERT INTO ".DB_PREFIX."product_prices SET product_id='".$product_id."',price='".$data['price7']."',customer_group_id='8'");
$this->db->query("INSERT INTO ".DB_PREFIX."product_prices SET product_id='".$product_id."',price='".$data['price8']."',customer_group_id='9'");
$this->db->query("INSERT INTO ".DB_PREFIX."product_prices SET product_id='".$product_id."',price='".$data['price9']."',customer_group_id='10'");
$this->db->query("INSERT INTO ".DB_PREFIX."product_prices SET product_id='".$product_id."',price='".$data['price10']."',customer_group_id='11'");
我不是专业的php编码专家,试图尽力而为,只是我认为可能有更快的方法来更新选定的表,或者如果表为空,则插入新值。尝试以其他方式执行此操作,例如检查每个数据[price1],[price2]等,但这要慢得多,因此尝试以这种方式进行-仍在缓慢进行。
答案
使用循环foreach
类似这样,但我不知道您如何管理“ $ product_id”和“ customer_group_id”
$query=$this->db->query("SELECT product_id from ".DB_PREFIX."product WHERE model='".$data['model']."' ");
foreach($data as $key=>$value)
$customer_group_id = $key+1;
if($query->num_rows)
$this->db->query("UPDATE ".DB_PREFIX."product_prices SET price='".$this->db->escape($value)."' WHERE product_id='".$query->row['product_id']."' and customer_group_id='".$customer_group_id."' ");
else
$this->db->query("INSERT INTO ".DB_PREFIX."product_prices SET product_id='".$product_id."',price='".$value."',customer_group_id='".$customer_group_id."'");
以上是关于如何使查询和php值更容易获得?的主要内容,如果未能解决你的问题,请参考以下文章