How to union query in codeigniter? - mysql

I try to use this
$this->db->select('title, content, date');
$this->db->from('mytable');
$query = $this->db->get();
$subQuery1 = $this->db->_compile_select();
$this->db->_reset_select();
$this->db->select('title, content, date');
$this->db->from('mytable2');
$query = $this->db->get();
$subQuery2 = $this->db->_compile_select();
$this->db->_reset_select();
$sql = ("$subQuery1 UNION $subQuery2");
$rowsdata = $this->db->query($sql)->result_array();
print_r($rowsdata);
die();
when I use print_r(); to see array result but nothing happened.

Related

Wordpress Backend query with vars

I am getting the postID and successfully dumping it with the following value:
"string(18) "526"". When i put the var $postid in my query it does not give the same result. Code:
$postid = $post->ID;
$pdf = $wpdb->get_results("SELECT $wpdb->posts.guid FROM $wpdb->posts WHERE $wpdb->posts.post_parent = $postid");
Any ideas why this occurs?
Greetings and a happy new year
Try this:
global $post;
global $wpdb;
$postid = $post->ID;
$query = $wpdb->prepare('SELECT $wpdb->posts.guid FROM $wpdb->posts WHERE $wpdb->posts.post_parent = '$postid'');
$pdf = $wpdb->get_results($query);
Hope that works.
$text ="SELECT $wpdb->posts.guid FROM $wpdb->posts WHERE $wpdb->posts.post_parent = %d";
$query = $wpdb->prepare($text, $postid);
$pdf = $wpdb->get_results($query);
It should work with this synthax.

Merge arrays in php

In the above code in each foreach loop i get a seperate array every time . I wanna combine all these array . I have tried array_merge but it is not working . Is there any other way?
Some of the results are two dimentional array
foreach ($location as $loc)
{
$query = "select name
from locations l where l.location_id = $loc";
$query = $this->db->query($query);
$data = $query->result_array();
}
Thanks in advance.
Either use array_merge():
$data = array();
foreach ($location as $loc) {
$query = "SELECT name FROM locations l WHERE l.location_id = " . $loc;
$query = $this->db->query($query);
$data = array_merge($data, $query->result_array());
}
...or merge the location id's first and then do a single query, which is much faster:
$in = implode(', ', $location);
$query = "SELECT name FROM locations l WHERE l.location_id IN (" . $in . ")";
$query = $this->db->query($query);
$data = $query->result_array();
$q = 'SELECT `name` FROM `locations` AS `l` WHERE ';
foreach ($location as $loc) {
$q .= "`l`.`location_id` = '$loc' OR ";
}
$q = rtrim($q, 'OR ');
$query = $this->db->query($q);
$data = $query->result_array();

Query was empty SQL=

I have this error and I dont know why.
I have 3 similar requests and the second one doesnt work.
Im using Joomla 3.
$url = $json_data['url'];
$main_dir_uri = 'site/'. $mob_url;
$mob_url = 'http://'. $_SERVER['HTTP_HOST'] . $main_dir_uri;
$publish = true;
$call_btn = true;
$multilang = false;
$sinchronization = mysql_real_escape_string('23:00:00');
$columns1 = array('datecreate', 'dateedit');
$columns2 = array('siteurl', 'mobsiteurl', 'uridir', 'publish', 'multilang', 'callbtn', 'sinchronization');
$columns3 = array('idusers', 'datecreate', 'dateedit');
$values1 = array("NOW()", "NOW()");
$values2 = array($url, $mob_url, $main_dir_uri, $publish, $multilang, $call_btn, $sinchronization);
$values3 = array($user_id, "NOW()", "NOW()");
$db = JFactory::getDBO();
$query1 = $db->getQuery(true);
$query1
->insert($db->quoteName('#__sites'))
->columns($db->quoteName($columns1))
->values(implode(',', $values1));
$db->setQuery($query1);
$db->query();
$query2 = $db->getQuery(true);
$query2
->insert($db->quoteName('#__sites_data'))
->columns($db->quoteName($columns2))
->values(implode(',', $values2));
$query2 = $db->getQuery(true);
$db->setQuery($query2);
$db->query();
$query3 = $db->getQuery(true);
$query3
->insert($db->quoteName('#__sites_users'))
->columns($db->quoteName($columns3))
->values(implode(',', $values3));
$db->setQuery($query3);
$db->query();
if($db->getErrorMsg()) {
print_r($db->getErrorMsg());
}
I've checked all values, they are ok. What's the problem can be?
You have an extra $query2 = $db->getQuery(true); that is wiping out $query2.

