I am unsur why the below statement is not working basically in a nutshell I am wanting it to run the $result statement if only the $product_id is not found in the $images table. Is it is found I would like it to then run the inner statement.
Both statements do work via phpMyAdmin and the $result statement works when just using $this->db->query
Code:
public function product_delete($product_id)
{
$table = $this->_table_products;
$images = $this->_table_product_images;
$result = $this->db->query("SELECT `id` FROM $table WHERE $table.id ='$product_id'");
if(mysqli_num_rows($result) !== 0)
{
$this->db->query("DELETE FROM $table WHERE $table.id = '$product_id'");
}else{
$this->db->query("DELETE FROM $table INNER JOIN $images ON $table.id = $images.product_id WHERE $images.id = $product_id");
}
}
You have to use {} around variable name in query. Also use != instead of !==
$result = $this->db->query("SELECT `id` FROM {$table} WHERE {$table}.id ='$product_id'");
if(mysqli_num_rows($result) != 0)
{
$this->db->query("DELETE FROM {$table} WHERE {$table}.id = '$product_id'");
}else{
$this->db->query("DELETE FROM {$table} INNER JOIN {$images} ON {$table}.id = {$table}.product_id WHERE {$images}.id = $product_id");
}
Change !== to !=
if(mysqli_num_rows($result) != 0)
{
$this->db->query("DELETE FROM $table WHERE $table.id = '$product_id'");
}
else
{
$this->db->query("DELETE FROM $table INNER JOIN $images ON $table.id = $images.product_id WHERE $images.id = $product_id");
}
If you want to check the images table, you should query the table ${images}, not ${table}. Also, if you are only interested in finding out how many matching rows there are, it's best to use the COUNT() function in MySQL. That way you always get one row instead of potentially 100,000s. Using the function mysqli_num_rows() has the disadvantage that you're loosing the flexibility that the CodeIgniter database class introduces.
So your code should be something like
$result = $this->db->query("SELECT COUNT(*) `cnt` FROM ${images} WHERE ${images}.product_id ='$product_id'");
$row = $result->row();
if($row['cnt'] != 0) {
// found something
If the variable name is unclear in a string, you can use brackets to tell PHP what you want. "${foo}bar" means that the variable is $foo and bar is just some string to append to the variable content. It also helps to improve readability. And I changed !== to != because I'm not familiar enough with CI and I do not know if the value will be an integer or the string representation of an integer.
Related
I am new to CI/PHP/development. Im stuck on this problem. It seems to be really basic but i just cant get it right!
I am able to returning only last value while sql excecution .
controller
$data['leadd']= $this->dashboard_model->countt($this->session->userdata('user_id'));
foreach ($data['leadd'] as $q) { $date = $q['followup_date'];
$data['lead']= $this->dashboard_model->lead_count($this->session->userdata('user_id'),$date); }
$data['count'] = count($data['lead']);
Model
public function countt($userid)
{
$sql = $this->db->query("SELECT followup_date FROM tg_partner_data WHERE assign_to = '$userid' AND active = 'Y'");
return $sql->result_array();
}
public function lead_count($userid,$date)
{
$sql1 = $this->db->query("SELECT * FROM tg_partner_data
WHERE assign_to = '$userid' AND followup_date = '$date'
AND active='Y'");
//echo $this->db->last_query();
return $sql1->result_array();
}
First you select all followup dates from one table by executing this: "SELECT followup_date FROM tg_partner_data WHERE assign_to = '$userid' AND active = 'Y'".
Then for each followup_date, you execute another query but you don't merge prevoius results just overwrites them in foreach loop.
Foreach loop should look like this:
$data['lead'] = array();
foreach ($data['leadd'] as $q) {
$date = $q['followup_date'];
$data['lead'][] = $this->dashboard_model->lead_count($this->session->userdata('user_id'),$date));
}
You can also rewrite SQL query:
SELECT COUNT(*) FROM tg_partner_data WHERE assign_to = '$userid' AND followup_date = '$date' AND active='Y'"
It will allow you to get number of rows directly from DB.
How to get count of the sql search query
$u = new user();
$sql = "SELECT a.id FROM `accounts` AS a LEFT JOIN `users` AS u ON a.id = u.id WHERE a.id IS NOT NULL ";
$gender = $this->input->post('gender');
if($gender != NULL)
{
$sql .= "AND u.gender = '".$gender."' ";
}
$u->query($sql);
How to get count of the query results in $u->query($sql); .I need to set a validation on it. If the query results count is 0 , i need to set a message. Im using PHP Codeigniter ,datamapper library.
Thank you!!!
Just using count() function like this
...
$result = $u->query($sql);
$total_data = count($result);
if($u->exists())
{
echo "Found" // Do something
}
else
{
echo "Nothing found" //Do something
}
$result = $this->db->get();
For Codeigniter use $count = $result->num_rows(); // HERE IS YOUR COUNT
For OOP PHP use $count = $result->num_rows;
I am working on a query that is submitted from a form with a variety of search options that could or could not be included in my query. Is there a better way to do this dynamically as this doesn't seem to work?
I keep getting error Fatal error: Call to a member function fetchAll() on boolean. I know there is a lot going on here and I didn't want to put a ton of code to read through. So to make a long story short, when I echo out $sql my select statement looks good, but no matter what I do I keep getting the same error which tells me there is something wrong with the statement. Is this approach even workable or do I need to use another method? If so what is a better approach?
$sql = "SELECT DISTINCT mt.id, mt.name, mt.phone
FROM main_table mt
LEFT JOIN jnct_tbl_srvstate st ON mt.id = st.mt_id
LEFT JOIN jnct_tbl_equip eq on mt.id = eq.mt_id
LEFT JOIN jnct_tbl_accessory ac ON mt.id = ac.mt_id
LEFT JOIN tbl_car c ON mt.id = c.mt_id
WHERE mt.id = c.id";
if($state_array_count != 0){
$sql.= " AND srvstate_id IN ($st_holders)";
}
if($equip_array_count != 0){
$sql.= " AND equip_id IN ($eq_holders)";
}
if($accessory_array_count != 0){
$sql.= " AND accessory_id IN ($ac_holders)";
$sql.= " ORDER BY mt.id";
$query = $db->prepare($sql);
Incorporating the if statements above is what I really need help with.
if($state_array_count != 0){
foreach($state_array as $st_placeholder => $st_value) {
if($st_value != '') {
$st_placeholder = ":$st_placeholder";
$query->bindValue($st_placeholder, $st_value);
}
}
}
if($equip_array_count != 0){
if($eq_value != '') {
foreach($equip_array as $eq_placeholder => $eq_value) {
$eq_placeholder = ":$eq_placeholder";
$query->bindValue($eq_placeholder, $eq_value);
}
}
}
if($accessory_array_count != 0){
foreach($accessory_array as $ac_placeholder => $ac_value) {
if($ac_value != '') {
$ac_placeholder = ":$ac_placeholder";
$query->bindValue($ac_placeholder, $ac_value);
}
}
}
$results = $query->execute();
$rs = $results->fetchAll(PDO::FETCH_ASSOC);
The execute() method does not return a results object. It returns a boolean, true or false.
fetchAll() is a method of the PDOStatement object. Here's an example:
$sth = $dbh->prepare($sql);
$sth->execute();
$result = $sth->fetchAll();
i have a project to build a CMS. We were giving a few functions.. on is below. I would like someone to explain to me what the 'p', 'ca', and 'cm' mean/stand for?
function getAllPosts() {
global $db;
$sql = "SELECT p.*, ca.catName, COUNT(commentID) as numComments
FROM posts p
LEFT JOIN categorys ca USING (categoryID)
LEFT JOIN comments cm USING (postID)
";
if ($_GET['catID'] != ''){
$catID = (int)$_GET['catID'];
$sql .= "WHERE categoryID = $catID ";
}
$sql .= "GROUP BY postID
ORDER BY postDate DESC";
$qry = mysqli_query($db, $sql);
$result = array();
while ($row = mysqli_fetch_assoc($qry)) {
$result[] = $row;
}
if (count($result) > 0) {
return $result;
}
return false;
}
They are a shorter way to write a query without using the whole table names. (table alias)
E.g. SELECT a.name, a.surname FROM very_long_name_of_my_table [AS] a
instead of
SELECT very_long_name_of_my_table.name, very_long_name_of_my_table.surname FROM very_long_name_of_my_table
SQL aliases are used to give a database table, or a column in a table, a temporary name.
Basically aliases are created to make column names more readable.
for more visit: http://www.w3schools.com/sql/sql_alias.asp
Hi I am trying to use in_array, I think my syntax is correct,
but it says "Wrong datatype for second argument"
My code is
$result = mysqli_query($con, "SELECT * FROM Products WHERE Quantity_On_Hand < Min_Stock");
$filter = mysqli_query($con, "SELECT ProductID FROM Orders");
while($row = mysqli_fetch_array($result))
{
if (in_array($row['ProductID'], $filter))
{
}
}
My idea is to find out if the ProductID from Products Table is in the Order Table.
Could someone helps me, Thanks :-)
$filter isn't an array; it's a mysqli_result object:
$filter = mysqli_query($con, "SELECT ProductID FROM Orders");
I think you want to iterate over that, add each ProductID to a new array, and then pass that array to the in_array function like so:
$filter = mysqli_query($con, "SELECT ProductID FROM Orders");
$product_ids = array();
while ($row = $filter->fetch_assoc())
{
$product_ids[] = $row['ProductID'];
}
$result = mysqli_query($con, "SELECT * FROM Products WHERE Quantity_On_Hand < Min_Stock");
while($row = mysqli_fetch_array($result))
{
if (in_array($row['ProductID'], $product_ids))
{
}
}
Your code is failing because $filter is a MySQLi result resource, not an array. Really, this is better accomplished with a simple inner join between the two tables. If a ProductID does not exist in Orders, the INNER JOIN will exclude it from the result set in the first place.
$sql = "
SELECT Products.*
FROM
Products
INNER JOIN Orders ON Products.ProductID = Orders.ProductID
WHERE Quantity_on_Hand < Min_stock";
$result = mysqli_query($con, $sql);
if ($result) {
$results = array();
while ($row = mysqli_fetch_array($result)) {
$results[] = $row;
}
}
// Now $results is a 2D array of all your Products
If instead you want to retrieve all the Products, and simply have an indication of whether it has an active order, use a LEFT JOIN and test if Orders.ProductID is null in the SELECT list:
$sql = "
SELECT
Products.* ,
/* No orders will print 'no-orders' in a pseudo column called has_orders */
CASE WHEN Orders.ProductID IS NULL THEN 'no-orders' ELSE 'has-orders' AS has_orders
FROM
Products
LEFT JOIN Orders ON Products.ProductID = Orders.ProductID
WHERE Quantity_on_Hand < Min_stock";
$result = mysqli_query($con, $sql);
if ($result) {
$results = array();
while ($row = mysqli_fetch_array($result)) {
$results[] = $row;
}
}
// Now $results is a 2D array of all your Products
// And the column $row['has_orders'] will tell you if it has any...
In this case, you may test in a loop over your rowset whether it has orders:
foreach ($results as $r) {
if ($r['has_orders'] == 'has-orders') {
// this has orders
}
else {
// it doesn't...
}
}