Suppose I have following data :
a:1:{s:4:"date";a:3:{s:2:"mm";s:2:"12";s:2:"dd";s:2:"06";s:2:"yy";s:4:"1991";}}
I want the value of mm and dd from that serialized string.
I have following query to retrieve above data :
$birthday = "select meta_key, meta_value from $wpdb->usermeta where meta_key='pie_date_5'";
$user_birthday = $wpdb->get_results($birthday);
foreach($user_birthday as $ubday){
$ubd = $ubday->meta_value; echo $ubd;
echo json_decode($ubd[0]->mm);
}
I am not able to do that simple thing. Please help me out.
You have to use unserialize instead of json_decode because it's a serialized string.
So you could try something like this:
$user_birthday = 'a:1:{s:4:"date";a:3:{s:2:"mm";s:2:"12";s:2:"dd";s:2:"06";s:2:"yy";s:4:"1991";}}'; //the serialized string you get from your query
$upday = unserialize( $user_birthday );
echo $upday['date']['mm'] . PHP_EOL;
echo $upday['date']['dd'];
EDIT: If you want to read all properties from the $upday array you could use a foreach-loop
foreach ( $upday['date'] as $key => $value ) :
echo $key . ' = ' . $value . PHP_EOL;
endforeach;
Related
i have try to print json response but "" are added to my json response
i tried below code php and id generated by drupal field
<?php
$data = array("title"=>"test","body"=>"test body");
$php = json_encode($data,JSON_FORCE_OBJECT);
echo json_encode(array("php"=>$php,"id"=>10));
?>
output :
{"php":"{\"title\":\"test\",\"body\":\"test body\"}","id":10}
but i want output like below
{"php":{"title":"test","body":"test body"},"id":10}
i added some more code for above problem
{"php":"{\"title\":\"test\",\"body\":\"test body\"}","id":10}
why not remove json_encode from echo json_encode($php);
how i can get above output` second time
You're encoding in JSON two times :
$data = array("title"=>"test","body"=>"test body");
$php = json_encode($data,JSON_FORCE_OBJECT);
echo $php ; // remove json_encode() here
After question edit:
$data = array("title"=>"test","body"=>"test body");
echo json_encode(array("php"=>$data,"id"=>10));
You can also simply do this,
<?php
$data = array("title"=>"test","body"=>"test body");
echo json_encode(array("php"=>$data,"id"=>10));
?>
I'm trying to build a members-system in which you can view certain values from a database.
It worked, so that was great, but I'm having a problem now because I'm using a NULL value on 'age'.
When viewing a profile page, it looks like this:
$id = $_GET["id"];
$result = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember`=$_GET[id]");
$row = mysql_fetch_array($result);
echo "<b>" . $row['userusername'] . "</b>: </p>"; ?>
<?php
$id = $_GET["id"];
$result = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember`=$_GET[id]");
$noage = mysql_query("SELECT ISNULL([age]) FROM membersfromsite WHERE `idofmember`=$_GET[id]");
while ($row = mysql_fetch_array($result))
{
echo "<p class='middle'>id-number: " . $row['idofmember'] . "<br>";
echo "Username: " . $row['userusername'] . "<br>";
if ($noage)
{
echo "Age not specified";
}
else
{
echo "Age: " .$row['age'] ;
}
}
I have tried all kinds of other things, but the problem which I 'm having is that it either returns 'Age not specified' on every userpage or the age on every userpage, including the pages with a NULL value, which makes it look like:
Age:
The code which you can see above returns the age on every page, including the pages with an age which is set to NULL. What I don't understand is if I change the code to this:
$noage = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember`=$_GET[id] AND age IS NULL");
it simply doesn't work. Since I'm using IS NULL instead of = NULL I don't really see why this shouldn't work, but I guess it has to do with the IF which is inside the 'while' thing, I don't really see in what other way I could fix this though...
I'm having an idea what the problem is, because I think that there is already a MyQS, Query done with Results and $noage is maybe ignored because of this, but I don't know how to solve this.
You don't need to do a whole separate $noage query.
Just do:
if(!$row['age'])
{
echo "Age not specified";
}
else
{
echo "Age: " .$row['age'] ;
}
Instead of if($noage) use if(!$row['age']) and skip the second query.
The other reason your code does not work is that the second query returns an array with something like array('expr1' => 0) which is not false. You only get a false result if nothing is found.
The reason why = NULL does not work? There are books written about it, it is just as it is.
Rather than relying on MySQL to tell us if no age was found or not, we can programatically determine whether the age value is Null right in PHP.
Here is an example:
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember` = '" . mysql_real_escape_string($id) . "'");
while($row = mysql_fetch_array($result)) {
echo '<p class='middle'>id-number: ' . $row['idofmember'] . '<br>';
echo 'Username: ' . $row['userusername'] . '<br>';
if($row['age'] === Null) {
echo "Age not specified";
} else {
echo "Age: " .$row['age'] ;
}
}
I am trying to extract specific values returned from google api
<?php
$url='http://maps.googleapis.com/maps/api/distancematrix/json?origins=19.1629924,72.83930190&destinations=19.1802370,72.8554149&sensor=false';
$contents = file_get_contents($url);
$contents = utf8_encode($contents);
$array=json_decode($contents);
echo $array['rows']['elements']['distance']['value'];
?>
I expected the value of 4238 to be returned. Instead I get an error:
`PHP Fatal error: Cannot use object of type stdClass as array in test9.php on` line 7
Te correct solution is:
echo $array->rows[0]->elements[0]->distance->value;
Do the following to see the json:
<?php
$url='http://maps.googleapis.com/maps/api/distancematrix/json?origins=19.1629924,72.83930190&destinations=19.1802370,72.8554149&sensor=false';
$contents = file_get_contents($url);
$contents = utf8_encode($contents);
echo "<pre>";
echo $contents;
echo "\n";
echo "\n";
$array=json_decode($contents);
echo $array->rows[0]->elements[0]->distance->value;
?>
http://www.json.org/
have you tried node.js?
The following code solved the problem:
$array=json_decode($contents, true);
echo $array['rows'][0]['elements'][0]['distance']['value'];
I have wp_places custom table and I am getting this when I am printing array:
[0] => stdClass Object
(
[home_location] => 24
)
[1] => stdClass Object
(
[home_location] => 29
)
Now I want to implode value like this way (24,29) but in my code I am getting this error:
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
My Code
$getGroupType = $_POST['parent_category'];
$result = $wpdb->get_results( "SELECT home_location FROM wp_places WHERE blood_group LIKE '".$getGroupType."%'" );
$bgroup = Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$bgroup[] = implode(',',$row);
}
echo implode(',',$bgroup);
Any ideas or suggestions? Thanks.
$wpdb->get_results() already do the fetching for you, you don't need to call mysql_fetch_array
Given what you want to do, your code should look like this :
$getGroupType = $_POST['parent_category'];
$result = $wpdb->get_results( "SELECT home_location FROM wp_places WHERE blood_group LIKE '".$getGroupType."%'" );
$bgroup = Array();
foreach ($result as $location) {
$bgroup[] = $location->home_location;
}
echo '('.implode(',',$bgroup).')';
It's an PHP object that contains results, it isn't a MySQL Result.
Looking at the docs, it should be used like
foreach ($result as $row) {
$bgroup[] = $row->home_location;
}
echo implode(',',$bgroup)
I'm having trouble understanding the documentation for the UPDATE command of MYSQL. I am viewing records in a PHP page from the database and I want to edit them.
To INSERT I have this code which is an array. I want to know if I can do the same with the UPDATE statement, to save me lots of this=$this 's.
Insert
mysql_query("INSERT INTO $tbl_name(title, pbDate, summary, blog) VALUES('$title', 'pbDate', '$summary', '$blog')")or die(mysql_error());
Update
mysql_query("UPDATE $tbl_name SET title='$title', pbDate='$pbDate' summary='$summary' blog='$blog' WHERE id='$id'")
I thinking something like this, but I'm not sure and can't find anything in the manual.
mysql_query("UPDATE $tbl_name SET (title, pbDate, summary, blog) VALUES('$title', 'pbDate', '$summary', '$blog') WHERE id='$id'")
You could use an array ...
Working phpFiddle:
http://phpfiddle.org/main/code/pi9-ckh
<?php
$array = array(
"column" => "some_value",
"title" => "some_title",
);
$setString = '';
foreach($array as $key => $value){
$tempArray[] = $key . ' = ' . "\"" . $value . "\"";
}
$setString = implode(",", $tempArray);
echo $setString;
?>