Display Array Values from SQL Table Rows - php

categoryID
10
20
30
For example. Above is my categoryID column with values of 10, 20, and 30 PHP MySQL. What I want to do is to echo those values using an array. Like -> 10 20 30. Below is my code. In my code. I just stored all row data from categoryID into my $array variable. My concern is. How do I echo all the values?
Thank you for the help!
<?php
include ("dbconnect.php");
$sql = "SELECT categoryID FROM post";
$result = mysqli_query($con, $sql);
$array = array();
while($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
?>

Use $array variable as below:-
$array[] = $row['categoryID'];
and to print the array try:-
print_r($array);
and to print them as 10 20 30 try the following:-
echo implode(' ',$array);

The best way is to use json_encode() function
echo json_encode($array);
so try this
<?php
include ("dbconnect.php");
$sql = "SELECT categoryID FROM post";
$result = mysqli_query($con, $sql);
$array = array();
while($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
echo json_encode($array);
exit;
?>

Related

php mysql add sub array to main array

I need to append result of mysql query with existing result of another query, and both queries performed on different table, actually what I really want to do is take value from main table and get some data from another table using first value, and combine all together using json encode.
$query = "select ID,TIMESTAMP,UUID from TABLE1 where USERNAME='$user'";
$result = mysqli_query($conn, $query);
$numrows = mysqli_num_rows($result);
if($numrows>0)
{
$res=$a;
$myArray = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$eachuuid = $row[UUID];
$query1 = "select ID,IMAGEURL from TABLE_IMAGES where IMG_UUID='$eachuuid'";
$result1 = mysqli_query($conn, $query1);
$numrows1 = mysqli_num_rows($result);
if($numrows1>0){
$res1;
$myArray1 = array();
while($row1 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$myArray1[] = $row1;
}
$row['IMG_URL']=$myArray1;
}
$myArray[] = $row;
}
$res['Data']=$myArray;
echo json_encode($res);
Result:
{"Response":"OK","Data":[{"ID":"62",
"TIMESTAMP":"26 January",
"UUID":"12345",
"IMG_URL":[{"ID":"5","IMAGEURL":"26_January_2016_22_39_28_crop.jpg"}]}]}
And I need to get the output like,
{"Response":"OK","Data":[{"ID":"62",
"TIMESTAMP":"26 January",
"UUID":"12345",
"IMG_URL":{"ID":"5","IMAGEURL":"26_January_2016_22_39_28_crop.jpg"}}]}
Means need to remove [] from IMG_URL section so that I can parse the data easily.
You can do this
$row['IMG_URL']=$myArray1[0];
Or
$myArray1 = null;
while($row1 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$myArray1 = $row1;
}
$row['IMG_URL']=$myArray1;
I hope this helps.

MySQL: How to use implode array in WHERE IN clause of MySQL?

I'm having a problem when I try to use implode array in WHERE IN clause and display it. Some of the record in the database has an apostrophe in it.
Example the data in my database are testing,test'ing,tes't. And my code below is how I implode the data to be used in WHERE IN clause.
<?php
$arr = array();
$qry = mysqli_query($con,"SELECT sampleTxt FROM Table")or die(mysqli_error($con));
while(list($txt) = mysqli_fetch_row($qry)){
$t = mysqli_real_escape_string($con,$txt);
$arr[] = "'".$t."'";
}
$sampl = implode(',',$arr);
?>
And here is my sample code on how I used it on WHERE IN clause.
<?php
$qry2 = mysqli_query($con,"SELECT sampleTxt2 FROM Table2 WHERE sampleTxt IN (".$sampl.")")or die(mysqli_error($con));
while(list($txt2) = mysqli_fetch_row($qry2)){
echo $txt2;
}
?>
The output should be
testingtest'ingtes't
but instead the output is just the testing.
Check for below code
<?php
$arr = array();
$qry = mysqli_query($con,"SELECT sampleTxt FROM Table")or die(mysqli_error($con));
while(list($txt) = mysqli_fetch_row($qry)){
//$t = mysqli_real_escape_string($con,$txt);
$arr[] = '"'.$txt.'"';
}
$sampl = implode(',',$arr);
?>
And then
<?php
$qry2 = mysqli_query($con,'SELECT sampleTxt2 FROM Table2 WHERE sampleTxt IN ('.$sampl.')')or die(mysqli_error($con));
while(list($txt2) = mysqli_fetch_row($qry2)){
echo $txt2;
}
?>

Putting Mysql results into a multidimensional php array

I have a mysql table which I want to have it as a PHP array. Suppose we have a field call id and another field called name. As there may be not one result in the table I want something like $result[0]['id'] to point the first result's id.
I thought of this:
$result = mysql_query("SELECT * FROM db_name
WHERE dependence = 0");
$rows = array();
while($row = mysql_fetch_assoc($result)){
$rows = $row;
}
echo $rows[0]['name'];
But it doesn't work!!! Would you please help me?
$result = mysql_query("SELECT * FROM db_name
WHERE dependence = 0");
$rows = array();
while($row = mysql_fetch_assoc($result)){
$rows[] = $row;
}
try this
$i=0;
while($row = mysql_fetch_assoc($result)){
$rows[$i] = $row['id'];
$rows[$i] = $row['name'];
$i++;
}
You forgot to give column name
while($row = mysql_fetch_array($result)){
echo $row['colname'];//fill inside which column you need $row['colname']
}

Php mysql query array stores 2 fields in each array index

When I try to mysql_fetch_row the array that is created contains 2 fields from my selection at each index. I would like to ask why is this happening?
<?php
$categoryid = $_GET['id'];
include('connect.php');
$query = "SELECT
Categories_SubCategories.IdCategory,Categories_SubCategories.idSub_Category,
Categories.Name, Sub_Categories.Name from Categories_SubCategories JOIN
Categories on Categories.idCategory = Categories_SubCategories.idCategory
JOIN Sub_Categories on Categories_SubCategories.idSub_Category =
Sub_Categories.idSub_Category
WHERE Categories_SubCategories.IdCategory = $categoryid";
$result = mysql_query($query);
$rows = mysql_num_rows($result);
for($i=0; $i<$rows; $i++){
$display = mysql_fetch_row($result);
echo "$display[3]";
}
?>
most PHP programmers use while() loop when they want to work with mysql_fetch_array().
Take a look at this sample code:
$query = mysql_query("SELECT id,name FROM tbl_members");
if (mysql_num_rows($query)) {
while ($result = mysql_fetch_array($query)) {
echo('User #'.$result['id'].' is: '.$result['name'].'<br />');
}
}
// Output can be something like this:
// User #1 is: John
// User #2 is: Sarah
replace mysql_num_rows with mysql_fetch_array or mysql_fetch_assoc

why doesn't this code return all rows?

I have an access database, and one of the tables is called Products, which contains 9 rows.
I want to echo all of the rows, but when I use this code, it will echo only 4 rows.
Where is my fault ?
$conn = odbc_connect('MoeinODBCTest1', '', '');
$sql = "select * from Products";
$rs = odbc_exec($conn, $sql);
while(odbc_fetch_row($rs))
{
$arr = odbc_fetch_array($rs);
print_r($arr);
echo '<br>';
}
Both odbc_fetch_array and odbc_fetch_row are pulling rows out of the results.
Try instead:
$conn = odbc_connect('MoeinODBCTest1', '', '');
$sql = "select * from Products";
$rs = odbc_exec($conn, $sql);
while($arr = odbc_fetch_array($rs)) {
print_r($arr);
echo '
';
}
You are fetching rows both when calling odbc_fetch_row and odbc_fetch_array, so every second row gets discarded.
Since odbc_fetch_array returns FALSE when there are no more rows, you can use it to fetch the next row and check if there are any more rows, at the same time:
while ($arr = odbc_fetch_array($rs)) {
print_r($arr);
echo "\n";
}

Categories