I'm trying to make a query to a database in Joomla 2.5. I have a db named 'example', and I'm trying to get certain value named 'value' (very original) for a user whose id is 949:
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$user = 949;
$db->setQuery( 'SELECT value FROM example WHERE user_id = ' . $user );
$result = $db->loadObjectList();
echo $result;
However, I'm just getting 'Array' as result (the expected value is a decimal, e.g. 4.5).
Could someone please tell me what am I doing wrong?
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$user = 949;
$db->setQuery( "SELECT value FROM example WHERE user_id = '" . $user."'" );
$result = $db->loadObjectList();
echo $result;
try this one
$db->loadObjectList() returns an array of objects, which echo can't display. If you want to just return one value from one row, user $db->loadResult() instead.
Related
I'm creating a script that goes through the Active directory and then searches for information based on another query when the information is entered and there is no information from the active directory the information from the last entered area enters into that SQL row even if there is no information for that site. Should I create an if else statement within one of the foreach loop to fix this problem?
while($rows.Read()){
$SITEINFO = $rows['ActiveDirectoryOU']
$ADARRAY= Get-ADGroupMember -Identity $SITEINFO | Get-ADUser -Properties ('Mail')
ForEach($OBJECT in $ADARRAY){
$NAME = $OBJECT.Name
$USER = $OBJECT.SamAccountName
$EMAIL = $OBJECT.Mail
$SITECODE = $SITEINFO
$INSERT = "INSERT INTO $TABLE VALUES ('$USER','$SITECODE','$EMAIL', '$NAME');"
$SQL = $SQLCON.CreateCommand()
$SQL.CommandText = $INSERT
$SQL.ExecuteNonQuery()
}
}
}
$SQLCONN.Close()
<?php
$con = mysql_connect('localhost', 'root', 'pass');
if(!$con){
echo "Error connection";
}
$select_db = mysql_select_db('mydatabase', $con);
if(!$select_db){
echo "Error to select database";
}
mysql_set_charset("utf8", $con);
$result = mysql_query("select count(1) FROM Credit WHERE CreditStatusID=6");
$row = mysql_fetch_array($result);
$total = $row[0]-1;
echo "<font size='6px' color='#000000'>Total: </font><font size='6px' color='#FF0000'><b>" . $total . "</b></font>";
mysql_close($con);
?>
I need to get some other information for example WHERE CreditStatusID=6 AND CreditStatus=7,8,9,.. but it does not work. Can you explain me how can I do that in another way?
Try:
select count(1) FROM Credit WHERE CreditStatusID=6 and CreditStatus in (7,8,9)
The WHERE-clause should look like this:
...WHERE CreditStatusID=6 AND CreditStatus IN (7, 8 ,9);
instead of the array "(7,8,9)" you can also use a sub-select..
I had a query:
$query =
Yii::app()->db->createCommand()->setFetchMode(PDO::FETCH_OBJ)
->select('*')
->from('products p')
->limit(5);
And then I got results:
$result = $query->queryAll();
How can I use the existing query object, without rewriting it, to re-instantiate the values for "select" and "limit". I want to change select to "count(p.productid)" and unset limit. I tried the following but it does not work:
$query =
Yii::app()->db->createCommand()->setFetchMode(PDO::FETCH_OBJ)
->select('*')
->from('products p')
->limit(5);
$result = $query->queryAll();
$query =
$query
->select('count(p.productid)')
->limit(-1);
$result2 = $query->queryRow();
This still returns the result of the first select?
EDIT
Basically, I want to RESET the select. It looks like it builds on the select. So, I think you can do this:
$query =
$query->select('something')
->select('somethingElse')
->select('somethingMoreStuff');
My problem is that I want to "reset" the select. So basically UNDO these selects. Is this possible?
Why don't you do:
$intialQuery =
Yii::app()->db->createCommand()->setFetchMode(PDO::FETCH_OBJ)
->from('products p');
$query = $intialQuery
->select('*')
->limit(5);
$query2 = $intialQuery
->select('count(p.productid) as number')
->limit(-1);
$result = $query->queryAll();
$result2 = $query2->queryRow();
I understand there are MANY ways to do all of this, but trying to do it the best way.
I have created the db parameters, dns, dbh, sth, sql and generally quite happy with the result up to ... well ... the result part.
<?php
// db parameters
$dbhost = "localhost";
$dbname = "x";
$dbuser = "y";
$dbpass = "z";
// driver invocation (dsn is short for data source name)
$dsn = "mysql:host=$dbhost;dbname=$dbname";
// create db object (dbh is short for database handle)
$dbh = new PDO($dsn, $dbuser, $dbpass);
// execution of database query (sth is short for statement handle)
$sql = "SELECT * FROM a_aif_remaining";
$sth = $dbh->prepare($sql);
$sth->execute();
NOT SURE WHAT TO PUT BELOW.... (A) or (B)
I just want to present a simple array of the data. One row from the table per line.
Option A
echo $_POST['fieldname1'];
echo $_POST['fieldname2'];
echo $_POST['fieldname3'];
Option B
while ($rows = $sth->fetch(PDO::FETCH_ASSOC)) {
echo $row[fieldname1],'<br>';
}
AND I AM CONFIDENT WITH THE ENDING
$dbh = NULL;
?>
Any advise would be GREATLY appreciated.
UPDATED CODE: (Produces nothing on the page)
<?php
// db parameters
$dbhost = "localhost";
$dbname = "theaudit_db1";
$dbuser = "theaudit_user";
$dbpass = "audit1999";
$dsn = "mysql:host=$dbhost;dbname=$dbname"; // driver invocation (dsn is short for Data Source Name)
try {
$dbh = new PDO($dsn, $dbuser, $dbpass); // connect to new db object (dbh is short for Database Handle)
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // set the PDO error mode to enable exceptions
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); // set the PDO emulate prepares to false
// execute query to database (sth is short for Statement Handle)
$sql = "SELECT * FROM a_aif_remaining";
$sth = $dbh->prepare($sql);
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
$dbh = NULL;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Though I can't get what's the connection between A anb B, I can answer the
I just want to present a simple array of the data. One row from the table per line.
question.
$sql = "SELECT * FROM a_aif_remaining";
$sth = $dbh->prepare($sql);
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
where $data is a sought-for array.
The problem with your updated code is simple - you arent echo'ing your data out. You need to add something like..
foreach($data as $arKey=>$dataRow){
foreach($dataRow as $arKey=>$rowField){
echo $rowField.','; //concat with a ',' to give csv like output
}
echo '<br>'; //to get to next line for each row (may want to trim the last ','
}
I am also confused by the reference to $_POST. It is true both are associate arrays but that does not mean that the $_POST option is viable - the data would only be available in the $_POST if you put it there (eg $_POST = $data) which would be pointless. Or if you had posted the data from somewhere else. Neither seem to fit what you are asking so I would forget about the $_POST and just figure out how you access your multi dimensional $data array. There is endless tut's on this subject. Try using
var_dump($data)
to see whats inside that should help you visualise what is going on.
NOTE: in option B you are not correctly concatenating or referencing your array it should be:
while ($rows = $sth->fetch(PDO::FETCH_ASSOC)) {
echo $rows[fieldname1].'<br>'; //$row doesnt exist its $rows and you use . to concat not ,
}
Ah yes and probably better to use unset rather than setting $dbh to equal null
unset($dbh);
How to create select list, radio buttons, checkboxes with field_create_field() and how to specify the options to be given in these fields
Run this code with the details for an existing field with the properties that you want to copy:
$entity_type = 'node';
$field_name = 'body';
$bundle_name = 'article';
$info_config = field_info_field($field_name);
$info_instance = field_info_instance($entity_type, $field_name, $bundle_name);
unset($info_config['id']);
unset($info_instance['id'], $info_instance['field_id']);
include_once DRUPAL_ROOT . '/includes/utility.inc';
$output = "field_create_field(" . drupal_var_export($info_config) . ");\n";
$output .= "field_create_instance(" . drupal_var_export($info_instance) . ");";
drupal_set_message("<textarea rows=30 style=\"width: 100%;\">". $output .'</textarea>');
That will produce the PHP code used to create the field/field instance. Then you just need to go through the code and make the changes for your new field/instance.