Read from database, single rows (Joomla framework) - php

I use this code to read from data base:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array('category_name', 'category_short_description'));
$query->from('#__redshop_category');
$query->where('category_id = 1');
$db->setQuery($query);
$results = $db->loadObjectList();
How can i read once element, for example extra *category_name* and extra *category_short_description*
Here is also offical joomla wiki, but without single output: http://docs.joomla.org/J2.5:Accessing_the_database_using_JDatabase
Here what i have doing, but that's not work:
echo $results; ->output:ARRAY
echo $results['category_name']; -> without output, empty (In database rows are not empty)
How can i do this?

You can use loadObject() instead of loadObjectList() to retrieve just the first row:
$results = $db->loadObject();
echo $results->category_name;
If you want an associative array instead of an object, there is loadAssoc():
$results = $db->loadAssoc();
echo $results['category_name'];

To fetch single row you can use
$result = $db->loadObject()
instead of
$results = $db->loadObjectList()
and then you can access data by
$result->category_name;
$result->category_short_description;

Related

Can't get data from table with php and sqlite db

I want to echo onto my screen all the data is my 'userdata' table. i have looked around and found this code but when i run it i get a HTTP ERROR 500.
this is my code that im trying to use:
<?php
$database = new SQLite3('home.db');
$result = $database->query("SELECT * FROM userdata");
echo $result;
?>
the $database->query() method will return an SQLite3Result object which you can't just "echo". Instead, you should loop through all the results like so:
<?php
$database = new SQLite3('home.db');
$result = $database->query("SELECT * FROM userdata");
while ($row = $result->fetchArray()) {
print_r($row);
}
?>
The $row variable inside the while loop will be an array. Use the appropriate index to get the value of a single column if necessary.

Selecting data from other columns

I've rather new at php and mysqli and I've had some success but am currently facing a wall. Mostly because I dont know the terms to express what i'm trying to do.
I can select a row all day long but I seem to be stuck on the same row. I need to be able to select data from further down the column. Here is the code i'm working with.
$qry = "select * from products";
$rs = mysqli_query($conn,$qry);
$getRow = mysqli_fetch_row($rs);
$getRowAssoc = mysqli_fetch_assoc($rs);
echo "<img src=\"".$getRow['1'] . "\">";
i have several links to images in the picture column on my database but can't seem to figure out a simple way to display the links from other rows in that column. I may be way off base here but I dont think I am.
this is a layout of the db db pic
any help would be much appreciated
For each mysqli_fetch_assoc you get the result of the next row. As the index starts in -1, the first time you call it, it goes to the first row. So whenever you call it again, it goes to the next row. Use it inside of a loop and you will be all set.
while($row = mysqli_fetch_assoc($result)){
// $row will have new content each iteration
}
mysqli_fetch_assoc Fetch a result row as an associative array, but just 1 row, if you want to get all the result, you have to go through a loop to get everything for the result, example :
<?php
$query = 'SELECT `products`.* FROM `products`';
$mysqli = new Mysqli('localhost','test','root','password');
$result = $mysqli->query($query);
while ($row = $result->fetch_assoc()) {
echo '<img src="' . htmlentities($row['pic']) . '" /><hr />';
}
unset($row);
$result->free();
$mysqli->close();
if you want to get all the rows in 1 step for later usage, you would use mysqli::fetch_all(), example :
<?php
$query = 'SELECT `products`.* FROM `products`';
$mysqli = new Mysqli('localhost','test','root','password');
$result = $mysqli->query($query);
$products = $result->fetch_all();
$result->free();
$mysqli->close();
print_r($products);

how to echo multiple DB results with one query

