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);
Related
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.
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]);
}
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()
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 9 years ago.
Improve this question
Im trying to json_decode the following:
"main":{
"temp":9.04,
"temp_min":9.04,
"temp_max":9.04,
"pressure":938.13,
"sea_level":1037.57,
"grnd_level":938.13,
"humidity":87
},
"weather":[
{
"id":801,
"main":"Clouds",
"description":"few clouds",
"icon":"02d"
}
],
$result = json_decode($json);
$temp = $result->main->temp; //is displaying the data
however
$id = $result->weather->id; //is not displaying anything
all i can see is difference that te second one have an extra "[]"
can you help me telling how can i get weather->id from that json, thank you
The weather element is an array: it contains a list of elements. To get what you want from that exact example you would want:
$id = $result->weather[0]->id;
You also might want to think about what you want to happen if there is more than one element in that array, or if there are zero.
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.