I'm using Joomla 2.5 and I need to retrieve the field 'avatar' for the current user.
This is the code I'm using:
$user = JFactory::getUser();
$id = $user->get('id');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('avatar')
->from('#__discuss_users')
->where('id = $id');
$db->setQuery($query);
$results = $db->loadObjectList();
But it isn't displaying any data. I tried changing the last line to:
$results = $db->loadResult();
But it doesn't work either.
If you are using it as you posted, you won't include the actual value of $id in your query. You need to append it to the string like this:
$query->select('avatar')
->from('#__discuss_users')
->where('id = '.$id);
I am usually not using the $query->select stuff, but a plain old query. Thus, you might try to do it like this (this might not be best practice though):
$user = JFactory::getUser();
$id = $user->get('id');
$db = JFactory::getDBO();
// $query = $db->getQuery(true);
$query = "SELECT ".$db->quoteName('avatar')
." FROM ".$db->quoteName('#__discuss_users')
." WHERE ".$db->quoteName('id')
." = ".$db->quote($id).";";
// $query->select('avatar')
// ->from('#__discuss_users')
// ->where('id = $id');
$db->setQuery($query);
$results = $db->loadObjectList();
Thanks for your help. The following code works:
$user = JFactory::getUser();
$id = $user->get('id');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
->select($db->quoteName('avatar'))
->from($db->quoteName('#__discuss_users'))
->where('id = '.$id);
$db->setQuery($query);
$results = $db->loadResult();
echo $results;
Related
When I enter category name manually, I'm able to get its product but when I insert a variable to like the code, I get success but data is empty.
Connection file:
<?php
//cnnection.php
$server = "localhost";
$user = "root";
$password = "";
$db = "db_ecommerce_shoes";
$connect = new mysqli($server,$user,$password,$db);
Main script:
<?php
include '../connection.php';
With this, request is successful but data is empty
$name= 'name';
$sql = "SELECT * FROM tb_shoes
WHERE
catName ='$name'
";
With this, request is successful, data is loaded successful
$sql = "SELECT * FROM tb_shoes
WHERE
catName ='Ladies Shoe'
";
$result = $connect->query($sql);
if($result) {
$data = array();
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
echo json_encode(array(
"success"=>true,
"data"=> $data,
));
} else {
echo json_encode(array(
"success"=>false,
));
}
Any other way of doing this will be much appreciated.
Any other way of doing this will be much appreciated
You should never be directly concatenating variables into the SQL query making it vulnerable to SQL injections. That is a major security issue.
Read about a simple and easy to understand version of prepared statements here.
In your case, try this out
$category = "Ladies Shoe"
$statement = $connect->prepare("SELECT * FROM tb_shoes WHERE catName = ? ")
$statement ->bind_param("s", $category);
$statement ->execute();
$result = $statement ->get_result();
Then you can loop through the results as above. Check if it works!
Try to assign
$name = 'Ladies Shoe';
$sql = "SELECT * FROM tb_shoes
WHERE
catName ='$name'
";
Does this work?
Sorry. I am very new to Joomla.
After I load data from the database like this:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('user_id')));
$query->from($db->quoteName('#__user_profiles'));
$db->setQuery($query);
$results = $db->loadObjectList();
How to I show the results?
Try
foreach ($results as $result) {
echo $result->user_id.'<br />';
}
Am learning & executing php by working on Joomla project
How to Improve this code & resolve the PHP Notices - Any suggestions - solutions - well appreciated !!
Notice: Undefined variable: cond in*/home/mygames/public_html/components/com_toys/models/category.php on line 140
(which is $sql line)*
function loadSubCat($id,$Carmodel,$minprice,$maxprice){
$mainframe =& JFactory::getApplication();
$option = JRequest::getCmd('option');
$database =& JFactory::getDBO();
global $Itemid;
if($Carmodel!="")
$cond=" and prod_id='$Carmodel' ";
$sql = "Select * from #__toycar_products Where prod_cat_id='".$id."' $cond and prod_status='1' and prod_id in (select v_prod_id from #__toycar_variants) Order By prod_sorder";
Notice: Trying to get property of non-object in /home/truecar7/public_html/components/com_toys/models/category.php on line 200
Line 200 is return $row->id;
function getItemIdByName($Name){
$mainframe =& JFactory::getApplication();
$option = JRequest::getCmd('option');
$database =& JFactory::getDBO();
$sql = "Select id from #__menu Where name = '".$Name."'";
$database->setQuery($sql);
$row = $database->loadObject();
return $row->id;
}
Edit
Hello Lodder & Elin, it works but like this, else it's showing undefined variable notice for row on return $row line.
function getItemIdByName($Name){
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
->from('#__menu')
->where('id = ' . $db->quote($Name));
$db->setQuery($query);
$rows = $db->loadObjectList();
foreach ($rows as $row){
$row = $row->msg;
}
$row='';
return $row;
}
Try using the following. I have made some changes to your function and used Joomla 2.5 coding standards for the database query.
$Name = "XXXXXXXXX"; //define the name variable
function getItemIdByName($Name){
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
->from('#__menu')
->where('id = ' . $db->quote($Name));
$db->setQuery($query);
$rows = $db->loadObjectList();
foreach ($rows as $row){
$row = $row->msg;
}
return $row;
}
echo getItemIdByName($Name); //echo the result of the function
For your Undefined Notice, You have to modify your codes like this
$cond = '';
if($Carmodel!="") {
$cond = " and prod_id='$Carmodel' ";
}
For Trying to get property of non-object Notice : I think $row is empty that is why throws notice.Check $row
var_dump($row);
Problem :
$database->loadObject(); // This line
I use such code:
$query = "SELECT introtext FROM #__content WHERE alias = '$alias'";
$db->setQuery($query);
$fullArticle = $db->loadResult();
if(!strlen(trim($fullArticle))) $fullArticle = JText::_('ERR_ARTICLE_NOT_LOADED');
Article: <p>1</p><p> </p><p>2</p>
In database: <p>1</p><p> </p><p>2</p>
But it returns: <p>1</p><p>B </p><p>2</p>
Try using the following code which is the Joomla 1.6+ database query method. Works fine for me.
$db = JFactory::getDbo();
$alias = "";
$query = $db->getQuery(true);
$query->select('introtext')
->from('#__content')
->where('alias = '.(int) $alias);
$db->setQuery($query);
$fullArticle = $db->loadResult();
Then echo it like so:
echo '<p>' . $fullArticle . '</p>';
At first I was getting an error saying that $alias was undefined so I just defined it as nothing in my code, howeevr you can change it to whatever suits your needs.
I am getting mental on this. I have tried everything so far, for hours. Here is the task:
In a module override, I use this code:
$db =& JFactory::getDBO();
$title = "Analysen & Auswertungen Infos";
$query = "SELECT introtext FROM #__content WHERE title=\"$title\"";
$db->setQuery($query);
$result = $db->loadRow();
echo $result;
This works, but since I am getting the $title dynamically from a variable I need this to work:
$db =& JFactory::getDBO();
$title = "$linktext Infos";
$query = "SELECT introtext FROM #__content WHERE title=\"$title\"";
$db->setQuery($query);
$result = $db->loadRow();
echo $result;
I have 6 variables that populate $linktext in a foreach loop, all work except the one with the string including the "&"...
I tried htmlentities and utf8_encode and different kind (actually all combinations) of " and ' in the query... nothing worked.
Whe I use the following sql query in phpmyadmin it works:
SELECT `introtext` FROM `x999x_content` WHERE `title`="Analysen & Auswertungen Infos"
I am really puzzled over this, and right now very tired and angry...
Any help will be greatly appreciated!!!
Sometimes a good long sleep is the best you can do!
I just used strlen to check the length of $linktext (which I get from DB via a foreach loop) and found that it is longer than the visible chars. This is logical because of the & which is returned as &.
In order to use this $linktext inside a new DB query, all I needed to do was to decode the html entity:
$db =& JFactory::getDBO();
// this is the correct way of doing it
$title = html_entity_decode($linktext)." Infos";
$query = "SELECT introtext FROM #__content WHERE title=\"$title\"";
$db->setQuery($query);
$result = $db->loadRow();
or any other combi for the query (",',`,$query->select ) as pointed out in the other answers
Did you try:
$db =& JFactory::getDBO();
$title = $linktext." Infos";
$query = "SELECT `introtext` FROM `#__content` WHERE `title`='".$title."'";
$db->setQuery($query);
$result = $db->loadRow();
echo $result;
I don't know if it'll work but, I never have any problems and this is how I write my code.
Try changing this line:
$query = "SELECT introtext FROM #__content WHERE title=\"$title\"";
For this:
$query = $db->getQuery( tru );
$query->select( 'introtext' );
$query->from( '#__content' );
$query->where( 'title=' . $db->Quote( $title );
I hope it helped!
I got this working:
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
$keyvalue='101';
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM mytable WHERE keyfield='".$keyvalue."'";
$result = $conn->query($sql);
if($result && $row = $result->fetch_assoc()) {
$fsql='DESCRIBE mytable';
$fresult = $conn->query($fsql);
if($fresult){
while($frow = $fresult->fetch_assoc()) {
$fieldname=$frow['Field'];
echo($fieldname . ' = ' . $row[$fieldname] . '<br><br>');
}
}
}else echo("Failed: " . $sql);
?>