I have a dynamic table in my db, that columns are altered and deleted from other scripts. Because there is a chance that all those extra columns might hold null or empty values eventually i want to have a 'cleanup' action somewhere that checks for those cells, and if all are empty, to drop the whole row.
The ones in red are the ones i want to check. If both columns are null or empty WHERE customers_id = 1, then DELETE row.
Like i said this is just an example...There could be 5 or 10 columns after customers_id. I need to check them all if they are empty.
I can get all the names of those extra columns in a string like this:
$temparray = array();
$q = mydb_query("SHOW COLUMNS FROM $table WHERE field NOT REGEXP 'id|customers_id'");
while($row = mydb_fetch_array($q)){
$temparray[] = $row['Field'];
}
$forSQL = " '".implode("', '", $temparray)."' "; // gives 'client_custom_contact_person', 'client_custom_password'
$forPHP = implode(", ", $temparray); //gives client_custom_contact_person, client_custom_password
What is the fastest way to check if all those column values are empty to drop that row ?
Should i go with a php foreach function ? Or is there a faster mysql query i could do ?
-Thanks
Figured it out. Using the $forPHP string:
$q2 = mydb_query("SELECT $forPHP FROM $table WHERE customers_id = 1");
$res = mydb_fetch_assoc($q2);
if(!array_filter($res)) {
//If all are empty then
mydb_query("DELETE FROM $table WHERE customers_id = 1");
}
Related
code:
<?php
$this->db->select('*');
$this->db->from('bugs');
$where = "project_name = '".$project."'";
$this->db->where($where);
$sql = $this->db->get();
$res = $sql->num_rows();
if($res === 0)
{
$i = 1;
foreach ($bug as $row)
{
echo $i;
}
}
else
{
$i = 1;
foreach ($bug as $row)
{
echo ++$i;
}
}
?>
$data = array(
'project_name'=>$this->input->post('project'),
'bug_id'=>$this->input->post('bug_id'),
);
$query = $this->db->insert('bugs',$data);
In this code I have a table name bugs where I have a column i.e. bug_id. Now, I want to insert data into my table if table row having no value it insert bug_id = 1 and after that insert 2 and then 3 and so on. But now when I click on submit button it insert bug_id = 1 and then 2 but when I click on third it insert again bug_id 2 . So, How can I fix this problem?Please help me.
Thank You
MySQL has integrated AUTO_INCREMENT. Either you log into phpMyAdmin and change the properties of your bug_id field by clicking on edit, then selecting the "A_I" field.
Or you can do it with a SQL query:
ALTER TABLE YOUR_TABLE MODIFY bug_id INTEGER NOT NULL AUTO_INCREMENT;
If #Twinfriends answer doesn't fit your needs (but that should be the correct way to do that, you shouldn't reinvent the wheel) then maybe the problem is with your:
$data = array(
'project_name'=>$this->input->post('project'),
'bug_id'=>$this->input->post('bug_id'),
);
$query = $this->db->insert('bugs',$data);
Since you are actually using $this->input->post('bug_id') as your new id, maybe you want to use something else?
Where does your $bug var come from? To be sure of not getting duplicated ids or wrong one you could add to your MySql query something like "ORDER BY bug_id DESC LIMIT 1" (so that you take 1 row as result with the highest bug_id) and then just use that result + 1 as the new id
I have a table without an AI index. I had to trim data from a colum, and now I need to update with result from trim, but with the cod I have now, only updates with the same value, last value from that array.
$i=0;
while ($row = $column->fetch_assoc()) {
$arr[$i] = trim($row['profile_value'],"\"");
$db->query("UPDATE `vwfl5_user_profiles` SET profile_value='".$arr[$i]."'");
$i++;
}
Thanks!
You don't really need a loop for this. Just do it with one query.
$db->query("UPDATE `vwfl5_user_profiles` SET profile_value= TRIM( '\"' FROM profile_value)");
If you don't want to update every record, just add a WHERE clause with whatever criteria you used to get the results you are looping over.
$db->query("UPDATE `vwfl5_user_profiles`
SET profile_value = TRIM( '\"' FROM profile_value) WHERE... ");
2 things:
Shouldn't you update a specific row? Ie: "UPDATE `vwfl5_user_profiles` SET profile_value='".$arr[$i]."' WHERE id=$row[id]" (given there is an id and is integer)
I think you can do that with Trim Function
You should add WHERE clause with table primary key to identify each item in the db:
$i=0;
while ($row = $column->fetch_assoc()) {
$arr[$i] = trim($row['profile_value'],"\"");
$db->query("UPDATE `vwfl5_user_profiles` SET profile_value='".$arr[$i]."' WHERE id = " . $row['id'] . "");
$i++;
}
I'm using a select * query from a table.
$query = $this->db->get($table_name);
what I want is to for it to discard all records where the value for a column is empty so for example
Array
{
Topic_1 ->
Topic_2 -> Cats
}
It would discard Topic_1 key and value pair entirely. I'm also using Codeigniter active record so I tried doing
$query = $this->db->get_where($value, array(test_id != 'NULL'));
But i cannot specify each column, I just want test_id to be all columns in the table? Is there someone I can do this or can I just run a loop after the query is returned where it discards unmatched key value pairs?
First, you are doing NULL wrong. You should go like this:
$query = $this->db->where('test_id !=', NULL)->get($value)->result_array();
That query will work when field is NULL, but in your case it is not NULL, it is empty. In order for the field to be NULL you must specify it as default when you are creating table.
Query for your case would be:
$query = $this->db->where('test_id !=', '')->get($value)->result_array();
For all the fields, I guess you will need to go with foreach loop:
$data = array();
$field = array('field1', 'field2', ...);
foreach($field as $f) :
$query = $this->db->where($f, '')->get('table')->result_array();
$data = $data + $query;
endforeach;
This way in the $data you will get all the field that are empty.
I have test table which is
this is my table i am comparing string and getting the entity_id from this table
my sql query for one value search is like this
Select entity_id From test Where BINARY value = '".$my_search_list."'
this works fine if i search for single value like Shirt
i want to search for multiple values and when i try it with Comma Separated value
like Root,Appare,hand Bags (more than work) it is not giving me output
i Also tried with this
Select entity_id From test Where BINARY value IN ( '".$my_search_list."' )
i don't want multiple query i want to do it in single query is it possible???
what i was thinking as i said is to create an array of keywords and see which one match the criteria then show all result.
$search = array('Root','Appare','hand Bags');
$sql = "SELECT `entity_id` FROM `test` WHERE ";
$count = 0;
$search_size = count($search);
foreach($search as $key)
{
$count++;
if($count < $search_size)
{
$sql .= "(`value` = '".$key."') or ";
}
else if ($count == $search_size)
{
$sql .= "(`value` = '".$key."')";
}
}
is you echo $sql you will see the correct query wich should work:
SELECT `entity_id` FROM `test` WHERE (`value` = 'Root') or (`value` = 'Appare') or (`value` = 'hand Bags')`
you need to use quotes for values
SELECT entity_id FROM test WHERE value IN ('one','two','three')
I'm afraid this is going to sound redundant. I have done my diligence, and substituted at least three ideas I've found so far, and nothing works.
The goal is to merge unique fields from two tables, but right now I can't even get identical fields from one table into another that was created as an identical table. Here is the code so far, with my comments:
$result = mysql_query("SELECT * from $db.$tbl LIMIT 50");
if($result) {
while($maindata = mysql_fetch_assoc($result)) {
$newdata = implode ("," , $maindata);
$newdata = mysql_escape_string($newdata);
$pop = "INSERT INTO $db.address_new (id,entitynum,address,city,state,zip,country,remarks)
SELECT FROM $db.address";
//This is the original code replaced by the above
//$pop = "INSERT INTO $db.address_new (id,entitynum,address,city,state,zip,country,remarks)
// VALUES ($newdata)";
mysql_query($pop);
//print_r($pop);
//Seems to give the correct output in the browser, but the table address_new remains empty. `
Thank you in advance. I really appreciate your help.
To directly insert from another table (note: if ID is auto_increment you may or may not want to insert it this way):
INSERT INTO db.address_new (id,entitynum,address,city,state,zip,country,remarks)
SELECT (id,entitynum,address,city,state,zip,country,remarks)
FROM db.address LIMIT 50
(do not put this in a loop)
If you're looking for unique values, you can do that a couple of ways. Personally, I would have a unique key on one (or a set of) the values and then just do INSERT IGNORE:
INSERT IGNORE INTO db.address_new
(id,entitynum,address,city,state,zip,country,remarks)
SELECT (id,entitynum,address,city,state,zip,country,remarks)
FROM db.address LIMIT 50
As a side note:
// use mysql_real_escape_string
mysql_escape_string($newdata);
// since you're passing this through PHP, you need to make sure to quote
// all of your values. This probably means you'll need to loop and replace this
// $newdata = implode ("," , $maindata);
// with something like:
$newdata = array();
foreach( $maindata as $column )
{
$newdata[] = "'" . mysql_real_escape_string( $column ) . "'";
}
$newdata = implode(',', $newdata);
// you're missing the columns in your select clause.
$pop = "INSERT INTO $db.address_new ".
"(id,entitynum,address,city,state,zip,country,remarks) ".
// You need to select *something*
"SELECT FROM $db.address";