Query was empty SQL= - mysql

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.

Related

How to union query in codeigniter?

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.

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();

Why is my UPDATE Query not working?

I have the following update query but for some reason it's not working. I think it's something to do with the "id = '".$id."' but I've tried about three different ways and I cannot seem to get it to work. I've written update queries before with no problems but for some reason this one is being a pain. Thanks in advance.
$id = $_GET['id'];
$speaker = mysql_real_escape_string($_POST['speaker']);
$message = $_POST['message'];
$title = mysql_real_escape_string($_POST['title']);
$date = $_POST['date'];
$day = $_POST['day'];
$password = mysql_real_escape_string($_POST['password']);
$complete = $_POST['complete'];
$title = ucwords(strtolower($title));
if ($complete && ($password == "*****"))
{
$db = mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db($dbname,$db) or die(mysql_error());
mysql_query("UPDATE sermons SET speaker = '$speaker', message = '$message', title = '$title', date = '$date', day = '$day' WHERE id = '$id'");
$num_rows = mysql_num_rows(mysql_query("SELECT speaker, message, title, date, day FROM sermons WHERE speaker = '$speaker' AND message = '$message' AND title = '$title' AND date = '$date' AND day = '$day'", $db));
if ($num_rows == 1)
echo "<script type='text/javascript'> alert('Sermon Information Entered Successfully!'); </script>";
else
echo "<script type='text/javascript'> alert('Error! Please Try Again.'); </script>";
}
else if ($complete && ($password != "*****"))
{
echo "<script type='text/javascript'> alert('Incorrect Password! Please Try Again.'); </script>";
}
Because of id is Integer so this is correct code try thus :
mysql_query("UPDATE sermons SET speaker = '$speaker', message = '$message', title = '$title', date = '$date', day = '$day' WHERE id =".$id);
I expected it will be works.
Writing this as an answer as providing code in comments is difficult to read
Try changing
mysql_query("UPDATE sermons SET speaker = '$speaker', message = '$message', title = '$title', date = '$date', day = '$day' WHERE id = '$id'");
To
mysql_query("UPDATE sermons SET speaker = '$speaker', message = '$message', title = '$title', date = '$date', day = '$day' WHERE id = '$id'") or die(mysql_error());
Then add the error message to your question please.

mysql query convert to mysqli

I am using the below query with the server setting as mysql in Joomla quite successfully, but can't get it run using the settings as mysqli in joomla Fabrik extension.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$sql = mysql_query("SELECT first_name,mobile FROM students WHERE
DAY(dob) = DAY(CURDATE()) AND
MONTH(dob) = MONTH(CURDATE())");
while($row = mysql_fetch_array($sql)) {
//mycode- to - send SMS
}
I have modified the above code for mysqli as below with no success:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$sql = mysqli_query("SELECT first_name,mobile FROM students WHERE
DAY(dob) = DAY(CURDATE()) AND
MONTH(dob) = MONTH(CURDATE())");
while($row = mysqli_fetch_array($sql)) {
//mycode- to - send SMS
}
What other changes I need to make in the above query so that I can use it using mysqli settings.
Thanks.
Joomla has it own database class so you don't need to use the likes of mysqli_*. Use something along the lines of the following:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('first_name', 'mobile')));
->from($db->quoteName('#__students'))
->where($db->quoteName('DAY(dob) = DAY(CURDATE())' AND 'MONTH(dob) = MONTH(CURDATE())'));
$db->setQuery($query);
$results = $db->loadObjectList();
foreach ($results as $result) {
your code here
}
For more information in Joomla database queries, read this:
http://docs.joomla.org/Selecting_data_using_JDatabase
I have modified the query partly according to the suggestion by Lodder. My final query looks like this:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query = "SELECT SUBSTRING_INDEX(name,' ',1)first_name,mobile FROM students WHERE
DAY(dob) = DAY(CURDATE()) AND
MONTH(dob) = MONTH(CURDATE())";
$db->setQuery($query);
$results = $db->loadObjectList();
foreach ($results as $result) {
$mobile = $result->mobile;
$name = $result->first_name;
//further code
}