problem with a mysq database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kujtim
Forum Commoner
Posts: 35
Joined: Sat Oct 25, 2003 4:00 am
Location: kosovo
Contact:

problem with a mysq database

Post 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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Re: problem with a mysq database

Post 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.
kujtim
Forum Commoner
Posts: 35
Joined: Sat Oct 25, 2003 4:00 am
Location: kosovo
Contact:

no no i am riciving the same thing and without the echo line

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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'].
Post Reply