I am writing this code and I am reciving this
<html>
<head>
<title></title>
</head>
<body>
<?php
$link = mysql_connect("localhost", "administrator", "")
or die ("Could not connect to MySQL");
mysql_select_db ("test")
or die ("Could not select database");
$query = "SELECT * FROM votimi";
$result = mysql_query ($query)
or die ("Query failed");
// printing HTML result
print "<table>\n";
while ($line = mysql_fetch_array($result)) {
print "\t<tr>\n";
while(list($col_name, $col_value) = each($line)) {
print "\t\t<td>$col_value</td>\n";
echo $col_value;
}
print "\t</tr>\n";
}
print "</table>\n";
?>
</body>
</html>
I am riciving this
3 3
4 4
5 5
5 5
5 5
but my database in mysql loks like this
votat
-----
3
4
5
5
5
the answer is why i am reciving two colums
i want to recive one column like in mysql
problem with a mysq database
Moderator: General Moderators
Re: problem with a mysq database
Code: Select all
<?php
print "\t\t<td>$col_value</td>\n";
echo $col_value;
?>Remove one of those and you should just get one row of results, as you want.
no no i am riciving the same thing and without the echo line
no no i am riciving the same thing and without the echo line
the echo line is there because i was testing something and i forgat to remuve it from ther
the echo line is there because i was testing something and i forgat to remuve it from ther
It's because you are using the mysql_fetch_array.
[php_man]function.mysql-fetch-array[/php_man]
Use the mysql_fetch_row instead, or add MYSQL_NUM as second parameter to mysql_fetch_array call.
[php_man]function.mysql-fetch-array[/php_man]
According to the explanation, you are getting two values, one stored under the integer index and one - under the string index.mysql_fetch_array() is an extended version of mysql_fetch_row(). In addition to storing the data in the numeric indices of the result array, it also stores the data in associative indices, using the field names as keys.
Use the mysql_fetch_row instead, or add MYSQL_NUM as second parameter to mysql_fetch_array call.
Or [php_man]mysql_fetch_assoc[/php_man]() that will fetch an associative array.
Explaining further:
kujtim,
You have two while-loops. As you mysql_fetch_array you get this result:
The inner while-loop then prints both the [0]-value AND the [fieldname]-value, hence you get two printouts all the time.
Remove the inner while-loop and play around with using $line[0] or $line['fieldname'].
Explaining further:
kujtim,
You have two while-loops. As you mysql_fetch_array you get this result:
Code: Select all
Array
(
ї0] => value
їfieldname] => value
)Remove the inner while-loop and play around with using $line[0] or $line['fieldname'].