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
}
Related
I have done this type of SELECT many times, but this time I can't get it to work. Any ideas, please?
$Name = "Dick";
$conn = mysqli_connect($server, $dbname, $dbpw, $dbuser);
$sql = "SELECT id FROM table WHERE $Name = table.first_name";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$customer_id = $row['id'];
Database::disconnect();
echo "customer id = " . $customer_id;
If you really DO have a table named table it would be more appropriate to use back ticks around the name since the word TABLE is a reserved word in MySQL. You should also use single quotes around your variable if it contains a string:
$sql = "SELECT `id` FROM `table` WHERE `first_name` = '$Name'";
Other possible reasons if the query still doesn't work for you:
Make sure you have the connection parameters in the right order. It should be: mysqli_connect($server, $dbuser, $dbpw, $dbname).
You should be using fetch_array() instead of fetch_assoc() if you expect a one row result.
You are mixing PROCEDURAL STYLE with Object Oriented Style when using mysqli_connect() instead of mysqli(), at the same time using $result-> which is object oriented style. You should decide one style and stick with it.
This would be the procedural style of your query:
$Name = "Dick";
$conn = mysqli_connect($server, $dbuser, $dbpw, $dbname); // NOTE THE CHANGED ORDER OF CONNECTION PARAMETERS!
$sql = "SELECT `id` FROM `table` WHERE `first_name` = '$Name'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$customer_id = $row['id']; // YOUR CUSTOMER ID
mysqli_free_result($result); // FREE RESULT SET
mysqli_close($conn); // CLOSE CONNECTION
And this would be the object oriented style:
$Name = "Dick";
$conn = new mysqli($server, $dbuser, $dbpw, $dbname);
$sql = "SELECT `id` FROM `table` WHERE `first_name` = '$Name'";
$result = $conn->query($sql);
$row = $result->fetch_array(MYSQLI_ASSOC);
$customer_id = $row['id']; // YOUR CUSTOMER ID
$result->free(); // FREE RESULT SET
$conn->close(); // CLOSE CONNECTION
I would recommend naming your table something else than table since it's a reserved word and could get you into parsing problems. The same goes with field names. More reading: https://dev.mysql.com/doc/refman/5.5/en/keywords.html
More about mysqli_fetch_array() and differences in procedural style and object oriented style use: http://php.net/manual/en/mysqli-result.fetch-array.php
$sql = "SELECT id FROM table WHERE '$Name' = table.first_name";
You simply need to concat the variable like this:
$sql = "SELECT id FROM table WHERE " . $Name . " = table.first_name";
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.
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 this query in Fabrik (Joomla Application) to pull the data from database, which is not working.
The same query with mysql syntax is working fine in PHPMYADMIN.
$db = FabrikWorker::getDbo(false, 2);
$query = $db->getQuery(true);
$query
->select('hostel_fee')
->from('hostel_fee AS a')
->join('INNER','students AS b ON (b.class = a.class)');
$db->setQuery($query);
$a = $db->loadResult();
return $a;
use the full queries like this
$db = &JFactory::getDBO();
$query = "SELECT m.id, m.title,m.level,mt.menutype FROM #__menu AS m
INNER JOIN #__menu_types AS mt ON mt.menutype = m.menutype
WHERE mt.menutype = m.menutype AND m.published = '1' ORDER BY mt.menutype,m.level";
$db->setQuery($query);
$rows = $db->loadObjectList();
OR
$rows = $db->loadResult();
Instead of using this
$db = FabrikWorker::getDbo(false, 2);
Use this.
$db =& JFactory::getDBO();
Or if you want to use any external database to connect to your extension you can use this
Connecting to an external database