PHP PDO Fetching Results - php

I'm trying to simply echo results out on the screen from my database. I'm quite new with PDO, and I've been searching on this site for an answer that works, but have found none so far.
$results = $MyConnection->prepare("SELECT Antwoord1, Antwoord2, Antwoord3 FROM CCOpendag ");
$results->execute();
$foundResults = $results->fetchAll(PDO::FETCH_ASSOC);
echo $foundResults;
This piece of code simply echoes out 'Array'. What I want to achieve is that I get the results from these 3 columns and display them on the screen.
If somebody could help me out, that would be amazing.

print_r($foundResults) or var_dump($foundResults)

It shows "Array" because you need to iterate over the results, and not simply just echo it out. Use var_dump or print_r and you should see the array; you can then loop out the results as you wish - var_dump will just show you the whole array.

$foundResults is an array with all the records .
You can view the array by using print_r($foundResults).
Or it will be better if you use a loop to read each rows
while($foundResults=$results>fetch(PDO::FETCH_ASSOC)) {
echo $foundResults['Antwoord1'].$foundResults['Antwoord2'];
}

Related

Display a data from an array

I just want to ask how to display the data from an array like this? Here's the link
This is my first time seeing an array like this long and I am confused. I tried getting the data from the cart_message and I get an array as a result. I'm sure this is the wrong format to get it. I am a newbie when it comes to this.
$messages['cart_message'];
Update:
Here's what I am trying to do.
I am trying to display the string on the cart_message
I want to display the text on the cart_message on the side cart template. So, to do that, I thought it will display if I use this filter
$yith_message = apply_filters( 'yith_ywpar_panel_messages_options', $messages);
echo '<pre>'; var_dump($yith_message); echo '</pre>';
The result is Null
Try:
var_export($messages);
More info: var_export

JSON PHP using API pointers

I'm trying to learn JSON and PHP using API.
My problem is that I can't get out the data of example Array 0, the output as can seen at bottom if you visit the link, just Array googled as mutch as I can but kinda new on the json array.
$json_data=json_decode($json_array,true);
print_r($json_data);
echo "<br>";
echo $json_data['total'],"<br>";
echo $json_data['skipped'],"<br>";
echo $json_data['count'],"<br>";
echo $json_data['0'],['name'],"<br>";
Based on your comment, I think I see what you want to do. You're trying to access the second level of your array incorrectly. You don't put anything between the two bracket sets. You also don't need to put the 0 in quotes, as it is not a string. I think you want this:
$json_data=json_decode($json_array,true);
print_r($json_data);
echo "<br>";
echo $json_data['total'],"<br>";
echo $json_data['skipped'],"<br>";
echo $json_data['count'],"<br>";
echo $json_data[0]['name'],"<br>"; // Line changed, removed comma and quotes.
EDIT: Note that the [0]['name'] is inside the hits value, so you'll probably want this instead:
echo $json_data['hits'][0]['name'];

SimplePie RSS not accepting variable with comma separated strings

