how get the value from assoc array using php - php

Array([0] => stdClass Object([max(Transaction_id)] => 10251))
My php sql query returns the answer in this format.
How can i display the only value from this format.
Thanks in advance

In your query add the as then the name of the column that you want to retreive.
SELECT MAX(COLUMNNAME) AS example example FROM TABLENAME
And then in your php file
echo $result[0]['example'];

if for some reasons you are unable to convert the sql query you can access to this variable in the following way
echo $obj->{"max(Transaction_id)"};

Related

PHP: Get string from within Array?

I'm using facebook SDK and I'm trying to get a string from an array which is the facebook php sdk response.
Basically, I get the age_range from facebook and I need to get the value of age_range.
The age range always is an array for some strange reason.
It looks like this:
[age_range] => Array
(
[min] => 21
)
so, in my php I have this:
$age_range = $me['age_range'];
but when i insert this $age_range into mysql database, I get the word Array inserted into mysql database!
But all i need is the 21 or any other number that returns back from age range.
could someone please advise on this issue?
Thanks in advance.
As stated in my comment, simply change:
$age_range = $me['age_range'];
to
$age_range = $me['age_range']['min'];

How to store data in db in array form

i want to store my PHP values in db(mysql) in the form of array...
for example
$a=array{10,20,30,40};
i want to store this variable $a in to db in the array form like how it's storing in array using index.
why i want to do this because in future i may have to perform update or delete operation on the array values..
i know that it's possible to do this thing... but i don't know how to implement this..
i searched about this topic but i didn't get proper answer....
Please suggest me how to do this things...
Why don't use json_encode in PHP and store it on your database. It's the best way.
The array will be converted to a string and will be stored.
Retrieve the data and make use of json_decode and then start working as per your needs.
Example:
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
OUTPUT: {"a":1,"b":2,"c":3,"d":4,"e":5}
You should create a distinct TABLE to store this kind of data.
Consists of 2 columns, corresponding record ID and the actual data.
So, your record will be looks like
rid value
1 10
1 20
1 30
1 40
2 10
2 40
...
this way you will be able to perform update or delete operation on the array values using conventional SQL routines, as well as selecting data based on the array values.
This is how the things done oin the real world, not in PHP sandbox.
All othe answers here are plainly wrong
I would use serialize/unserialize for this. You can use it like this:
Send to MySQL
<?php
$a = array{10,20,30,40};
$a = serialize($a);
// your code here to send it to the mysql
?>
Get from MySQL
<?php
// your code here to collect it from mysql
$a = unserialize($mysql->str);
?>
The field in the MySQL should be TEXT or VARCHAR.
Regards
BlackBonjour
You can always serialize your array then store the result in a VARCHAR or TEXT field and after fetching you can unserialize the field.

Merging several MySQL results into a single JSON encode (PHP)

Here is code as exists now:
while($row=mysql_fetch_assoc($count_query_result))
$output[]=$row;
while($row=mysql_fetch_assoc($average_query_result))
$output2[]=$row;
while($row=mysql_fetch_assoc($items_query_result))
$output3[]=$row;
print(json_encode(array($output,$output2,$output3)));
mysql_close();
My question:
How do I take a single column from each of the three query results, and make a JSON array out of it, like so:
[{ 'att1' : 'data'}, { 'att2' : 'data'}, { 'att3' : 'data'}]
ASSUMING:
att1 came from the $count_query_result/$output
att2 came from the $average_query_result/$output2
att3 came from the $items_query_result/$output3
Therefore, encoding only one variable, not 3.
Well I answered my own issue. I had to get to the very root of the problem. The MySQL queries. I have joined them all so now there is just one. This creates a single JSON array for what I need. I believe there is something to be said about just doing it ... right .. the first time.
$result = array('att1' => $row['data'],
'att2' => $row['data']
echo json_encode($result)
where $row['data'] is the information that you want returned from each of your queries

Extracting values out of serialized php array in postgresql

Problem:
one column of a table contains serialized php arrays. i'd like to extract values of that serialized data structure without using php. i'd need a postgres sql statement to get those values.
Example:
here's the part of the serialized datastructure, i'd need (the bold part):
... s:12:"SearchtermID";s:4:"1008"; ....
THANKS!
This will work in your example:
SELECT substring('... s:12:"SearchtermID";s:4:"1008"; ....', 's:4:"([0-9]+)"');
See the manual here and here.
You may want to provide more details ...
This my solution:
select substring((regexp_matches(db_fieldname,'("\d+")','g'))[1] from '"(\d+)"') from db_tablename

How do i properly format a Mysqli query result in PHP? And is there an equivalent to mysql_field_table() for mysqli?

So I am writing a small PHP framework for fun, and I have come to a point where I need to format an arbitrary query result into this format:
Array(<br>
[tablename]<br>
[fieldname] => value<br>
[fieldname] => value<br>
[tablename]<br>
[fieldname] => value);<br>
<br>
If I was doing this in mysql, I would simply do it this way: $tmp[mysql_field_table][mysql_field_name] = $value. But I chose to do this project using mysqli, and I can't seem to find an equivalent for mysql_field_table() so I can determine the table for each individual value. What is the ideal method for gaining this result?
Look at the example here in the manual; the return value of the function documented is an object with a member called 'table'.
This gives you what you need.

Categories