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()
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 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 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 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 All: I do not have much experience with Arrays but am trying to learn. Trying something new and don't know if this can be done this way or not. Essentially what I am trying to do is go through a wp database table; and go through the records; the $postnum value is equivalent to a WP post_id and is in the database multiple times. So I want to just whip through the table and create an array with each post_id ($postnum) in there just once. So I thought, all i need to do is create an array outside of loop; then while in loop just check if the $postnum is already in the array; if it is not just add it…so then all records with that same postnum in them they will not add to the array…
here is my code:
$posts_counted=array();
global $wpdb;
foreach( $wpdb->get_results("SELECT * FROM wp_c93hh3bk23_pvc_daily") as $key => $row) {
$id = $row->id;
$time = $row->time;
$postnum = $row->postnum;
$postcount = $row->postcount;
if (in_array($postnum,$posts_counted))
{
array_push($posts_counted,$postnum);
}
} // close out foreach
I have never really used the "in_array" thing or the "array_push" thing either; so these are all new to me…
I am getting: "Parse error: syntax error, unexpected ' ' (T_STRING) in…"
referring to the line "if (in_array($postnum,$posts_counted))"
So I am wondering if anyone can tell me if what I am trying to do can be done this way or what I am doing wrong on this…
Thanks!
This is how I get data from db:
$sql= "Select * FROM table";
$result=mysql_query($sql);
while($rows = mysql_fetchArray($result)){
$data= $rows;
}
//checking if something is in array
if(in_array($data['id')){
//do something
}
//now how you want to get data
// echo json_encode($data)
//OUTPUT: {id:"1",name:"xyz"}
Anyway if you want your "postNum" unique, fix your sql with adding DISTINCT after SELECT.