I'm trying to use SimplePies "$feed->set_feed_url (array());" function but I am having major difficulty understanding why it wont accept my values.
When I add the URLS manually (i.e directly below), they work just fine. The feeds go through ok and displays the feeds as needed.
$feed ->set_feed_url (array(
'http://www.theverge.com/tag/rss',
'http://feeds.ign.com/ign/all'
));
I have URLS in a database table that I am pulling out like normal with a while loop. I then append a comma and remove the trailing comma so its nice for the SimplePie array. Like so:
while($row = mysqli_fetch_array($pullAllAccountsDoIt)){
$result4mdb .= $row[0] . ",";
}
$result4mdb = substr($result4mdb, 0, strlen($result4mdb) -1);
echo "the result is: " . $result4mdb;
When I do this, and echo out "$result4mdb", it prints out: the result is: http://www.gamespot.com/feeds/mashup/,http://www.theverge.com/tag/rss
Meaning that the variable is good and printing out what i need. So far so good.
I then go into the simplePie code, put in my varialble like so:
$feed ->set_feed_url (array($result4mdb));
and nothing happens. I don't get any errors or anything, just that the page stays blank and nothing comes up.
For testing, I do a gettype($result4mdb); and it tells me that the variable is a "string" and again, the output of this variable when echoed is the URLS it got from the database so I KNOW that the whole process so far is working.
For further testing, I go to the database and remove one of the URLS so that when queried, that it returns one value, and all of the sudden SimplePie works.
I've been searching for a good day and a half now, trying different things and googling as much as possible but to no avail. I just cant quite get it.
I'm at my wits end. Any assistance as to why this isn't working is GREATLY appreciated.
Thank you in advance everyone
You're just producing a string containing comma seperated values. What you need is an array, so just explode() your string:
Change:
$feed ->set_feed_url (array($result4mdb));
to
$feed ->set_feed_url (explode(',', $result4mdb));
More elegant version, change:
while($row = mysqli_fetch_array($pullAllAccountsDoIt)){
$result4mdb .= $row[0] . ",";
}
to
while($row = mysqli_fetch_array($pullAllAccountsDoIt)){
$result4mdb[] = $row[0];
}
This way you're not creating a string that needs to be exploeded, but create an array at first.

php mysql , need to take the result from a query to another page

code in page1.php (partial code)
while ($row = mysql_fetch_assoc($result))
{
echo '',($row[0]),'';
}
How can I take the value in $row[0] to page2.php?
Basically i want to use that value in page2.php to run a mysql query. Am I on the right track by using here?..
Any advice is appreciated.
Please let me know if my question is not clear.. i will try my best to word it better.
thanks for the help.
One possible way would be:
while ($row = mysql_fetch_assoc($result))
{
echo 'Link Text';
}
Then on the second page, you can access that variable using $_GET['value1']
In this example, you are taking values out of the database and sticking them in the query string. Just be sure you don't do it the other way around without sanitizing them!
To do two, do it like this:
echo 'Link Text';

php json decode blank page

Hello I want to decode json. My code below:
<?php
$json = '{"response":{"count":1,"items":[{"id":165983743,"owner_id":170785079,"title":"Ke$ha - Blow","duration":253,"description":"","date":1379017507,"views":1,"comments":0,"photo_130":"http:\/\/cs518121.vk.me\/u170785079\/video\/s_5e5f6f2c.jpg","photo_320":"http:\/\/cs518121.vk.me\/u170785079\/video\/l_dd4ec237.jpg","files":{"mp4_240":"http:\/\/cs518121v4.vk.me\/u170785079\/videos\/500770e51c.240.mp4","mp4_360":"http:\/\/cs518121v4.vk.me\/u170785079\/videos\/500770e51c.360.mp4","mp4_480":"http:\/\/cs1-46v4.vk.me\/p13\/483502b20c4f.480.mp4","mp4_720":"http:\/\/cs518121v4.vk.me\/u170785079\/videos\/500770e51c.720.mp4"},"player":"http:\/\/vk.com\/video_ext.php?oid=170785079&id=165983743&hash=1e417a266e9a3f00"}]}}';
$obj = json_decode($json);
print_r ($obj);
print $obj->{'response'}->{'items'}->{'files'}->{'mp4_240'};
But I get a blank page
print_r should actually print something - your json is correct.
You should do it like this:
print $obj->response->items[0]->files->mp4_240;
Here's a code working on ideone: http://ideone.com/4xXfOl
EDIT: Please whoever downvoted these answers, explain why you do so in comments...
at first u must enable display errors at yours php interpreter
ini_set('display_errors',1);
error_reporting(E_ALL);
and then u have to read more intently the structure of json which u want to travers, the
items as an array, actually the object keys says it to you: the plural form of item
so the solve is:
print $obj->{'response'}->{'items'}[0]->{'files'}->{'mp4_240'};
of course I dislike such syntax, it would be better using
print $obj->response->items[0]->files->mp4_240;
use $obj->{'prop_name'} form when the programm selects accessing attributes dynamicly
I think you need this
echo $obj->response->items[0]->files->mp4_240;
instead of print $obj->{'response'}->{'items'}->{'files'}->{'mp4_240'};

Categories