Evening community,
I stuck at one problem, which I can't find solution for. I would appreciate if you could give me a piece of your advice.
In brief, I wrote the following function in a php-file:
public function getCurrencyReal(){
$sql = "SELECT currency_real FROM currency WHERE currency_id = '4' limit 1";
$query = $this->db->query($sql);
$currency_real = $query;
return $currency_real->row;
}
After that I added the following code in another php file, that should generate XML-list:
$currency_real = $model_module_xmlcreator -> getCurrencyReal();
and
$out .= "<test>" . $currency_real . "</test>";
As a result I've received the following thing:
, saying "Array".
I've realized that I'm asking for an array even though I want to get info only from one field and I actually need a string. So I changed the code a bit to
$currency_real = json_encode($query);
return $currency_real;
and my next output was
I believe that I miss something simple, but I can't find what (the output should be just "33.00"). Pardon me if the question is silly, I've started studying PHP not much time ago.
All best
just change this line
$out .= "<test>" . $currency_real->rows[0]->currency_real . "</test>";
or
$out .= "<test>" . $currency_real->row->currency_real . "</test>";
and don't use json_encode
Related
From Joomla documentation i read of how to run sql queries in php.
My code is the below:
$query->select($db->quoteName('customfield_value'));
$query->from($db->quoteName('#__virtuemart_product_customfields'));
$query->where($db->quoteName('virtuemart_custom_id')." = ".$db->quote('322'));
$query->and($db->quoteName('virtuemart_product_id'). " = ".$item->virtuemart_product_id));
$db->setQuery($query);
$results = $db->loadObject();
echo "Factory Code:" . $results->customfield_value;
Code was bringing a Factory code but not product's Factory code and then i had to add the line
$query->and......
to tell to bring Factory code from specific product but it brings no results at all. Eventually i write something wrong with $query->and ?
Any ideas/solutions?
Thank you in advance
Ok i found correct answer/syntax:
$query->select($db->quoteName('customfield_value'));
$query->from($db->quoteName('#__virtuemart_product_customfields'));
$query->where($db->quoteName('virtuemart_custom_id')." = 322", "AND")
->where($db->quoteName('virtuemart_product_id')." = ". $item->virtuemart_product_id);
Couldn't find an exact answer to this question on here, though it may be simple (if there is an answer please point me in the right direction).
I am pulling some data from MySQL and some of the characters are causing the data not to be displayed at the end point. I therefore need to single out these specific characters and replace them with a permitted character. Specifically I need to change & , ' and + . I have to do this working from this query:
$query = "select * from data where a_data_id=".$ID." AND a_discard_data_from!=1";
I was wondering if I can add to this string, or after this string, some rule to replace instances & , ' or + with another character/s.
Thanks for looking!
NOTE: There is no problem with the string above, it is functioning fine, I just want to add to it or after it some type of code that will replace certain characters in the data pulled from the query
Surrounding Code:
$arr="";
$main_arr="";
$query = "select * from data where a_data_id=".$ID." AND a_discard_data_from!=1";
$query .=" and last_update >= " . '"' . $dataDate . '"';
$table = mysql_query($query);
if (mysql_num_rows($table) > 0) {
while ($row = mysql_fetch_assoc($table)) {
foreach ($row as $key => $value) {
$arr[$key] = $value;
if ($key == "to_data_id") {
$array=mysql_fetch_array(mysql_query("select name,additional from details where data_id=".$value));
$arr["data_id"] = $array['additional'].".".$array['name'];
}
}
$main_arr[] = $arr;
}
}
$data = json_encode($main_arr);
Just how to slow it into this code would be great, thank you! (This code works fine, just want to know what I could change / add to replace those characters).
UPDATE: Is anyone able to give me an answer with how I might be able to use strtr()function to replace the results please? Thanks again for the responses!
In mysql if you manually select a field after selecting * it overwrites that field, so provided there's only one field you want to run replaces on and you know the field you know which field it is in advance you can change your select query to (the somewhat ungraceful):
"select
*,
REPLACE(
REPLACE(
REPLACE(`data`.`YOURFIELDNAME`, '&', 'A'),
',', 'A'),
'\'', 'A')
from
data
where a_data_id=".$ID." AND a_discard_data_from!=1";
Which will remove those characters. This is not very graceful, and as eggyal pointed out, you're better off doing this on the front end.
I am referring to this question here on stackoverflow, and I have the same problem, except that I use postgre database and can't seem to get this working.
This is my php function which is querying the database:
public function getCashData($id, $date)
{
if ($this->openConnection()){
$query = "SELECT * FROM cash_register (". $id .", '". $date ."');";
$result = pg_query($query);
if (!$result){
return false;
}
return pg_fetch_all($result);
}
}
I call this function like this:
$cashReport = getCashReport($id, $date);
$cashReport = array_map('utf8_encode' , $casaReport); //**note: please read below
echo json_encode($casaReport);
**This was working perfectly when I was returning only one row of result (and not all like now), but now when I'm returning an array of rows this array_map function (which I found in the above mentioned link) is not working as it expects an array and not an array of arrays.
Can you guys help me solve this problem?
Try this:
function encode_items(&$item, $key)
{
$item = utf8_encode($item);
}
array_walk_recursive($cashReport, 'encode_items');
It's also possible to "tell" the postgre server to send the data utf-8 encoded.
see http://docs.php.net/pg_set_client_encoding
I did this in the end as I was forced to "fix" it asap:
$json = json_encode($cashReport);
$jsonEscapedNulls = str_replace('null', '""', $json);
echo $jsonEscapedNulls;
Now, I understand that "fixes" aren't solutions but I had no other choice in a give period of time :(. So if someone gives an answer which will work not as a fix but as a general solution I will be grateful. Until then, this will have to do.
HI Please help to fix this code , i use TITLE
in this code but its not work
<?
$sql = "select * from wallpaper order by wallpaperid desc limit 20";
$result = mysql_query($sql, $db) or die(mysql_error());
if(mysql_num_rows($result)) {
while($myrow = mysql_fetch_array($result)) {
$title = substr($myrow['title'] ,0,31);
$wurl = ereg_replace(" ", "-", $myrow['title']);
$html = '<dt>%s..</dt>';
printf($html, $wurl, $myrow["wallpaperid"], $myrow["wallpapername"], $myrow["title"], $category);
} }
?>
plsease someone help me to fix this
Second code not work onmouseOver TIP
First of all, this is purely a front-end (that is, Javascript/HTML) problem. It has nothing to do with PHP. You haven't actually provided enough information to help pinpoint the issue. It would be much more helpful to see your "doTooltip" and "hideTip" javascript functions.
That said, I notice that you're attempting to use variables $siteurl, $wallpapername, and $wallpaperid variables in your link string. You cannot use PHP variables in a string delimited with ' (single quotes).
Try this:
$html = '<dt>%s..</dt>';
But I suspect that this isn't related to the problem you're actually trying to solve. I'd recommend that you revise your question. Leave out the PHP this time and only show the final output generated by your script. Good luck!
What I thought was going to be an easy implementation of two lines of code and a function, turned out to be made of fail.
On my webpage, I want to be able to type [text]1[/text], and what it will do is pull the title of that ID.
function textFormat($text) {
$raw = array(
'\'\[text\](?P<id>.*?)\[/text\]\'is'
);
$out = array (
'<a href="index.php?function=getData&reference=text&id=$1">' . getTextTitle() . '</a>'
);
preg_replace($raw, $out, $text);
return $text;
}
function getTextTitle($id) {
$sql = mysql_query("SELECT title FROM text WHERE id = $id");
return mysql_result($sql);
}
So, here's the lovely little problem: As one can tell, I'm calling a function with a numeric-titled variable, which works great in the quotation marks, but as we know, PHP doesn't like that. So, I opted for a named group. Using $regs['id'] fails to work.
Am I doing something wrong?
Am I going about this the wrong way?
Well, you're certainly doing it in a radically different way than I ever would, but I think something not too far off from what you're attempting may possibly work. Try this:
function textFormat($text) {
$raw = array(
'\'\[text\](?P<id>.*?)\[/text\]\'ise'
);
$out = array (
'\'<a href="index.php?function=getData&reference=text&id=$1">\' . getTextTitle(\'$1\') . \'</a>\''
);
preg_replace($raw, $out, $text);
return $text;
}
function getTextTitle($id) {
$sql = mysql_query("SELECT title FROM text WHERE id = '" . mysql_real_escape_string($id) . "'");
$res = mysql_result($sql);
$row = mysql_fetch_array($res);
return $row ? $row[0] : 'invalid ID';
}
Your original getTextTitle() would, unless something else is going on I'm not aware of, let anyone do anything they liked to your database via SQL injection, by the way. You're welcome.
Also, I don't know what that (?P<id> noise is about in the regex, so I'm assuming it's needed for some reason and leaving it alone. I do not know whether this is correct.