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.
Related
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.
Hi gus this is my first question in this coumunity i hope someone answer me
when i want to search from array of mysql_fetch_arrray he give me on error
this is the code
$key = "Meca";
$query = "SELECT * FROM subject WHERE `name` LIKE '%$key%' Or `sale` LIKE '%$key%' Or `Emphet` LIKE '%$key%' ";
$sql = mysql_query($query);
$num = mysql_num_rows($sql);
while ($num >0)
{
$row = mysql_fetch_object($sql);
$num--;
$row = mysql_fetch_object($sql);
in_array($row , "Meca");
}`
You are not clear with your question;
Anyway I give a try:
$key = "Meca";
$query = "SELECT * FROM subject WHERE name LIKE '%$key%' Or sale LIKE '%$key%' Or Emphet LIKE '%$key%'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
if(in_array($row , "Meca")){
echo "$key is present this time."
}
}
Note that:
I am not on your requirement. So I just make a rough sketch.
You have not provided your error message.
You may have error in your code(SQl or PHP)[like usage of names
'Emphet'].
Anyway give a try.
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.
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
}
I have the following sql code:
$statement = "`maildb`";
$query = "SELECT COUNT(*) as `num` FROM {$statement}";
$row = mysql_fetch_array(mysql_query($query));
I am trying to transform to PDO... I have tried the following:
$statement = "`maildb`";
$query = $db->prepare("SELECT COUNT(*) as `num` FROM {$statement}");
$query->execute();
$row = $query->fetchAll(PDO::FETCH_ASSOC);
That results in: Fatal error: Call to a member function prepare() on a non-object in function.php on line 8.
I have also tried this before, which should do the same:
$query = $db->prepare("SELECT * FROM maildb");
$query->execute();
$row = $query->fetchAll(PDO::FETCH_ASSOC);
$num = count($row);
Any help?
Thank you!!
You are using $db which is not initialized. Please initialize it first by doing $db = new PDO( <connection details here> ). See PDO manual on PHP.net for more details.
You probably didn't instantiate $db. Therefore, it's not an object, thus you can't call a function from it
I think the error lies in this line:
$query = $db->prepare("SELECT COUNT(*) as `num` FROM {$statement}");
Instead try using:
$query = $db->prepare("SELECT COUNT(*) as `num` FROM ?");
$query->execute($statement);