PHP Accessing property containing * - php

i am returned an array that looks like this:
array(1) { [0]=> object(stdClass)#176 (1) { ["COUNT(*)"]=> string(1) "1" } }
when var_dump() is executed.
So how would i acceess the count object?
Its
$result[0]->COUNT(*)
but this results in an error?
$result[0]->{COUNT(*)}
does not help either.

Use brackets:
echo $data->{'COUNT(*)'};
But it's about SQL and I recommend to give an alias to your field, like
SELECT COUNT(*) AS records_count FROM t

Related

How do I print Attribute Values of an object(stdClass) in PHP

I'm having a difficult time understanding how to print out an attribute value of an object. The particular example I am working from is this:
object(SimpleXMLElement)#1 (1) {
["links"]=>
object(SimpleXMLElement)#4 (2) {
["#attributes"]=>
array(3) {
["total-matched"]=>
string(2) "31"
["records-returned"]=>
string(2) "10"
["page-number"]=>
string(1) "3"
}
I want to print the value of the links total-matched (which is 31). I've tried this: echo $object->links->total-matched; but I only get the value of 0.
How can I do this?
$object->links->total-matched evaluates as $object->link->total - matched (- is minus, I suppose you should see warning about using unknown constant - turn on error reporting). To access attributes with names like this you can do following: $object->links->{'total-matched'} although in this case, since it's SimpleXML attribute, I think you need to get attributes array:
$attr = $object->links->attributes();
echo $attr['total-matched'];

Selecting item from multi-dimensional array

I have an array with the following contents:
$tester
array(1) {
[0]=>
object(CategoryItem)#79 (17) {
["type"]=>
string(0) ""
["addedInVersion"]=>
string(4) "0.02"
["lastUpdatedInVersion"]=>
string(4) "0.02"
["AToZ"]=>
bool(false)
["name"]=>
string(22) "Page Name"
["scopeNotes"]=>
string(0) ""
["historyNotes"]=>
string(13) "Added in 0.02"
["broaderItems"]=>
array(0) {
}
I want to echo out the name, if this case Page Name and then use this in an if statement.
I have but this errors, I also tried $tester->CategoryItem->name but no joy.
Is there anything obvious I am missing?
You need to access it like this:
$name = $tester[0]->name;
echo $name;
you have some leaks in your php OOP understanding, you should fix them by following some tutorials like these ones:
http://www.killerphp.com/tutorials/object-oriented-php/
http://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--net-12762
http://www.tutorialspoint.com/php/php_object_oriented.htm
Now to answer your question, your code should be this:
$the_name = $tester[0]->name;
if($the_name == 'whatever value you want') {
echo $the_name;
}
first of all, your initial variable is a array, therefor, $tester[0], then, this position is an object of the class CategoryItem so you use scope: $tester[0]->value
As for the last of your values in the class properties, broaderItems, this is again an array, so to access one of his values, you will have to call it like:
$tester[0]->broaderItems[0]; //or whatever the keys you will have here
Hope this helps!
:D

Zend\Sql\TableGateway|Where|Predicate does not replace the string in the where condition - Is this a bug?

I put together a small application using Zf2 based on the tutorial and where I would like to get data from the database I had to face a strange issue.
I have this method in my TableGateway class:
public function selectWith($select = null) {
$filter = new Predicate();
$filter->like('category_name',"%dess%")
->OR
->like('category_desc', "%ks%");
var_dump($this->tableGateway->select(function(Select $select) use ($filter) {
$select->where($filter);
}));
return $this->tableGateway->select(function(Select $select) use ($filter) {
$select->where($filter);
});
}
According to the dump the following query was executed:
SELECT `categories`.* FROM `categories` WHERE (`category_name` LIKE :where1 OR `category_desc` LIKE :where2)
Instead of this which should be formalized by the source code:
SELECT `categories`.* FROM `categories` WHERE (`category_name` LIKE '%dess%' OR `category_desc` LIKE '%ks%')
Is this a bug, or did I wrong something?
According to the API the Like accepts only two parameters, both are string.
I use ZF 2.2.2
Thanks for any help in advance!
No, this is not a bug. Please check below how it works:<br />
Before viewing the vardump of select statement
var_dump($this->tableGateway->select(function(Select $select) use ($filter) {
$select->where($filter);
}));
Please perform the var_dump of $filter. Where you will find the array of like statements.
example:
["predicates":protected]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(3) "AND"
[1]=>
object(Zend\Db\Sql\Predicate\Like)#377 (3) {
["specification":protected]=>
string(14) "%1$s LIKE %2$s"
["identifier":protected]=>
string(12) "contact_name"
["like":protected]=>
string(4) "%dess%"
}
}
[1]=>
array(2) {
[0]=>
string(2) "OR"
[1]=>
object(Zend\Db\Sql\Predicate\Like)#378 (3) {
["specification":protected]=>
string(14) "%1$s LIKE %2$s"
["identifier":protected]=>
string(13) "business_name"
["like":protected]=>
string(4) "%ks%"
}
}
}
}
These two array's ["like":protected] portion are replaced with "where1" and "where2" respectively when the query is being executed. <br />
Thanks :)

Only get field names from MySQL

Right now when I run the following query SELECT * FROM table I get the following response
array(1) {
[0]=>
array(36) {
[0]=>
string(5) "31764"
["id"]=>
string(5) "31764"
...
}
...
}
As you can see I am getting 2 of the same data ("0" and "id"). Is there any way I can only get "id" and not "0"?
Check the documentation of mysql_fetch_array(), the second argument allows you to specify what kind of array to return:
The type of array that is to be fetched. It's a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH.
Or you could just use mysql_fetch_assoc().
you can write the sql just like :
SELECT 'id' FROM table
it wil just return

How do I get my values out of their SimpleXMLObjects?

After an xPath, I'm left with this var_dump:
array(1) {
[0]=>
object(SimpleXMLElement)#2 (1) {
[0]=>
string(11) "22-99586795"
}
}
echoing the damn thing only gives me "Array()"
How do I get the bloody string out?
Thanks
It's an array with one item, so you need to do:
$myelement[0];
or
$myelement[0][0];
(I can't tell from your question which element you're referring to)
Try casting it to string
print (string)$yourarray[0];

Categories