In the function below I am implementing substring for the field $field but I am getting,
Error Notice: Error: Unknown column 'EBook_at' in 'field list'
I know there is no column like EBook_at but how substring the $field.
Expecting Output:
/image/56789011/5678901145678
public function prependText($adjustment) {
$prepend_text = $adjustment[0];
$field = $adjustment[1];
$this->db->query('UPDATE ' . DB_PREFIX . "hj_import SET `" . $field . "` = CONCAT( '" . $this->db->escape($prepend_text) . "'," . SUBSTR($field,0,8) . "`, `" . $field . "` )");
}
It is fixed by using below query :
$this->db->query('UPDATE ' . DB_PREFIX . "hj_import SET `" . $field . "` = CONCAT( '" . $this->db->escape($prepend_text) . "', substr($field, 1, 8 ),'/',`" . $field . "` )");
Related
I selected a product and then I have this product in a table, when I want to select other product I have to filter the suggestion of the products it should show the options without the product to add to the table. But I dont know how, if you know an option or an idea I'd appreciate it.
query:
$Input = $request->key;
$almacenOrigen = $request->almacenOrigen;
$lengthSku = 16;
$Sku = substr($Input, 0, $lengthSku);
$name = DB::table('products')
->leftJoin('line_products', 'products.lineProduct_id', '=', 'line_products.id')
->leftJoin('inventories', 'products.id', '=', 'inventories.product_id')
->leftJoin('warehouses', 'inventories.warehouse_id', '=', 'warehouses.id')
->where('inventories.status_id', 1)
->where('products.sku', $Sku)
->where('warehouses.id', $almacenOrigen)
->whereNull('products.deleted_at')
->select(
'products.sku',
'products.reference',
'products.id',
'products.name as nameP',
'line_products.skuLength',
'line_products.expirationLength',
'line_products.lotLength',
'line_products.traceabilityLength',
'line_products.name as nameL',
'inventories.lot',
'inventories.warehouse_id',
'inventories.id as inventory_id',
'inventories.traceability'
)
->get();
$html = '';
if (!empty($name)) {
$Expiration = substr($Input, $name[0]->skuLength, $name[0]->expirationLength);
$yearToday = substr(Carbon::now(), 0, 2);
$year = $yearToday . substr($Expiration, 2, 2);
$month = substr($Expiration, 4, 2);
$day = substr($Expiration, 6, 2);
$Expiration = $year . '-' . $month . '-' . $day;
$Lot = substr($Input, $name[0]->skuLength + $name[0]->expirationLength, $name[0]->lotLength);
$Lot = substr($Lot, 2);
$Traceability = substr($Input, $name[0]->skuLength + $name[0]->expirationLength + $name[0]->lotLength, $name[0]->traceabilityLength);
$Traceability = substr($Traceability, 2);
foreach ($name as $value) {
$html .= '
<div>
<a style="color: #000000" class="suggest-element" exist="1" inventory_id="'.($value->inventory_id).'" lot="'.($value->lot).'" warehouse_id="'.($value->warehouse_id).'" "'
. '" sku="' . ($value->sku)
. '" reference="' . ($value->reference)
. '" caducidad="' . ($Expiration)
. '" lote="' . ($Lot)
. '" trazabilidad="' . ($Traceability)
. '" nameL="' . ($value->nameL)
. '" nameP="' . ($value->nameP)
. '" data="' . ($value->sku . $Expiration . $Lot . $Traceability)
. '" id="' . $value->id . '">'
. ($value->inventory_id . "-" .$value->sku .$Expiration . $Lot . $Traceability ) .
'</a>
</div>'
;
Example:
This are the suggestions, if I select id = 1 this is added in a table then when I want to select another the id=1 have not to appear.
|id |---- sku ----|
| 1 |108888893024899|
| 2 |108888893024896|
| 3 |108888893024895|
According to w3resource you can replace text using REPLACE keyword.
Now look at this code extracted from OpenCart
<?php
class ModelToolOnline extends Model {
public function addOnline($ip, $customer_id, $url, $referer) {
$this->db->query("DELETE FROM `" . DB_PREFIX . "customer_online` WHERE date_added < '" . date('Y-m-d H:i:s', strtotime('-1 hour')) . "'");
$this->db->query("REPLACE INTO `" . DB_PREFIX . "customer_online` SET `ip` = '" . $this->db->escape($ip) . "', `customer_id` = '" . (int)$customer_id . "', `url` = '" . $this->db->escape($url) . "', `referer` = '" . $this->db->escape($referer) . "', `date_added` = '" . $this->db->escape(date('Y-m-d H:i:s')) . "'");
}
}
?>
They have Replace query with SET keyword and no WHERE condition. Now I couldn't find anything similar to this anywhere to understand whats happening in this query.
If anyone knows what is happening in this query please explain with authentic source link
That are two different things. There is a REPLACE() function and a REPLACE statement.
In the code the REPLACE statement is used. It is similar to an INSERT statement, and thus has no WHERE clause.
Here's what the documentatin says:
REPLACE works exactly like INSERT, except that if an old row in the
table has the same value as a new row for a PRIMARY KEY or a UNIQUE
index, the old row is deleted before the new row is inserted.
Beginner here trying out PDO/MySQL. I am coding a simple SELECT * and displaying the results. The code below works fine to display the contents.
$sql = 'SELECT * FROM names'; //fieldnames are id, name, email
$stmt = $db->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch())
{echo $row[0] . " | " . $row[1] . " | " . $row[2] . "<br/>";}
However, when I use the fieldname as the index instead of the number, I get a PHP notice:
Notice: Undefined index: name
Codes below does not work.
{echo $row['id'] . " | " . $row['name'] . " | " . $row['email'] . "<br/>";}
I have seen several tutorials where they have used fieldname as the index. Can someone provide some further clarification on when/how the fieldname can be used instead of the number for the index?
How to define where condition in this below query?form the database except the zero value need to shown
private function totalVisitedPagesPerUser() {
$query = "SELECT COUNT(*), " . $this->_db->quoteName('customer_name') . "," .
"\n MAX(". $this->_db->quoteName('visit_timestamp') . ")," . $this->_db->quoteName('browser') . "," .
$this->_db->quoteName('os') . "," .
$this->_db->quoteName('session_id_person') . "," .
$this->_db->quoteName('ip') . "," .
"\n SUM(" . $this->_db->quoteName('impulse') . ")," .
$this->_db->quoteName('geolocation') .
"\n FROM ( SELECT * FROM #__realtimeanalytics_serverstats " . $this->whereQuery . " ORDER BY `visit_timestamp` DESC) AS INTABLE" .
$this->whereQuery .
"\n GROUP BY " . $this->_db->quoteName('session_id_person') .
"\n ORDER BY " . $this->_db->quoteName('customer_name');
$this->_db->setQuery($query);
$results = $this->_db->loadRowList();
if ($this->_db->getErrorNum()) {
throw new JRealtimeException(JText::sprintf('COM_JREALTIME_ERROR_RECORDS', $this->_db->getErrorMsg()), 'error');
}
return $results;
}
you can prepare where condition variable by checking conditions before query start, than append that variable at that point like:
$wherecondtion = "Where 1=1";
if($condition1){$wherecondtion."AND (add required filter 1)"}
if($condition2){$wherecondtion."AND (add required filter 2)"}
your subquery will be like:
"( SELECT * FROM #__realtimeanalytics_serverstats " . $wherecondtion . " ORDER BY `visit_timestamp` DESC)"
Is this the thing you needed or something different thing?
Hi I've searched for a solution to this and found several answers and after many edits to the code and no success I'm asking here directly.
$return_arr = array();
$fetch = tep_db_query("select * from products, " . TABLE_PRODUCTS_DESCRIPTION . " WHERE products.products_status = '1' and products.products_id = " . TABLE_PRODUCTS_DESCRIPTION . ".products_id and " . TABLE_PRODUCTS_DESCRIPTION . ".language_id = '" . (int)$languages_id . "' and " . TABLE_PRODUCTS_DESCRIPTION . ".products_name LIKE '%" . $_GET['term'] . "%' LIMIT 0,10");
if($fetch === FALSE) {
die(mysql_error());
}
while ($row = mysql_fetch_assoc($fetch))
{
array_push($return_arr, $row['products_name']);
}
print json_encode($return_arr);
This returns: mysql_fetch_assoc() parameter 1 resource, object given in ... on line 6
I get where it is, just can't seem to find the problem.
Thanks in advance
Use tep_db_fetch_array()
$return_arr = array();
$fetch = tep_db_query("select * from products, " . TABLE_PRODUCTS_DESCRIPTION . " WHERE products.products_status = '1' and products.products_id = " . TABLE_PRODUCTS_DESCRIPTION . ".products_id and " . TABLE_PRODUCTS_DESCRIPTION . ".language_id = '" . (int)$languages_id . "' and " . TABLE_PRODUCTS_DESCRIPTION . ".products_name LIKE '%" . $_GET['term'] . "%' LIMIT 0,10");
echo $fetch; die;
if($fetch === FALSE) {
die(mysql_error());
}
while ($row = tep_db_fetch_array($fetch))
{
array_push($return_arr, $row['products_name']);
}
print json_encode($return_arr);
string mysqli_real_escape_string ( mysqli $link , string $escapestr )
link- A link identifier returned by mysqli_connect() or mysqli_init()
escapestr- The string to be escaped.
You can find an example http://php.net/manual/en/mysqli.real-escape-string.php the first parameter needs to be the link identifier returned by mysqli_connect()