If I were to echo the result of this query:
//Find membertype from community builder
$db->setQuery('Select cb_membertype From #__comprofiler Where id='.$user->id);
$membertype = $db->loadResult(); //load the result from the query
I would use:
echo "Your member type is: $membertype";
I'd rather not use a new query for every variable thought since they are all in the same table.
I'd rather run a single query like this:
//Find all 3 from one query
$db->setQuery('Select cb_membertype, cb_2015vopac, cb_2015llf
From #__comprofiler Where id='.$user->id);
$result = $db->loadResult(); //load the result from the query
How do I echo the specific fields from that query when using the single $result variable?
First, let me just say that should should quote your values/columns/table names.
Secondly, if you want to get multiple values, you need to use loadObjectList instead of loadResult.
The following will do it for you:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('cb_membertype', 'cb_2015vopac', 'cb_2015llf')))
->from($db->quoteName('#__comprofiler'))
->where($db->quoteName('id') . ' = '. $db->quote($user->id));
$db->setQuery($query);
$results = $db->loadObjectList();
foreach ($results as $result)
{
echo $result->cb_membertype;
echo $result->cb_2015vopac;
echo $result->cb_2015llf;
}
So loadObjectList give you an object, then you can loop though it using foreach loop.
Hope this helps

PHP MySQL Database - All rows into Multi-Dimensional Array

It's been a long while since I touched PHP so, I just need a refresher of sorts.
I have an HTML form that captures several lines of data (dataA, dataB, dataC & dataD) and inserts them into a database table (dataAll). I will be entering rows upon rows of data into "dataAll". What I'm looking to do is create a display.php page, where the code will take all of the data and place each cell into an array, or the row of an array, for example:
new Array = [["dataA", "dataA", "dataA", "dataA", "dataA"],
["dataB", "dataB", "dataB", "dataB", "dataB"],
["dataC", "dataC", "dataC", "dataC", "dataC"],
["dataD", "dataD", "dataD", "dataD", "dataD"]];
But I cannot remember the syntax on how to perform this task. Any help would be greatly appreciated.
The database is named 'compdata', the table is 'dataAll', and each row is 'dataA', 'dataB', 'dataC', 'dataD'.
Please let me know if I need to supply more information.
Since you asked for All rows, so the simple code for query is written below:
<?php
//after connection to mysql db using mysql_connect()
$sql = "Select dataA, dataB, dataC, dataD from `compdata`.`dataAll`" ;
$result = mysql_query($sql) ;
if(mysql_num_rows($result) > 0 ){
while($row = mysql_fetch_array($result)){
$dataArray[] = $row ;
}
}
echo '<pre>';
print_r($dataArray) ;//you got the desired 2D array with all results
?>
Assuming you are used to the old mysql_xxx functions:
$data = array ();
$result = mysql_query ('select * from dataAll');
while ($row = mysql_fetch_array ($restult, MYSQL_ASSOC))
$data [] = $row;
Result:
$data = array (
0=>array ('col_1'=>'dataA', 'col_2'=>dataB...),
1=>array ('col_1'=>'dataA', 'col_2'=>dataB...)
);
If you only want the numbers and not the column names, use MYSQL_NUM.
However, mysql functions are being replaced with the more generic PDO object so you might want to look into that.
$stmt = $pdo->query ('select * from dataAll');
$result = $pdo->fetchAll (PDO::FETCH_ASSOC); // Or PDO::FECTCH_NUM
Same results as above.

how to get data from virtuemart categories tables?

hi i need to make a drop down list field in a form (RSForm joomla 2.5), that will drew its values from virtuemart categories names.
i have this block of code that i need to customize to my needs but since i dont know php all my improvisations ended up in fatal error and need to reinstall the form again:(
the name of my table in mysql is xxx_virtuemart_categories_he_il
the names of the categories are listed here category_names
their id's are here virtuemart_category_id
this is the block of code how do i change it?
//<code>
// Prepare the empty array
$items = array();
// Prepare the database connection
$db = JFactory::getDbo();
// Run the SQL query and store it in $results
$db->setQuery("SELECT your_value, your_label FROM #__your_table");
$results = $db->loadObjectList();
// Now, we need to convert the results into a readable RSForm! Pro format.
// The Items field will accept values in this format:
// value-to-be-stored|value-to-be-shown
foreach ($results as $result) {
$value = $result->your_value;
$label = $result->your_label;
$items[] = $value.'|'.$label;
}
// Multiple values are separated by new lines, so we need to do this now
$items = implode("\n", $items);
// Now we need to return the value to the field
return $items;
//</code>
Add:
$db->query();
before:
$results = $db->loadObjectList();
The fast and simple solution to connect to a bd in virtuemart is:
$db = JFactory::getDbo();
$db->setQuery("SELECT * FROM ... WHERE ...");
$db->query();
$results = $db->loadObjectList();
echo var_dump($results);

Categories