Page 1 of 1

problem with a mysq database

Posted: Wed Nov 05, 2003 6:25 am
by kujtim
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

Re: problem with a mysq database

Posted: Wed Nov 05, 2003 6:33 am
by m3mn0n

Code: Select all

<?php
     print "\t\t<td>$col_value</td>\n";
     echo $col_value;
?>
Those two lines of code indicate to PHP that you want to echo the same variable twice. Notice how $col_value appears twice?

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

Posted: Wed Nov 05, 2003 6:44 am
by kujtim
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

Posted: Wed Nov 05, 2003 6:57 am
by Weirdan
It's because you are using the mysql_fetch_array.
[php_man]function.mysql-fetch-array[/php_man]
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.
According to the explanation, you are getting two values, one stored under the integer index and one - under the string index.

Use the mysql_fetch_row instead, or add MYSQL_NUM as second parameter to mysql_fetch_array call.

Posted: Wed Nov 05, 2003 10:06 am
by JAM
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:

Code: Select all

Array
(
    &#1111;0] =&gt; value
    &#1111;fieldname] =&gt; value
)
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'].