I have made MySQL database with three columns tags_id,tags,user_id, it must give out all the tags with respect to the user_id given.
eg: for this link:"http://allwaysready.16mb.com/Cuboid/tagsTest.php?user_id[]=7"
Output is:
{"result":[{"tags":"Pascol"},{"tags":"PHP"},{"tags":"Python"}]}
But I need my result to be in a sing string like this:
{"result":[{"tags":"Pascol","PHP",","Python"}]}
It should not be in array i want it in a single string.
Here is my php code :
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$user_id = $_GET['user_id'];
require_once('dbConnect.php');
$user_tags = array();
foreach ($_REQUEST['user_id'] as $key => $val) {
$user_tags[$key] = filter_var($val, FILTER_SANITIZE_STRING);
}
$user_ids = "'" . implode("','", $user_tags) . "'";
$sql = "SELECT * FROM user_tags WHERE user_id IN ({$user_ids})";
$r = mysqli_query($con,$sql);
//creating a blank array
$result = array();
//looping through all the records fetched
while($row = mysqli_fetch_array($r)){
//Pushing name and id in the blank array created
array_push($result,array(
"tags"=>$row['tags']
));
}
//Displaying the array in json format
echo json_encode(array('result'=>$result));
mysqli_close($con);
}
what should i change in my code.?
please help me guys thank you.
Your array_push mean
Push to your array new element with $key => $value
So solution in this case is remove array_push and try
$result['tags'][] = $row['tags'];
Related
Good day! I am trying to get the user ids based on the names submitted on the input field. But the loop only returns the user id of the first selected user. Can somebody please help me?
Here's the code:
if(isset($_POST['proponent'])){
$proponent = mysqli_real_escape_string($con,$_POST['proponent']);
$myArray = explode(',', $proponent);
$posts = array();
foreach($myArray as $item) {
$query = "SELECT user_id FROM user WHERE CONCAT(fname, ' ', lname) = '$item'";
$get = mysqli_query($con, $query);
array_push($posts, mysqli_fetch_assoc($get));
print_r($posts);
}
}
Using mysqli_fetch_assoc only once will only get you the first row if it exists. So you will need to add this in a loop, say while loop like below in order for the mysqli_function to move it's pointer over the entire result set.
while($row = mysqli_fetch_assoc($get)){
$posts[] = $row;
}
I am trying to get the matched rows from a table and save it in a Global Array so that I can use it in different functions.
But when I do print_r of that array it shows only last row.
Here is my code
function setCampoFeed()
{
echo $sql = "SELECT campofeed.tag,campofeed.registro,campofeed.valor FROM campofeed ".
"INNER JOIN registrofeed ON registrofeed.id = campofeed.registro ".
"WHERE registrofeed.feed='".$this->idFeed."'";
$result= $this->localDb->execute($sql);
$this->campoFeed= mysql_fetch_array($result))
}
So here campoFeed is the array that should have all the rows of the match, but now its just having the last row.
Thanks in advance
Use
$this->campoFeed[] = mysql_fetch_array($result);"
insted of
$this->campoFeed= mysql_fetch_array($result);
You will get all data in array
Try this one if it works for you..
$resultArray = array();
$campoFeed = array();
$resultArray = mysql_fetch_array($result);
foreach($resultArray as $key => $value){
$campoFeed[$key] = $value;
}
print_r($campoFeed);
Use this
$mergedArray=array();
while($data= mysql_fetch_array($result)) {
$final_array = unserialize($data['data']);
$mergedArray=array_merge($mergedArray,$final_array);
}
array_unique($mergedArray, SORT_REGULAR);
I'm trying to put data from my database into seperate arrays within another array. This works but when I'm trying to fetch the 'user_id' information, it only shows one number so it works like a string. How can I get it to work like an array and get the entire user_id?
$fetch = mysqli_query($con, "SELECT * FROM spotify_userdata");
$return_arr = [];
while ($row = mysqli_fetch_array($fetch, MYSQL_ASSOC)) {
$return_arr[] = array(
$row_array['user_id'] = $row['user_id'],
$row_array['name'] = $row['name'],
$row_array['artists'] = $row['artists'],
);
}
$user = json_encode($return_arr[0]);
echo $user[2];
This code returns 1 so it show the third number of the user_id. How can I get it to show the entire user_id like this: 111434343
You have many things in your code that's wrong:
Remove the last array item's comma
Change
$return_arr = [];
To
$return_arr = array();
3.Add:
$row_array = array()
at the begginning of all that code
At the end your code must be like this:
$fetch = mysqli_query($con, "SELECT * FROM spotify_userdata");
$row_array = array();
while ($row = mysqli_fetch_array($fetch, MYSQL_ASSOC)) {
$return_arr = array(
$row_array['user_id'] = $row['user_id'],
$row_array['name'] = $row['name'],
$row_array['artists'] = $row['artists'],
);
}
$user = json_encode($return_arr[0]);
According to your code you should use:
echo $user['user_id'];
But the real problem is - where is $row_array initialized?!?
And even bigger problem - why use "=" inside array creation in the while ... it seems to me that "=>" would fit better, don't you think?
I have a query that's retraive a list of id's. Those id's are in an array and i need to save it in table with those id's. I tried using implode to make those id's a string i could use in a where clause but i keep getting this error.
$save_food = $_POST['save_food'];
$unserializedData = array();
parse_str($save_food,$unserializedData);
foreach($unserializedData as $unserializedData1){
$query = mysql_query("insert into subscribefood (s_user_id,s_food) values ('$ft_user_id','".implode($unserializedData1, ',')."')");
}
Try this
<?php
$save_food = $_POST['save_food'];
$unserializedData = array();
parse_str($save_food,$unserializedData);
$datalist = $unserializedData['foodtype'];
foreach($datalist as $data){
$query = mysql_query("insert into subscribefood (s_user_id,s_food) values ('$ft_user_id','$data')");
}
?>
I would want to create an array that will hold records retrieved from a database using a query of SELECT statement.
The records to be retrieved have multiple fields such as lastname, firstname, mi and 20 more fields. What would be the best approach on coding this function?
alright i have followed what prisoner have given below.. the next question is how do i search through this kind of array using queries? for example i want to search for a username..
<?php
// run query
$query = mysql_query("SELECT * FROM table");
// set array
$array = array();
// look through query
while($row = mysql_fetch_assoc($query)){
// add each row returned into an array
$array[] = $row;
// OR just echo the data:
echo $row['username']; // etc
}
// debug:
print_r($array); // show all array data
echo $array[0]['username']; // print the first rows username
You shouldn't search through that array, but use database capabilities for this
Suppose you're passing username through GET form:
if (isset($_GET['search'])) {
$search = mysql_real_escape_string($_GET['search']);
$sql = "SELECT * FROM users WHERE username = '$search'";
$res = mysql_query($sql) or trigger_error(mysql_error().$sql);
$row = mysql_fetch_assoc($res);
if ($row){
print_r($row); //do whatever you want with found info
}
}
$mysearch="Your Search Name";
$query = mysql_query("SELECT * FROM table");
$c=0;
// set array
$array = array();
// look through query
while($row = mysql_fetch_assoc($query)){
// add each row returned into an array
$array[] = $row;
$c++;
}
for($i=0;$i=$c;$i++)
{
if($array[i]['username']==$mysearch)
{
// name found
}
}
$memberId =$_SESSION['TWILLO']['Id'];
$QueryServer=mysql_query("select * from smtp_server where memberId='".$memberId."'");
$data = array();
while($ser=mysql_fetch_assoc($QueryServer))
{
$data[$ser['Id']] =array('ServerName','ServerPort','Server_limit','email','password','status');
}