Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
When i run my code it gives me extra square brackets in Response, and i want to get response without these SQUARE Brackets.
Respose which i am getting is below.
{"error":false,"message":"Login successfull","user":[{"u_id":"102","cust_name":"new","u_name":"user","cnic":"421","address":"sadaddd","cell_num":"43243","email":"n#gmail.com"}]}
Response which i want must be that: {"error":false,"message":"Login successfull","user":{"u_id":102,"cust_name":"new","u_name":"user","cnic":"421","address":"sadaddd","cell_num":"43243","email":"n#gmail.com"}}
My Code Section is below:
$user_login = $conn->prepare("SELECT u_id,cust_name,u_name,cnic,address,cell_num, email FROM users,cell_num WHERE u_name = :u_name AND password=:password AND users.u_id=cell_num.u_id_fk");
$user_login->execute(['u_name' => $u_name,'password'=>$password]);
if($user_login->rowCount() > 0){
$row = $user_login->fetchAll(\PDO::FETCH_ASSOC);
$response['user'] = $row;
//echo $row;
//print_r($row);
The square brackets means it is an array of data it's giving you rather than just one object.
Using fetchAll() will always return an array of rows matching the SELECT, if you know there is just one row, then you can use fetch() instead...
$row = $user_login->fetch(\PDO::FETCH_ASSOC);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to write a little webapp that will show some data. The data comes from an API. All data from the API I get displayed, but if I only want to display specific data, I always get an error.
I have not had much experience with API's yet, but all the tutorials I've done show the data via this echo method.
My current code:
<?php
$user_id = "3bb8d3bc-ab7a-45fb-8154-ed54897b2c4c";
$api_response = file_get_contents('https://r6tab.com/api/player.php?p_id='.$user_id);
$api_response_decoded = json_decode($api_response, true);
//foreach($api_response_decoded as $val) {
//echo $val, '<br>';
//}
echo $api_response_decoded->p_name;
?>
api_response Output:
"playerfound":true,"social":{"twitter":"","instagram":"","mixer":"","twitch":"","youtube":"","bio":"","esl":"","discord":"","background":"","aliases":"","embed":""},"seasonal":{"current_NA_mmr":0,"last_NA_mmr":0,"last_NA_mmrchange":0,"current_EU_mmr":3831,"last_EU_mmr":3881,"last_EU_mmrchange":-50,"current_AS_mmr":0,"last_AS_mmr":0,"last_AS_mmrchange":0,"total_casualwins":6,"total_casuallosses":5,"total_casualtotal":11,"total_casualkills":28,"total_casualdeaths":18,"total_rankedwins":38,"total_rankedlosses":24,"total_rankedtotal":62,"total_rankedkills":244,"total_rankeddeaths":201,"total_generalwins":44,"total_generallosses":29,"total_generaltotal":73,"total_generalkills":272,"total_generaldeaths":219,"total_totalbulletshits":3851,"total_totalhs":172,"total_totaltimeplayed":0,"bomb_wins":67,"bomb_losses":41,"bomb_total":108,"secure_wins":0,"secure_losses":0,"secure_total":0,"hostage_wins":1,"hostage_losses":0,"hostage_total":1,"favorite_mode":"bomb"},"matches":[{"casual_wlstatus":"won","casual_winslost":"1 Won","casual_datatime":"10\/02\/19","ranked_wlstatus":"won","ranked_winslost":"6 Won, 2 Lost","ranked_datatime":"10\/02\/19","next":"default","db_p_total_casualwins":1,"db_p_total_casuallosses":0,"db_p_total_casualkills":2,"db_p_total_casualdeaths":2,"db_p_total_rankedwins":6,"db_p_total_rankedlosses":2,"db_p_total_rankedkills":23,"db_p_total_rankeddeaths":23,"db_p_total_totalhs":10,"db_p_NA_currentmmr":0,"db_p_EU_currentmmr":3881,"db_p_AS_currentmmr":0,"NA_mmrchange":0,"EU_mmrchange":189,"AS_mmrchange":0},
My Error Message:
Trying to get property 'p_name' of non-object in C:\xampp\htdocs\R6S-Stats\stats.php on line 12
By using the true flag on json_decode, you're creating an associated array instead of an object. Remove that flag, and you'll be able to use the object methods:
$api_response_decoded = json_decode($api_response);
echo $api_response_decoded->p_name; //Scenus
If you want to keep it as an associated array, you can access the data like this:
echo $api_response_decoded['p_name'];
when use json_decode('dsfsdf', true);
$p_name = $api_response_decoded['p_name'];
when use json_decode('dsfsdf);
$p_name = $api_response_decoded->p_name;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have a very simple web app that is capturing RFID tag reads and then submits it into the Database.
I have a function that was to pass the information through a filter and remove the duplicates and then return an array of unique tag reads.
The function looks like this
$txtarea = $_POST["rfid"];
rfid($txtarea);
function rfid($txtarea){
$array = explode("\r\n", $txtarea);
$rfid_array1 = array_unique($array);
return $rfid_array1;
}
I then use Print_r to check the contents of the array to make sure it works.
When I run the code inside the function I do not get a result returned but when I run the following outside the function
$txtarea = $_POST["rfid"];
rfid($txtarea);
$array = explode("\r\n", $txtarea);
$rfid_array1 = array_unique($array);
It returns the values correctly ?
I am very new to PHP so I apologize if this question seems a little basic.
The function rfid returns a value which you could capture in a variable.
$rfid_array1 = rfid($txtarea);
Note that you could shorten the function a bit:
function rfid($txtarea){
return array_unique(explode("\r\n", $txtarea));
}
Demo on https://3v4l.org/DY8Ts
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The title is a bit confusing, but the error which I have is -
$xml="<Contacts>";
for($i=0;$i<count($results['records']);$i++){
$xml. = "<Contact>
<Name>".$results['records'][$i]['name']."</Name>";
}
$xml.="</Contacts>";
When I try to add something to string (concatenate) I get 500 Internal server error. I believe that problem is in " $results['records'][$i]['name']". I think the solution is to replace JSON value with variable and enclose it in {}...maybe I am wrong, i don't know.
UPD:
if I "echo ".$results['records'][$i]['name'].""; It works fine.
You have a string concatenation (.=) syntax error. Change
$xml. = "<Contact>
to
$xml .= "<Contact>
I'm strictly answering the question about the "string concatenation" PHP error. No downvotes for anything except this point, please. But yes, try not to generate XML manually. I closed </Contact>s for you below.
$xml="<Contacts>";
for ($i = 0; $i < count($results['records']); $i++) {
$xml .= "<Contact><Name>".$results['records'][$i]['name']."</Name></Contact>";
}
$xml.="</Contacts>";
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Trying to fetch the first URL field from an array of them that comes from a JSON I have decoded but I get this error:
Parse error: syntax error, unexpected '[' in C:\blabla
foreach($data-> images as $data2) {
print_r(images[0]['url']);
}
I hope its enough of my code to work out what I am doing wrong?
Added: I would like the first "url" and it was getting the last one hence why I am changing the code and trying to debug it here.
Within your foreach you use the variable name you specified in the definition:
So something like...
foreach($data->images as $data2) {
print_r($data2[0]['url']);
}
Although, depending on the structure of the array, I'd imagine that you don't need the number, so it might be:
foreach($data->images as $data2) {
print_r($data2['url']);
}
If you wanted to loop through the values by a number, you'd use a for loop
for ($i = 0; $i <= count($data->images); $i++)
{
print_r($data->images[$i]);
}