INSERT in a WHILE LOOP Not Inserting into Database

I have a SELECT statement, WHILE statement and an INSERT:
$result = mysqli_query($con,"SELECT winner, time, course, market, twitter_pubstatus
FROM combo
WHERE twitter_pubstatus = 0 AND market = '$win' GROUP BY winner");
while($row = mysqli_fetch_array($result))
{
$winner = $row['winner'];
$time = $row['time'];
$course = $row['course'];
$message = "$winner won the $time at $course. You are a winner! #GetIn";
$query = "INSERT INTO messageTable (MESSAGE) VALUES($message)or die(mysql_error())";
}
It runs through with no errors. There should be 12 rows that get inserted into the database. What am I doing wrong?
Try changing $query = "INSERT INTO messageTable (MESSAGE) VALUES($message)or die(mysql_error())";
to
$query = "INSERT INTO messageTable (MESSAGE) VALUES('$message')or die(mysql_error())";
Notice the single quotes in '$message'
And $query is just a string so execute the query
$result=mysqli_query($query)
And then check if query executed by doing this
if(!$result) die(mysqli_error());

MySql LIKE returns false if search term is same as entire string in the column, why is that?

So I have following as part of my query
SELECT * FROM $table WHERE columname LIKE '%$searchterm%'
I have tried taking out leading and/or ending wildcards meaning
SELECT * FROM $table WHERE columname LIKE '$searchterm%'
AND
SELECT * FROM $table WHERE columname LIKE '%$searchterm'
AND
SELECT * FROM $table WHERE columname LIKE '%$searchterm%' OR columname LIKE '$searchterm'
and also tried adding following to the query with no luck
OR columname = '$searchterm'
So when my search term is "myval" and if column has whole string "myval", I would like to have that selected. But ALL of my queries above, return false/return nothing where myval is searchterm and column value as full.
I can not use MATCH because this is not Full-Text index.
EDIT:
PHP Code:
$sterm = NULL;
$table = 'mytable';
if(isset($_GET['s'])) { $sterm = explode(" ", mysql_real_escape_string($_GET['s'])); }
if(isset($_POST['s'])) { $sterm = explode(" ", mysql_real_escape_string($_POST['s'])); }
if(!empty($sterm)){
$getdata = "SELECT * FROM $table WHERE termsi != 'Special' ";
foreach ($sterm as $value){
$getdata .= "AND netid_all LIKE '%$value%' OR netid_all = '$value' ";
} //End foreach
$getdata .= "LIMIT 10";
$result = mysql_query($getdata) or die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo <<<PRINTALL
{$row[0]}, {$row[1]}, {$row[2]}, {$row[3]}, {$row[4]}, {$row[5]}, {$row[6]}, {$row[7]}, ' <br />'
PRINTALL;
} //End While
} //End If search exists
Okay So As you guys suggested, i tried PHPMyAdmin sql console and it works fine, so it would have to be by PHP!? so here it is.
I'd suggest writing your query building like this:
$fullvalues = array();
$partials = array();
foreach ($sterm as $value){
$partials[] = "(netid_all LIKE '%" . mysql_real_escape_string($value) . "%')";
$fullvalues[] = "'" . mysql_real_escape_string($value) . "'";
}
$partials = implode(' OR ', $partials);
$fullvalues = implode(', ', $fullvalues);
$sql = <<<EOL
SELECT *
FROM $table
WHERE (termsi != 'Special')
AND (($partials) OR (netid_all IN ($fullvalues));
EOL;
Assuming your search string is a b c, you'd get this query:
SELECT *
FROM yourtable
WHERE (termsi != 'Special')
AND (((netid_all LIKE '%a%') OR (netid_all LIKE '%b%') OR (netid_all LIKE '%C%')) OR (netid_all IN ('a', 'b', 'c')))
If your search requires that all terms be present, then change the 'OR' to 'AND' in the implode.
Well found it,
$row = mysql_fetch_array($result, MYSQL_ASSOC);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
Was the problem, earlier when I was testing things, anyhow, it should have been the following
$row = mysql_fetch_array($result, MYSQL_ASSOC);
while($row)