1

I want to add the column in mysql to the array.

i want in this way

$php_framework = array("Stack", "Over", "Flow", "Php", "Sql");

i tried this but not working

$cek = $db->query("select * from anket order by cevap_id desc ",PDO::FETCH_ASSOC);
            
if(empty($cek)){echo "<br/><center><p><h1>Aradığın ile ilgili bişey yok.</h1><p></center>";}              

foreach($cek as $m){  
    $php_framework = array($m['cevap']);
} 

i want to make a poll system. $php_framework is listing the results

thank you so much

2 Answers 2

1

Looks like you're overwriting the array on each iteration. Hard to know for sure, but try appending the elements to the array.

<?php

...

// Initialize the array
$php_framework = array();

foreach($cek as $m){  
    // Keep appending elements to the array. 
    $php_framework[] = $m['cevap'];
} 

var_dump($php_framework);
Sign up to request clarification or add additional context in comments.

Comments

1

at first you should not check the result of query by checking it with empty function

even queries that have not any record has an instance of PDOStatements class on their return so even if your query result is empty, by checking the query variable you can't specific that query has any record or not

for checking that your query has any record the best way is using rowCount that show you the count of your query result rows count. so check it that rowCount is more than 0 or not.

<?php
// query
$cek = $db->prepare('SELECT * FROM `anket` ORDER BY `cevap_id` DESC');
$cek->execute();

// checking query has any record or not
if($cek->rowCount() < 1){
    print 'no record found!';
}else{
    // your process
}

and at last for saving a column values in an array, you should create it and in loops put your column value of each row in every loop iterates

<?php
// query
$cek = $db->prepare('SELECT * FROM `anket` ORDER BY `cevap_id` DESC');
$cek->execute();

// checking query has any record or not
if($cek->rowCount() < 1){
    print 'no record found!';
}else{
    
    // your array
    $php_framework = [];
    
    // your process
    while($row = $cek->fetch(PDO::FETCH_ASSOC)){
        $php_framework[] = $row['amirali']; // put your column name instead of "amirali"
    }
    
    print_r($php_framework);
    
    // or
    
    var_dump($php_framework);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.