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()
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|
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?
I created the following SQL Query, which works fine. However, I want to add 2 other checkbox $Post's from another label. Now I'm a little bit without any idea how I can add it. Do I have to add an "AND" between the tbs queries?
Here is the code
include "db_connect.inc.php";
$sql = "SELECT * FROM profiles";
$sql .= " WHERE profilename = '". $_POST["profilename"] ."' ";
$sql .= " AND ort = '". $_POST["ort"] ."' ";
$sql .= "AND jahren = '" . $_POST["alter"] . "' ";
$tbs = array();
foreach( array( 'tb1', 'tb2', 'tb3' ) as $tb_key )
{
if ( empty( $_POST[$tb_key] ) ) continue;
$tbs[] = "`grosse` LIKE '" . $_POST[$tb_key] . "'";
}
if ( !empty( $tbs ) )
{
$sql .= ' AND ( ' . implode( ' OR ', $tbs ) . ' )';
}
$tbs = array();
foreach( array( 'tb4', 'tb5', 'tb6', 'tb7' ) as $tb_key )
{
if ( empty( $_POST[$tb_key] ) ) continue;
$tbs[] = "`haare` LIKE '" . $_POST[$tb_key] . "'";
}
if ( !empty( $tbs ) )
{
$sql .= ' AND ( ' . implode( ' OR ', $tbs ) . ' )';
}
$res = mysqli_query($con, $sql);
$num = mysqli_num_rows($res);
if ($num==0) echo "Kein Profil gefunden";
echo "<table border='1'>";
echo "<tr><td>Profile</td><td>Alter</td>";
echo "<td>Ort</td><td>Brustgrösse</td>";
echo "<td>Haarfarbe</td></tr>";
while ($dsatz = mysqli_fetch_assoc($res))
{
echo "<tr>";
echo "<td>" . $dsatz["profilename"] . "</td>";
echo "<td>" .$dsatz["jahren"] . "</td>";
echo "<td>" .$dsatz["ort"] . "</td>";
echo "<td>" .$dsatz["grosse"] . "</td>";
echo "<td>" .$dsatz["haare"] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
i could it resolve it. I had a wrong entry in the DB. The query works fine!
Im trying to rewrite mysql_ into mysqli_, but got 2 errors
mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
and
mysqli_num_rows() expects parameter 1 to be mysqli_result,
why? Ive fixed mysql_query("SELECT...) into mysqli_query($db, "SELECT..) and all others
<?php
ob_start();
session_start();
include('config/configuration.php');
if($_POST['Login'])
{
$UserName=$_POST['username'];
$Password=md5($_POST['password']);
$UserQuery=mysqli_query($db, "SELECT Id, UserName, FirstName, LastName, Level FROM users WHERE UserName='$UserName' AND Password='$Password' AND IsActive=1 and level >= 3");
$UserDetails=mysqli_fetch_array($UserQuery);
if(mysqli_num_rows($UserQuery))
{
$_SESSION['UserName'] = $UserDetails['UserName'] . ' (' . $UserDetails['FirstName'] . ' ' . $UserDetails['LastName'] . ')';
$_SESSION['UserId'] = $UserDetails['Id'];
$_SESSION['Level'] = $UserDetails['Level'];
mysqli_query("UPDATE users SET NumberOfLogin = NumberOfLogin + 1, LastLoginDate = NOW() WHERE Id = " . $_SESSION['UserId'] . " ");
Your query is failing.
Try this to see the issue:
if (!$UserQuery) {
echo "MySQLi Error: " . mysqli_error($con);
die();
}