I have 10 rows, however when I do the following, it's only giving me the last row. Any kind of help I can get on this is great appreciated!
$query = "select * FROM `$table`.`channels` WHERE `country`='vietnam' ORDER BY `chanid`";
$result = mysql_query($query,$db) or die(mysql_error());
$data = array();
while($row = mysql_fetch_array($result)) {
$chanid = $row['chanid'];
$data[navtitle] = "$chanid - $row[title]";
$data[navurl] = "http://www.localhost.com/vietnam.php?chanid=$row[chanid]&country=$row[country]";
$data[vid_art] = "$chanart";
}
$array2=array_merge(array($array,array($data));
$JSON=json_encode($array2);
echo $JSON;
My $data array is only outputs the last row of my mysql fetch. How can I get it to pull out all 10 rows that I have?
Each time you go through the
while($row = mysql_fetch_array($result)) {
loop, you're setting $data to a new value.
This means it'll only ever contain the last row's worth of data.
Your code should look something like this.
$array2 = array();
while ($row = mysql_fetch_array($result)) {
$data = array();
$chanid = $row['chanid'];
$data['navtitle'] = "$chanid - $row[title]";
$data['navurl'] = "http://www.localhost.com/vietnam.php?chanid=$row[chanid]&country=$row[country]";
$data['vid_art'] = $chanart;
$array2[] = $data;
}
$JSON = json_encode($array2);
Related
I've never really used json_encode, but it's easy enough to do:
$result = $dblink->query("SELECT * FROM Contracts LIMIT 3");
$dbdata = array();
while ( $row = $result->fetch_assoc()) {
$dbdata[]=$row;
}
echo json_encode($dbdata);
/tada!
However, if I want to give the output data custom "column names", is there a simple way of doing this?
So, rather than outputting something like:
[{"TableColumn1":"147","TableColumn2":"9","TableColumn3":"39","TableColumn4":"32","TableColumn5":"41"...
I can have something like:
[{"My Own Title":"147","My Own Title 2":"9","My own title 3":"39",...
Try this
$result = $dblink->query("SELECT * FROM Contracts LIMIT 3");
$dbdata = array();
while ( $row = $result->fetch_assoc()) {
$rowarray = [];
$rowarray['My Own Title 1'] = $row['TableColumn1'];
$rowarray['My Own Title 2'] = $row['TableColumn2'];
$rowarray['My Own Title 3'] = $row['TableColumn3'];
$dbdata[]=$rowarray;
}
echo json_encode($dbdata);
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've looked for something similar on stack but nothing exactly as this.
I (think I) need to generate a unique MySQL query inside a loop as each iteration needs to look up a different table. the loop is from an exploded $_GET array.
The problem is creating a differently named mysql query based on the loop iteration. I've done it where the $var name is different but it doesn't work, I think because it is a string not a variable?
Any help appreciated
$temps = explode(",", $_GET['temps']);
$tempCount = count($temps);
for ($i=0; $i<$tempCount; $i++)
{
/*'normal' database lookup
$check = mysql_query("SELECT * FROM _db_".$temps[$i]."");
$checks = array();
while ($row = mysql_fetch_assoc($check)) {
$checks[] = $row;
}*/
//here's where I'm trying to build a 'dynamic' lookup for each loop iteration
$checkTemp=$check.$temps[$i];
$checkTempArray=$check.$temps[$i].'Array';
$checkTemp = mysql_query("SELECT * FROM _db_".$temps[$i]."");
$checkTempArray = array();
while ($row = mysql_fetch_assoc($checkTemp)) {
$checkTempArray[] = $row;
}
}
If I understand correctly you're trying to SELECT * from all tables seperated by , in the $_GET["temps"]
$temps = explode(",", $_GET['temps']);
$tempCount = count($temps);
$allResults = array();
for ($i=0; $i<$tempCount; $i++)
{
$checkTemp = mysql_query("SELECT * FROM _db_".mysql_real_escape_string($temps[$i]));
$allResults[$temps[$i]] = array();
while ($row = mysql_fetch_assoc($checkTemp))
{
$allResults[$temps[$i]][] = $row;
}
}
// Now for example $allResults["john"][3] contains the fourth row in the table _db_john
print_r($allResults["sally"][2]); // print the third row in _db_sally
Seems like a typo in your code
$checkTemp = mysql_query("SELECT * FROM db".$temp[$i]."");
either use
$temps[$i] or just $temp
$temp[$i] doesn't makes any sense
so your query should be instead
$checkTemp = mysql_query("SELECT * FROM db".$temps[$i]."");
EDIT:
for your array part you can use
$$temp = array();
while ($row = mysql_fetch_assoc($checkTemp)) {
$$temp[] = $row;
}
I have an MSSQL query where it SELECTS all rows that meet a specific criteria. In PHP I want to fetch that array, but then I want to convert it into one array per row. So if my query returns 3 rows, I want to have 3 unique arrays that I can work with.
I'm not sure how to go about this. Any help would be greatly appreciated!
Thanks, Nathan
EDIT:
$query = "SELECT * FROM applicants WHERE applicants.user_id ='{$_SESSION['user_id']}'";
$query_select = mssql_query($query , $connection);
if (mssql_num_rows($query_select) == 2){
$message = '2 students created successfully';
}
$i = 0;
while($row = mssql_fetch_array($query_select)) {
$student.$i['child_fname'][$i] = $row['child_fname'];
$student.$i['child_lname'][$i] = $row['child_lname'];
$i++;
}
$query_array1 = $student0;
$query_array2 = $student1;
You will notice from the code above that I am expecting two rows to be returned. Now, I want to take those two rows and create two arrays from the results. I tried using the solution that was give below. Perhaps my syntax is incorrect or I didn't understand how to properly implement his solution. But any help would be greatly appreciated.
$query = mssql_query('SELECT * FROM mytable');
$result = array();
if (mssql_num_rows($query)) {
while ($row = mssql_fetch_assoc($query)) {
$result[] = $row;
}
}
mssql_free_result($query);
Now you can work with array like you want:
print_r($result[0]); //first row
print_r($result[1]); //second row
...
$i=0;
while( $row = mysql_fetch_array(query){
$field1['Namefield1'][$i] = $row['Namefield1'];
$field2['Namefield2'][$i] = $row['Namefield2'];
$field3['Namefield3'][$i] = $row['Namefield3'];
$i++;
}
or if you preffer an bidimensional array:
$i=0;
while( $row = mysql_fetch_array(query){
$result['Namefield1'][$i] = $row['Namefield1'];
$result['Namefield2'][$i] = $row['Namefield2'];
$result['Namefield3'][$i] = $row['Namefield3'];
$i++
}
I am having a problem. The following code works fine in my local, but on the live server, it's not working properly..I was supposed to get two rows, but on the live server I am getting only 1 result.
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
$result = mysql_fetch_object($query);
print json_encode($result);
What could possibly be the error ?...
I can't believe you get two rows with this, to get all rows you have to do like this:
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
while($result = mysql_fetch_object($query))
{
print json_encode($result);
}
mysql_fetch_object/array/row always returns only one row and moves the pointer to the next row, if there is no next row it returns false.
Your code is only getting one row. The mysql_fetch_object() function only returns one row. You need to try something like this:
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
$json = array();
while ($result = mysql_fetch_object($query))
$json[] = $result;
print json_encode($json);
I think this is because mysql_fetch_object only returns a single row result. You need something like this:
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
while($row = mysql_fetch_array($query))
{
\\access each result here
}
I can't see how you can have two rows in local
$array = array();
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
while($result = mysql_fetch_object($query))
{
$array[] = $result;
}
print json_encode($array);