inner join query in joomla - php

I want to fire one complex query in my joomla site. I have written below code for it.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('`chr`.`characteristic_id`,`chr`.`characteristic_value`,`prc`.`price_value`');
$query->from('`#___hikashop_product` AS `pdt`');
$query->join('inner', '`#__hikashop_variant` AS `vari` ON `pdt`.`product_id` = `vari`.`variant_characteristic_id`');
$query->join('inner', '`#__hikashop_characteristic` AS `chr` ON `vari`.`variant_characteristic_id` = `chr`.`characteristic_id`');
$query->join('inner', '`#__hikashop_price` AS `prc` ON `pdt`.`product_id` = `prc`.`price_product_id`');
$query->where('`pdt`.`product_id` = 68');
$db->setQuery($query);
Query is executing in my local mysql.
any help will be appreciated

You can try this
$db->setQuery("Your query");
$result = $db->query();
//if you need the count
$rowcount = $db->getNumRows();
//if the result is multiple rows
$result_array = $db->loadAssocList() or $db->loadObjectList();
//if the result is single row you can use
$result = $db->loadAssoc() or $db->loadObject();

To fire the query, all you need to do is:
$rows = $db->loadAssocList(); // or loadObjectList()
The above will put all of the rows into $rows
You can also fire the query without grabbing the rows with:
$db->query();

Related

PHP PDO count and sum value from MySQL table

Well, I'm pretty sure this is just a novice question, so please forgive me for that, but I feel like I'm losing my mind.
I have a simple MySQL rating table and I need to count rows and to sum rates values (int) with PHP PDO
$sql = "SELECT rate FROM rating_table";
$query = $db->query($sql);
$rate_times = count($query->fetchAll()); // it works!
echo '<p>'.$rate_times.'</p>';
$sum_rates = array_sum($query->fetchAll()); // it doesn't work!
echo '<p>'.$sum_rates.'</p>';
Thank you in advance for any suggestion
If I understand you right, all you have to do is to modify your sql request, this will return a single row
sql = "SELECT sum(rate) as rate_sum, count(*) as record_count FROM rating_table";
$query = $db->query($sql);
$row = $query->fetch(PDO::FETCH_ASSOC);
if ($row) {
$sum = $row['rate_sum'];
$count = $row['record_count'];
}

How do I count the number of rows in a MySQL Table?

I know that inside MySQL, you can use:
SELECT COUNT(*) FROM table
I have written the following code in PHP to display the number of rows on the page:
$sql = 'select * from users';
$data = $conn -> query($sql);
echo $data;
But when I run it, I get the following error:
Catchable fatal error: Object of class PDOStatement could not be converted to string in [Directory] on line 19.
I think the problem is that the returned value is not in string form. If that is correct, how would I be able to display the number of rows on the page?
If you want to count the rows you can do this with PDO:
$sql = 'select * from users';
$data = $conn->query($sql);
$rows = $data->fetchAll();
$num_rows = count($rows);
Well, you arent badly off, you are almost there:
$sql = 'SELECT COUNT(*) as numrow FROM users';
$data = $conn -> query($sql);
rows = $data->fetchAll();
Depending on the type of return data, you could use
$rows->numrow if the return data is an object
There are some easy and faster way to do this
COUNT the column in query and fetch
$sql = "SELECT COUNT(id) AS total_row FROM table_name";
$stmt = $conn->query($sql);
$stmt->execute();
echo $count['total_row'];
Using rowCount()
$sql = "SELECT * FROM table_name";
$stmt = $conn->query($sql);
$stmt->execute();
$count = $stmt->rowCount();
echo $count;

Joomla doesn't work with PDO connection

Hey guys I need your help I have this code in a localhost file :
$db = new PDO('mysql:host=localhost;dbname=db;charset=utf8', 'user', 'pass');
$data = array();
$results = $db->query("SELECT * from YOUR_TABLE_NAME");
while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
$data[$row['prov_name']][] = $row;
}
But when I try to integrate this file in Joomla it doesn't work! Do you know how can I "transform" this into Joomla connection?
I always use this one for some select
$db =& JFactory::getDBO();
$query = 'SELECT CA_id FROM compras_activos where STAT_name = "Solicitado"';
$db->setQuery($query);
$result = $db->loadObjectList();
$CA_id = $result[0];
But that is just for one specific value but now I need all(*) the table.
Thanks
You should really read the documentation that I provided. If you are able to write a query as shown in your question, then this should not be too taxing. You can use the following:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('*'))
->from($db->quoteName('#__compras_activos'))
->where($db->quoteName('STAT_name') . ' = '. $db->quote('Solicitado'));
$db->setQuery($query);
$result = $db->loadObjectList();
If your database table do not belong to an extension associated with Joomla, then remove the #__ prefix in the above code.
Hope this helps

