2

I'm having some problem when I try to select a column from my table in MySQL and turn it into a PHP array. When I echo my result (the PHP array), it doesn't return me anything.

Here is my code:

$result_while = mysql_query("SELECT id` 
      FROM afunda_eleva");

$array = array();
while ($row = mysql_fetch_array($result_while)) {
  $array[] = $result_while['id'];
}

for($i=0; $i < 4; $i++)
{
    echo "$array[$i]"; //Possibly error happening here!!
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
GustavoxD
  • 89
  • 8
  • Looks like you're missing the first backtick around 'id' in your SQL. It's probably returning an error. – JAL Sep 27 '13 at 01:06

3 Answers3

3

Gustavo mysql_query is deprecated in the newer PHP version, it is unsafe but does the job. Instead I rather give you an answer with my favorite way of query databases PDO it is secure and it is object-oriented.

first we need to set it up

// instance of pdo
$config['db'] = array
(
    'host' => '',
    'username' => '',
    'password' => '',
    'dbname' => ''
);
$database = new PDO('mysql:host=' . $config['db']['host'] . 
';dbname=' . $config['db']['dbname'],
$config['db']['username'],
$config['db']['password']);
$database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

//query with a new database object
$queryObject = $database->prepare("SELECT id 
FROM afunda_eleva");
//execute sql
$queryObject ->execute();
// store the results in array
$results = $queryObject->fetchAll();
//print_r results to test 
print_r($results);
//after analysing the array and how its structured define
for($i=0; $i < 4; $i++)
{
    echo $results[$i] 
}

try that code with your database credentials on the instance, if you want to include this in a seperate db file that gets included before calling pdo make sure you call global $database; to call upon its instance

here is a good pdo tutorials playlist, good luck

https://www.youtube.com/watch?v=XQjKkNiByCk

Sébastien
  • 11,860
  • 11
  • 58
  • 78
WeTheBrains
  • 164
  • 7
  • +1 very good example. I am not advocating sticking to `mysql_*` but GustavoxD had to understand his mistake in the context of his own code. – Sébastien Sep 27 '13 at 00:58
  • @Sébastien sorry both of you have latin names i didnt know who asked it, i did not mean to call upon you. sorry – WeTheBrains Sep 27 '13 at 01:01
2

First if you wish to work with column names in your array you need to replace mysql_fetch_array with mysql_fetch_assoc.

Then you need to access the row, not the result, inside your while loop:

$array[] = $row['id'];
Sébastien
  • 11,860
  • 11
  • 58
  • 78
1

1- To get the result you have to put $row instead $result_while['id]

2-In order to print an array you have to use print_r() or var_dump()

$result_while = mysql_query("SELECT id` 
      FROM afunda_eleva");

$array = array();
while ($row = mysql_fetch_array($result_while)) {
  $array[] = $row;
}

echo '<pre>';
print_r($array);
echo '<pre>';

Text in a <pre> element is displayed in a fixed-width font (usually Courier), and it preserves both spaces and line breaks.

Side Note: Why shouldn't I use mysql_* functions in PHP a useful link

Community
  • 1
  • 1
Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
  • 1
    As Sébastien suggested, i thought the problem was up to the " $array[] = $result_while['id'];". But thanks for the mysqli tips though! I'm beginning my studies. – GustavoxD Sep 27 '13 at 00:48