PHP - Pdo Returning Single String Not Associative Array.
Hi guys,
I am using this oAuth library https://github.com/elbunce/oauth2-php But more specifically these lines of code: protected function getToken($token, $isRefresh = true) { try { $tableName = $isRefresh ? self::TABLE_REFRESH : self::TABLE_TOKENS; $tokenName = $isRefresh ? 'refresh_token' : 'oauth_token'; $sql = "SELECT $tokenName, client_id, expires, scope, user_id FROM $tableName WHERE token = :token"; $stmt = $this->db->prepare($sql); $stmt->bindParam(':token', $token, PDO::PARAM_STR); $stmt->execute(); $result = $stmt->fetch(PDO::FETCH_ASSOC); return $result !== FALSE ? $result : NULL; } catch (PDOException $e) { $this->handleException($e); } } However, this is retuning no value. If I modify the code to be: protected function getToken($token, $isRefresh = true) { return $token; } It returns the $token value, so the $token is definitely being passed to the function. The code should return an associative array: i.e., $token["expires"], $token["client_id"], $token["userd_id"], $token["scope"], etc Also $token should not === NULL. PS. I run a few checks on $tableName = $isRefresh ? self::TABLE_REFRESH : self::TABLE_TOKENS; $tokenName = $isRefresh ? 'refresh_token' : 'oauth_token'; And they are returning the correct values. Which from my thinking narrows it down to: $sql = "SELECT $tokenName, client_id, expires, scope, user_id FROM $tableName WHERE token = :token"; $stmt = $this->db->prepare($sql); $stmt->bindParam(':token', $token, PDO::PARAM_STR); $stmt->execute(); $result = $stmt->fetch(PDO::FETCH_ASSOC); return $result !== FALSE ? $result : NULL; Any and all help is greatly appreciated. Thanks in advance. Similar TutorialsI came up with this but it outputs the value 3 times... (because there are 3 variables) ie mathsmathsmaths How do I fix this or do it the correct way? Thanks Code: [Select] $subject = "maths"; $phone = "1235"; $colour = "red"; $all_vars = compact("subject", "phone", "colour"); get_value($subject); function get_value($variable){ global $all_vars; foreach($all_vars as $key=>$value){ if ($key = $variable) { echo $key; } } } Hi Guys, I have 6 file upload fileds in an array. When i try to return the error messages at this link: http://www.php.net/manual/en/features.file-upload.errors.php i get an error displayed for every upload box. I was wondering if it is possible to simply return 1 error instead of the 6? For example rather than getting "No File Uploaded No File Uploaded No File Uploaded No File Uploaded No File Uploaded No File Uploaded" i simply get "No File Uploaded". Also If only some files are uploaded no error should be displayed. The code i am using is: foreach ($_FILES['image']['error'] as $key => $error) { if ($error == UPLOAD_ERR_NO_FILE) { echo "No File Uploaded"; } I have tried using "break;" which works in leaving only 1 error, however the error is still displayed if less than 6 files are uploaded. All help greatly appreciated. Thanks Hi I have a simple form and when the user submits, php is putting a \ before every single quote entered in the field. So for example, if a user enters O'Neill, once i do $lastName = $_POST["lastname"]; $lastName comes back as: O\'Neill is there some way I can turn this off? Hi there, Basic question, but I can't seem to find a page in the PHP docs that answers (though I'm sure such a one exists, I just can't seem to think up the right search terms - anyway) My question is: is there a short-hand way to use a value from an associative array as the key in another associative array? So for example, given this array: $the_array = array(0 => array('name': 'the_name', 'value' : 'the_value'), 1 => etc....); then this kind of foreach loop doesn't work Code: [Select] $new_data = array(); foreach($the_array as $element){ $new_data[$element['name']] = $element['value']; } Obviously I can just save out the values, like $name = $element['name'] --- but I was just wondering if there was a more efficient way to do it? Cheers, PS. I tried encapsulating with {} - i.e. $new_data[{$element['name']}] --- but that also fails. I have a associative array and I need to compare it to a normal array. Code: [Select] associative will have anywhere from 2-8 of these ['name'] ['color'] ['uid'] Code: [Select] normal array will have anywhere from 1-8 names ['name'] What I need to do is get the name of the people that are in the associative array that are not in the normal array. I've tried all sorts of different loops and what not and I cant seam to get anything to work in every case. I WANT HELP WITH ASSOCIATIVE ARRAY THERE IS ARRAY LIKE THIS $ARRAY= array( 'dd'=>1, 'dde'=>233, 'qww'=2231 ) i want to store this array in database which data type i should i use want to use mixed data mixed string-numeric type which data type is suitable and i want to print indexes only but values only what should i do Hello, I am not sure if you can help me or if this is the place but... I am running linux and php5.3.6 I am having problems that I did not experience on my Windows box and it has to do with arrays if ($answer['correct']) { (which contains 1 or 0) is throwing Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING removing the quotes works but just curious why it is not working and if there is a setting i can change Also $answers[] = $answer[id]; is throwing Parse error: syntax error, unexpected ']', expecting T_STRING or T_VARIABLE or T_NUM_STRING Thank you to anyone who might be able to help. Hi guys. I have an associative array called $VisitorCountryID $VisitorCountryID = filter_countries(array('Country' => $_SESSION['UserCountryName'])); and the function filter_countries() returns all the information relating to the visitors country from the database. To find out what we have in the array, we do: print_r($VisitorCountryID); and it gives us: Array ( [14] => Array ( [CountryId] => 14 [Country] => Australia [FIPS104] => AS [ISO2] => AU [ISO3] => AUS [ISON] => 36 [Internet] => AU [Capital] => Canberra [Comment] => ISO includes Ashmore and Cartier Islands,Coral Sea Islands [sortOrder] => 1 ) ) Which is OK. Now i just want the CountryId which you can see in the array is 14 therefore I write: print_r($VisitorCountryID['CountryId']); Which should give me 14 but it return blank no output. I am surprised. What do you think is missing here. Any response / feedback always welcomed! Cheers! Hello all!! I need help with an associative array I've been working on. I'm not sure if I should have typed the weekday in the index because I'm supposed to use the getdate() function to pull the day of the week into the array. I'm also supposed to use string functions to pull apart the value where it's connected with the colon ":". I've tried using explode() and strtok but I don't quite understand how they work...just like the getdate()..I don't understand. Thanks for any 'nudge' in the right direction you can offer. Code: [Select] <?php $title = "Associative Array"; $heading = "Daily Specials"; $specials = array( "Sunday" =>"52in Flat Screen TV:425.00", "Monday" =>"Amplifier:145.00", "Tuesday" =>"HP Computer:355.00", "Wednesday" =>"500GB External Harddrive:99.00", "Thursday" =>"Internal Speakers:55.00", "Friday" =>"Ergonomic Keyboard:85.00", "Saturday" =>"Wacom Tablet:175.00", ); echo "<html> <head> <title> $title </title> </head> <body> <h1> $heading </h1> <pre>\n"; //prints left and right aligned headers with 30 spaces printf ("%-30s%30s\n", "Items", "Price"); printf ("%'-60s\n", ""); foreach ($specials as $key=>$value); { //prints the key left aligned and the value right aligned //formats to two decimals spaces printf ("%-20s%20.2f\n", $key, $value); echo " </pre>\n"; $delimeter=":"; $inventory=strtok($inventory,$delimeter); while(is_string($inventory)) { if($inventory) { echo " $inventory\n"; } $inventory=strtok($delimiter); } $inventory=explode($delimeter,$inventory); foreach ($items as $item=>$price) { echo" $item, $price\n"; } echo " </pre>\n"; "</body> </html>\n"; ?> Hi guys, I'm really struggling trying toremove duplicates from an associative array. I've tried array_unique but it doesn't work. Can you point me in the right direction please? I'm populating my array using the following code Code: [Select] foreach($category['manufacturers'] as $manufacturer) { list($name, $id) = explode("|", $manufacturer); $brands[] = array("id" => $id, "name" => $name); } If the array contains: id = 1, name = brandA id = 2, name = brandB id = 1, name = brandA I'd like to remove either the first or third entry. I always struggle with associative arrays Hello I have been taking a look at associative arrays. I 'sort' of get the key/value idea, but I'm not quite sure if they appropriate for use with 3 items (as opposed to the 2 key/value items.) In the code below, I can incorporate the 'company' and 'table' values - but I'm not sure how to add the 'id' value (from the query). I would very much appreciate some guidance here. Thank you. Code: [Select] $a = array(); // 'A' table array $b = array(); // 'B' table array $c = array(); // 'C' table array $query = "SELECT id, company, `table` FROM EXHIBITORS WHERE year = $year ORDER BY company ASC"; $result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result)) { $testCol = substr($row['table'], 0, 1); if ($testCol == 'A') { $a[$row['company']] = $row['table']; } else if ($testCol == 'B') { $b[$row['company']] = $row['table']; } else if ($testCol == 'C') { $c[$row['company']] = $row['table']; } } echo "<table>"; foreach($a as $key => $value){ echo "<tr><td>company: $key</td><td>Table: $value </td></tr>"; } echo "</table>"; echo "<table>"; foreach($b as $key => $value){ echo "<tr><td>company: $key</td><td'>Table: $value </td></tr>"; } echo "</table>"; echo "<table>"; foreach($c as $key => $value){ echo "<tr>"<td>company: $key</td><td>Table: $value </td></tr>"; } echo "</table>"; } else { echo "No Exhibitor data for the year $_POST[selectByYear]"; } I'm scraping a housing website with html source: Code: [Select] <tr><td> </td><td> Bedrooms </td><td> </td><td >3</td><td> </td></tr> <tr><td> </td><td> Full Baths </td><td> </td><td >1</td><td> </td></tr> <tr><td> </td><td> Partial Baths </td><td> </td><td >1</td><td> </td></tr> <tr><td> </td><td> Interior Sq Ft </td><td> </td><td >1,356</td><td> </td></tr> <tr><td> </td><td> Acres </td><td> </td><td >0.16</td><td> </td></tr> What i'm trying to do is create an associative array with only the details I want from the above. I've already got a function that steps through the HTML and creates an array element for each row with the contents of each <td> statement as follows: Code: [Select] $repl = array(" ", "(", ")"); $repl_with = array("", "", ""); foreach ($rows_feat as $id2 => $row2){ $features_raw = $scrape->fetchAllBetween('<td','</td>',$row2,true); //$features_data[str_replace($repl,$repl_with,strtolower(trim($features_raw[1])))] = trim($features_raw[3]); $countid=$countid+1; } The array produced by this would look like: features_raw[1] = Bedrooms features_raw[3] = 1 Basically, I want to take data like this: Beds 1 Baths 1 Garage Yes and create an array on the fly: House[beds]=1 House[baths]=1 House[garage]="yes" The problem is that the order of Beds, Baths and Garage changes. So on some house pages the order would be Baths, Garage, Beds.. etc. Any pointers out there? Thanks, Brett i need some help sorting my arrays. i have a class that selects data from database, and holds it inside a class to be used multiple times. rather then executing multiple queries. i know how to sort an array. but i am sorting arrays inside an array $data = arrray( array( 'id'=>1, '_name'=>'Server A', '_date_added'=>1298207645.5424, ); array( 'id'=>2, '_name'=>'Server B', '_date_added'=>1298247384.4839, ); array( 'id'=>3, '_name'=>'Server C', '_date_added'=>1298218493.0493, ); ); i want to sort my array descending by key _date_added hope you understand... Hi there! Ok I've a very simple code he $character = array (name=>"Joe", occupation=>"Programmer", age=>30, "Learned language "=>"Java" ); print_r($character); echo "<br/>"; foreach ( $character as $key=>$val ){ print "$key = $val <br>"; } The output is: Code: [Select] Array ( [name] => Joe [occupation] => Programmer [age] => 30 [Learned language ] => Java ) name = Joe occupation = Programmer age = 30 Learned language = Java Now instead of returning all the other elements, I just need to display only the first element in the array that is the value 'Joe'. I tried $val[name]; Output I get is not what I want: Code: [Select] J P J All responses/feedbacks is always welcomed Thank you! i have problem to get the sum from the associative array. i have to get sum for the 3 result that being display from the array
rsort($sub); }
<HTML> <?php $arr = array("sue" => "betsy", "frank" => "marge"); ?> </HTML> This produces the following HTML page: "betsy", "frank" => "marge"); Its like when I use the => to make my associative array, the code thinks I'm closing the php codeblock and then just prints the rest of it to the screen...
This is the cart array: I want the key to be "id" and the value to be "quantity" like it is for the cart array. How do I define my cart_items array using variables? Hi guys
A mobile application send data in Json format, so I want to insert the data into Mysql table, when I try to insert I have gotten the error "PHP Warning: mysqli::prepare() expects exactly 1 parameter, 3 given", my table only has 2 columns, "id" and "name" column.
The array has this data
( [0] => ('id', '2') [1] => ('name', 'Jhon') )The function to parse and insert data ... function redeem() { // Check for required parameters $json = file_get_contents('php://input'); $obj = json_decode($json,true); // Assumes $obj == array(0 => $assocArray, 1 => $assocArray) foreach($obj as $index => $assocArray) { // Assumes $assocArray == array(0 => array('id' => '2'), 1 => array('name' => 'John')) $stmt = $this->db->prepare('INSERT INTO prueba (id,nombre) VALUES (%d,%s)',$assocArray[0]['id'],$assocArray[1]['name']) or die(mysqli_error($this->db)); $stmt->execute(); } } Edited by Maq, 06 June 2014 - 03:54 PM. I am an ASP.NET developer and I have to call a soap methed written in PBP that takes an argument required an associative array such as $prices = array( 'Tires'=>'test1', 'Spark Plugs'=>'test2' ); can yoiu tell me what the internal representation is of the above this is my solution:
$x = ['a' => 2, 'b' => 1, 'c' => 4, 'd' => 1]; $x1 = array_keys($x); $x2 = array_values($x); $z = []; for ($i=0; $i<count($x); $i++) { array_push($z, [$x1[$i],$x2[$i]] ); } print_r($z); //[['a', 2], ['b', 1], ['c', 4], ['d', 1]];is there easier (more elegant) way to do the same ? |