PHP SQL Joomla store table with two columns into array and echo them later

So I have a simple table with two columns. The table is called "mytable" and the columns are "product" and "id". The table has about 12 entries.
$db = JFactory::getDbo();
$query = "SELECT * FROM mytable";
$db->setQuery($query);
$results = $db->loadObjectList();
So I've gotten this far querying successfully but I want to call back the results of each row. That's the part I am stuck on. I need to store this into an array so I can later make a while loop spitting out each row. Also I cannot echo it right underneath that code. This needs to be stored in an array that is then pulled from another page and then I spit out each row. Thanks!
Try this,
$db = JFactory::getDbo();
$query = "SELECT * FROM mytable";
$db->setQuery($query);
$results = $db->loadObjectList();
echo '<pre/>'
print_r($results);//the resulted array is already in this variable you can iterate it later with foreach loop.
for looping and printing
foreach($results as $key=>$value){
echo 'Product-->'.$value->product;
echo 'ID-->'.$value->id;
}
check the Joomla DB query for more details.
Hope it helps..
I would personally use more up to date coding standards for you query, like so:
$db = JFactory::getDbo();
$query->select($db->quoteName('*'))
->from($db->quoteName('mytable'));
$db->setQuery($query);
$results = $db->loadObjectList();
Then to get the results, create a loop:
foreach($results as $result) {
echo 'ID = ' . $result->id;
echo 'Product = ' . $result->product;
}
Hope this helps

Using php to access mysql, how can I get the query result which is only a number?

For example, for some queries like SELECT MAX(field), the query result is usually only a field value, rather than returning rows to you.
Now the field of the value I wanna get is integer type.
As I'm a beginner of php, how can I get that value from the query result?
As I do the following
$query = "SELECT MAX(stringid) FROM XMLString";
$result = mysql_query($query, $link);
echo $result;
Then nothing is echoed out.
I have check the db connection made by mysqlconnect, and it's got no problem.
And I tried this query in MySQL at phpMyAdmin, then the query is what I want, too?
So why would it be like that, and any solution?
Thanks!
You will always retrieve rows back from an SQL query, even if there's only one row with one field. You can directly retrieve a specific field of a specific row using mysql_result:
$query = "SELECT MAX(stringid) FROM XMLString";
$result = mysql_query($query, $link);
echo mysql_result($result, 0, 0);
$query = "SELECT MAX(stringid) as val FROM XMLString";
$result = mysql_query($query, $link);
$rows = mysql_num_rows($result);
if($rows > 0) {
$rstAry = mysql_fetch_array($result);
echo $rstAry['val'];
}
Try This
mysql_query is not printing results. Maybe try this:
echo mysql_result($result);
You are not getting any result because mysql_query returns a database object.
You will need to process this "database object" using mysql_fetch_array. This command 'fetches' an array out of a MySQL "database object". The array you are fetching is the rows your query produced. In your case, it is just one row.
Here is the code:
This step sets your query:
$query = "SELECT MAX(stringid) as val FROM XMLString";
This step will run your query, and return the database object:
$result_database_object_whatever = mysql_query($query, $link);
This step will process the database object, and give you an array of the queried rows:
$result_array = mysql_fetch_array($query, $link);
This step will echo the first row returned by your query:
echo $result_array[1];
You can do something like this:
$query = "SELECT MAX(stringid) FROM XMLString";
$result = mysql_query($query, $link);
$fetch = mysql_fetch_assoc($result);
echo $fetch['stringid'];
The mysql_fetch_assoc() function retrieves the data in an associative array.
Would it help to give the max a name you could reference? Also ifyou think there's syntax errors you could just add the error to help clarify.
$query = "SELECT MAX(stringid) as maxNum FROM XMLString";
$result = mysql_query($query, $link) or die(mysql_error());
echo $result;

Categories