Selecting only the first item of an xpath result set in PHP - php

I am currently achieving the desired outcome with two PHP statements:
$thisBlarg = $xmlResource->xpath('//blarg[#ID='.$someBlargID.']');
echo $thisBlarg[0]->name;
But, not wanting to settle for second best, I'd really prefer this to be one statement, but PHP doesn't like this:
echo $xmlResource->xpath('//blarg[#ID='.$someBlargID.']')[0]->name;
And for good reason. But I can't find a way to force an xpath query to return the result directly. Any suggestions?

Try this
echo current(($xmlResource->xpath('//blarg[#ID='.$someBlargID.']')))->name;

Related

Why use always while loops to fetch mysql results?

The funny thing is that I can't find a good Explanation for this question.
Is there any disadvantage when I don't use a "while-loop" to fetch a mysql result?
Everywhere I see this line of code:
while ($row = mysql_fetch_array($result)) {
// do something with $row
}
I'm not sure that I have ever seen code that used a foreach-loop to fetch mysql results.
So is there anything wrong with other Loops or is the while-loop the only Loop that is easy and simple to use for it?
I guess there is no performance problem for regular uses with while-loops or foreach-loops with 100-1000 resultsets, isn't it?
I guess the answer is that :
"Foreach" is used when we want to loop through a code for each element in an array.(It handles each element inan array)
"For" is used when we want to loop through a block of code a specified number of times.
"While" : is used to loop through a block of code as long as a special condition is true .
I hope everything is clear for more info you may visit this website : http://www.tutorialspoint.com/php/php_loop_types.htm

how to show the result of a query on my website

so I want to show the result of my query, and I can't seem to find anything that works.
this is what I have so far and I have no idea why it won't work.
daydream festival <?php
$queryday="SELECT `tickets_MAX`-`tickets_VERKOCHT` AS `tickets_over` FOR `evenementdata` WHERE `ev id`=1";
$resultday=mysql_query($queryday);
$rowday = mysql_fetch_assoc($resultday);
echo $rowday;?> tickets left
Building on other reasons why this won't print anything like your rows not existing, or misnaming things.
If I'm not mistaken mysql_fetch_assoc returns an array, echo does not print arrays. You need to use print_r($rowday); - Assuming that $rowday contains data.
I'm making a few assumptions here, but I think it's likely that you should do it like:
<?php
$queryday="SELECT `tickets_MAX`-`tickets_VERKOCHT` AS `tickets_over` FROM `evenementdata` WHERE `ev_id`=1";
$resultday=mysql_query($queryday);
$rowday = mysql_fetch_assoc($resultday);
print_r($rowday);
?>
That being said, I would strongly recommend not using the mysql_ functions, since they are now being depreciated. You should do some research into The PHP Data Objects (PDO).

PhP Using echo to print out multiple variables with ),(,: in between

I've been looking everywhere about text formatting with echo but I couldn't find a simple answer on how do I echo my variables like this: field1(field2,field3,field4): field5. Without using $row['field'], or maybe it would be even easier that way?
Thanks!
Your question is not very clear. Are you trying to echo an array ?
If so check the print_r function (http://www.php.net/print_r‎).
print_r($your_array_here)
Or you are trying to join the variables ? (http://www.php.net/implode)
echo implode(',', array($variable1, $variable2, $variable3));
I dont think there is a way to do this (if you are really talking about data right from the DB) as you will always have an array and you have to access that array. Post what is it you are trying to achieve, perhaps there is a faster way.

Echoing string from a mysql query - PHP

How do I echo a simple string from a MySQL Query?
I'm trying trying to accomplish this with the following code but it is not working...The data I am pulling is fine so I know that my mysql_query is working (I've checked that via a different URL GET method.
<?php
$myQuery = mysql_query("fetch some stuff....");
$myResult = mysql_fetch_object($myQuery);
echo $myResult;
you need to know what is returned type. in what your doing you assume that it printable but most of what db queries return are either in object form or an array
try doing a
echo "<pre>" ,print_r($myResult, TRUE),"</pre>";
First of all use var_dump($myResult) to see the data and it's structure.
Since it's an object it will have properties named as the columns returned by the SELECT statement you used.
echo $myResult->column_name; // Should work fine
Usually if echo $variable; doesn't work it means that the variable is either en empty string '' or a null value NULL or a false value FALSE which all show "nothing" when echoed.
But when using var_dump() on them you get a report of the type of data and size of it.
Providing your query is correct, it looks like your php tags are incorrect:
<?php ?>
P.S. It might help if you post the actual query so it can be troubleshooted here. It's hard to ask why something is not working and get an answer if you don't show any of it.
First, var_dump($myResult);. If you see NULL, your query is failing. If you see a big block o' jumbled text, the query is, in fact, working. Since you are echoing $myResult, it is no surprise that nothing is being output, as you are trying to echo the object directly rather than the property you want. Try echoing $myResult->myColumn;
Also, please use MySQLi or PDO, as php_mysql is deprecated.

Using Same Variable in Multiple PHP `while` Loops

I have a PHP page with multiple mysql_query instances. Almost every one uses a while loop to retrieve multiple rows of data.
As I keep a snippet of this often used code, and the example I was taught with, used the variable $row, I have multiple instances of the following on my page:
while ($row = mysql_fetch_assoc($foo_data)) {
$barArray[] = $row['barValue'];
}
I even have an instance of $row = mysql_fetch_assoc($foo_data) without a while loop.
I'm wondering if the multiple uses of $row as a variable on a single page, is all right?
The PHP is functioning fine, but I always want to be sure that my code is proper and conforms to standard rules.
Thank you.
yes, it's all right.
it's all right to use the same spoon when you're eating soup.
Yes that is fine. but if you try:
var_dump($row);
at the end of your code -- it will only return that last value of $row
Yup, not a problem at all.
Just like always using $i as your count in a for loop, you can repeatedly use $row to no ill effect.

Categories