Accessing data from database in joomla - mysql

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

Related

Can't get a simple SELECT to work

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";

Cannot convert new database connection to older one

I need to convert this new database connectivity and retrive information from database to older fashion for my project. I have done the connection according to older fashion way PHP 4.0 but i need to use this PDO connectivity code to older database connectivity as i am not familiar with how to use and retrive information with using the older database connectivity.
Thank you.
<?php
$pdo = new PDO('mysql:host=localhost;dbname=sitepoint', 'root', '*****');
$opts = $_POST['filterOpts'];
$qMarks = str_repeat('?,', count($opts) - 1) . '?';
$statement = $pdo->prepare("SELECT mobile_phone.id, name, model, price FROM mobile_phone INNER JOIN brand ON brand_id = brand.id WHERE name IN ($qMarks)");
$statement -> execute($opts);
$results = $statement -> fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo($json);
?>
and i have tried this way with the older database connevtivity but its giving me nothing:
$opts = $_POST['filterOpts'];
$qMarks = str_repeat('?,', count($opts) - 1) . '?';
$statement = "SELECT mobile_phone.id, name, model, price FROM mobile_phone INNER JOIN brand ON brand_id = brand.id WHERE name IN ($qMarks)";
$statement1 = mysql_query($statement);
$results = mysql_fetch_assoc($statement1);
$json = json_encode($results);
echo($json);
mysql_query (and related) doesn't support bindings, so the final query is as follows
SELECT mobile_phone.id, name, model, price FROM mobile_phone INNER JOIN brand ON brand_id = brand.id WHERE name IN (?,?,?,?)
To be able of use dynamic parameters the same way with PDO, you should wrap each of them manually.
An approach could be as follows,
$opts = array();
foreach ($_POST['filterOpts'] as $filter) {
$opts[] = '\'' . mysql_real_escape_string($filter) . '\'';
}
$qMarks = implode(',', $opts);
$statement = "SELECT mobile_phone.id, name, model, price FROM mobile_phone INNER JOIN brand ON brand_id = brand.id WHERE name IN ($qMarks)";
$statement1 = mysql_query($statement);
$results = mysql_fetch_assoc($statement1);
$json = json_encode($results);
echo($json);

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
}

MySQL to PDO error

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

mysql unions with joomla

How do I do mysql UNIONS with joomla 2.5:
$query = parent::getListQuery();
$query
->select('*')
->from('#__game_types')
->where('published = 1')
->order('price ASC')
->union('ALL')
->select('*')
->from('#__dates')
I know union is not a method but I was just trying to show what I'd like to do.
according to my knowledge it's not in the Joomla doc too. So what you can do is execute the query the other way like shown in the example below.
$query = "
SELECT *
FROM . . . ";
$db->setQuery($query);
$row = $db->loadObjectList();