PHP: "->" not working on mysql resultset [closed] - php

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
I have a strange behaviour. Untilo today I was using -> to retrieve member variables from the mysql resultset, such as
$username = $result->user_name
But this is not working in my current project.
$sth = self::$dbConnection->prepare("SELECT user_id, user_name FROM users");
$sth->execute();
$result = $sth->fetch();
$test = $result->user_password_hash; // $test is null
$test = $result["user_password_hash"]; // this works
????

After fixing the parse error, if you want an object:
$result = $sth->fetch(PDO::FETCH_OBJ);
Or:
$result = $sth->fetchObject();
Or set the mode before using fetch():
$sth->setFetchMode(PDO::FETCH_OBJ);

As from PDOStatement::fetch fetch() is returning an array both indexed and associative. The arrow (->) is meant to access to object properties (or methods). To have and object instead of an array you can call fetch(PDO::FETCH_OBJ) instead.

Related

php: why is $row[0] not returning anything? [closed]

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 13 days ago.
Improve this question
I am converting all my php scripts due to moving to a new server. I am stumped as to why $row[0] is not working.
I am correctly getting $row populated as an array, and if I run a foreach on it, I get all the values populated just fine. But if, instead, I try to directly access the first value of the array as $row[0], I get nothing. Anyone know what?
$sql = "DESCRIBE USER";
$result = $dbh->query($sql);
$count=0;
while($row = $result->fetch_assoc()) {
print $row[0]; // this prints nothing
foreach($row as $column) {
print "$column"; // this works as expected
}
} #<-- while
fetch_assoc returns your results as a key value of column names and values, so you need to look at the $row['myColumn'] to get the value.

Removal of outer square brackets from a response in php [closed]

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);

How do I get individual data from an API array in PHP? [closed]

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;

Sqlite rows not outputing in php [closed]

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 4 years ago.
Improve this question
Hello i'm trying to echo all proxies from my sqlite database
Screenshot:
For some reason they dont output in the browser there are also no errors
When I run the sql command in a sqlite browser it works fine. (see screenshot)
$db = new SQLite3('proxies/http.db');
$results = $db->query('SELECT proxy FROM httpproxies LIMIT 25;');
foreach($results as $entry) {
echo $entry['proxy'];
}
I want it to echo all proxies, each on a new line.
try to use While insteadof foreach (you need fetchArray)
<?php
$db = new SQLite3('proxies/http.db');
$results = $db->query('SELECT proxy FROM httpproxies LIMIT 25;');
while ($row = $results->fetchArray()) {
var_dump($row);
}
?>

PHP array only showing last sql result [closed]

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
Hello i am hoping someone can help me, when i use print_r all only get the last result from the mysqli query, my code is below.
//Fetch data from sql results
while($row = $rs->fetch_assoc()){
//Put results in a array
$page_query=array($row['name']=>$row['system']);
}
}
you are overwriting your $page_query everytime in loop, change to:
while($row = $rs->fetch_assoc()){
//Put results in a array
$page_query[] =array($row['name']=>$row['system']);
}
You need to add it to the array - not replace the entire variable with what you have in that row.
while($row = $rs->fetch_assoc())
{
//Put results in a array
$page_query[]=array($row['name']=>$row['system']);
}
The short syntax for the function you are looking Array_push is simply to pop a set of empty square brackets behind the variable and then say =something;. This appends another element to the end of the array. This function will increment the index numerically.
because every time inside while you reinitialize $page_query so you should push them inside array to collect. use array_push()

Categories