I have create simple function to check if table exists in the database or not ..
I want fetch all table in the database and i use like
$sql = "SHOW TABLES FROM plus LIKE '%group%'";
$do = mysqli_query($db,$sql);
$table = array();
while ($row = mysqli_fetch_row($do)) {
$table[] = $row ;
}
print_r($table);
}
the output
Array ( [0] => Array ( [0] => group_1 ) [1] => Array ( [0] => group_2 ) [2] => Array ( [0] => group_3 ) [3] => Array ( [0] => group_4 ) )
I want all table in one array to encode with Json ,
and use the array to check in_array by PHP ..
==Edited==
I'm getting JSON as
[["group_1"],["group_2"],["group_3"],["group_4"]]
But, I'm expecting
["group_1","group_2","group_3","group_4"]
You could use json_encode, to get your resultset as json
echo json_encode($table);
Since, you are getting sub-array with your $table array, you need to merge them into single array to get your expected output.
You could make changes to your loop.
while ($row = mysqli_fetch_row($do)) {
array_merge($table,$row);
}
echo json_encode($table);
or, to print array
var_dump($table);
Related
I'm trying to get all tables from a certain database inside an array, so I can compare it to another database.
//Get a list of all tables
$sql = "SHOW TABLES FROM Data;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck == 0)
{
exit("NO SQL RESULTS");
}
$array = mysqli_fetch_all($result);
print_r($array);
However, when I do this, it results in me getting an array with the results nested inside another array.
Array
(
[0] => Array
(
[0] => table1
)
[1] => Array
(
[0] => table2
)
[2] => Array
(
[0] => table3
)
[3] => Array
(
[0] => table4
)
)
Is there a way of doing this, without using a while loop in combination with mysqli_fetch_assoc?
You could try something like this:
SELECT group_concat(table_name) as tables FROM information_schema.tables where table_schema='Data'
This will return a single field tables which contains a comma separated list of all table names.
You can then use:
$array = mysqli_fetch_array($result);
$tables = explode(",", $array['tables']);
print_r($tables);
To split the table names into an array
I have a table named as 'files' with fields id, formID ,filename, date_modified.
Now i want to fetch list of filenames from this table to an array with index of array from each row with same formID in that array for example:
(In core php)
Instead of this
Array
(
[0] => Array
(
[file_name] => filename1.jpg
)
[1] => Array
(
[file_name] => filename2.jpg
)
)
I want result in this format:
Array
(
[0] => filename1.jpg
[1] => filename2.jpg
)
Use a select query, then do a while loop to loop through each row. During that loop, assign the filename to each position of $array, be it what you name it.
$conn = $mysqli_connect(.....);
$query = "SELECT id,formID,filename,date_modified FROM files";
$result = $conn->query($query);
while($row = $result->fetch_assoc()){
$array[] = $row['filename'];
}
$result->close();
$conn->close();
print_r($array);
You should use this
$sth = $dbh->prepare("SELECT filenames FROM tablename");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);
print_r($result);
Result
Array
(
[0] => filename1.jpg
[1] => filename2.jpg
)
The array_column() function returns the values from a single column in the input array.
$result = array_column($yourArray, 'file_name');
I have the following loop that creates an array
while($row1 = mysqli_fetch_assoc($result1))
{
$aliens[] = array(
'username'=> $row1['username'],
'personal_id'=> $row1['personal_id']
);
}
It produces the following result
Array (
[0] =>
Array ( [username] => nimmy [personal_id] => 21564865 )
[1] =>
Array ( [username] => aiswarya [personal_id] => 21564866 )
[2] =>
Array ( [username] => anna [personal_id] => 21564867 )
Then I have another loop as follows, inside which, I need to fetch the personal_id from the above array. I fetch it as follows.
foreach($aliens as $x=>$x_value)
{
echo $aliens['personal_id'];
//some big operations using the
$aliens['personal_id']; variable
}
However, I can't get the values if personal_ids. I get it as null. What seems to be the problem? How can I solve it?
You have an array of "aliens", each alien is an array with personal_id and username keys.
foreach ($aliens as $index => $alien) {
echo $alien['personal_id'], PHP_EOL;
}
The foreach loop iterates its items (aliens). The $alien variable represents the current iteration's item, i.e. the alien array.
foreach($aliens as $x=>$x_value)
{
echo $x_value['personal_id'];
//some big operations using the
$x_value['personal_id']; variable
}
I have a task that I need to implement, in a nutshell I want to be able to read multiple date values from a table in mySQL using PHP then manipulate these values to get date difference in days, save these new days (int values) into another array for further manipulation (linear regression). My problem is I cant get to the point where I can save initial values retrieved from the DB into an array, but when I try to manipulate these values and save them into a second array, it does not work. here is my simple code:
$result = mysql_query("SELECT TO_DAYS(date) FROM ordering WHERE id=1",$conn);
$num_rows = mysql_num_rows($result);
// mysql_query($result,$conn) or die(mysql_error();
//echo $num_rows;
$data = array();
$days=array();
while (($row = mysql_fetch_array($result, MYSQL_ASSOC)) !== false){
$data[] = $row; // add the row in to the results (data) array
}
print_r($data); // print result
//This part is not working
for ($x=$num_rows;$x>0;$x--){
$days[x]=(($data[$x])-($data[$x-1]));
}
print_r($days);
mysql_close($conn);
If I don't include the for loop in my code, I get an output on the screen as follows:
Array (
[0] => Array ( [TO_DAYS(date)] => 735599 )
[1] => Array ( [TO_DAYS(date)] => 735630 )
[2] => Array ( [TO_DAYS(date)] => 735658 )
[3] => Array ( [TO_DAYS(date)] => 735689 )
[4] => Array ( [TO_DAYS(date)] => 735735 )
[5] => Array ( [TO_DAYS(date)] => 735780 )
[6] => Array ( [TO_DAYS(date)] => 735811 )
)
Please I really need some help here.
Thank You - Hamood
Try this:
for ($x = $num_rows - 1; $x > 0; $x--) {
$days[$x] = $data[$x]['TO_DAYS(date)'] - $data[$x-1]['TO_DAYS(date)'];
}
Here, $data[$x] is an array itself with key TO_DAYS(date), so you need to get your desired value with $data[$x]['TO_DAYS(date)'].
I am trying to count elements in an array, but it doens't work as intended:
I have a while loop, which loops through my user table:
while($refsData=$refs->fetch()){
$new_array = array($refsData['id']);
print_r($new_array);
$outcome = $rentedrefs->_paying($new_array);
}
The print_r($new_array); gives me:
Array
(
[0] => 90427
)
Array
(
[0] => 90428
)
Array
(
[0] => 90429
)
Array
(
[0] => 90430
)
Array
(
[0] => 90431
)
Array
(
[0] => 90432
)
Array
(
[0] => 90433
)
Array
(
[0] => 90434
)
Array
(
[0] => 90435
)
Array
(
[0] => 90436
)
Inside the _paying function, I count the number of values from the array:
function _paying($referrals_array){
echo count($referrals_array);
}
The problem is, that the above count($referrals_array); just gives me: 1, when it should be 10
What am I doing wrong?
You create a new array at each step of the loop. Instead it should be written like this:
$new_array = array();
while($refsData=$refs->fetch()){
$new_array[] = $refsData['id'];
// print_r($new_array);
}
$outcome = $rentedrefs->_paying($new_array);
Note that I moved the _paying call outside the loop, as it seems to be the aggregating function. If not, you'd most probably make it process $refsData['id'] instead - not the whole array.
As a sidenote, I'd strongly recommend using fetchAll() method (instead of fetch when you need to fill a collection with results of a query. It'll be trivial to count the number of the resulting array.
It works properly, in each circulation of loop you have one array, so in first you have:
Array
(
[0] => 90427
)
in 2nd:
Array
(
[0] => 90428
)
and so on.
It should work properly:
var $count = 0;
while($refsData=$refs->fetch()){
$new_array = array($refsData['id']);
$count += count($new_array);
$outcome = $rentedrefs->_paying($new_array);
}
You are creating $new_array as a new array with the single element $refsData['id']. The count of 1 is therefore correct.
To get the number of results, either use a COUNT(*) select to ask your sql server, or add a counter to your loop, like this:
$entries = 0;
while($refsData=$refs->fetch()){
$new_array = array($refsData['id']);
print_r($new_array);
$entries++;
$outcome = $rentedrefs->_paying($new_array);
}
echo $entries;
You are not adding elements to an array, but creating a new array each iteration. To add elements, just do:
$new_array[] = $refsData['id'];