Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've been trying for a while to show a table from my MySQL database in my browser using Python. I am using MySQL connector to create the connection with my database. (MySQLdb doesn’t work) The problem I have is that I can only display the first four columns. When I want to display an extra column (so 5 columns), nothing more is being displayed. I am stuck on this problem quite a while, maybe there is a limit on four columns in python??

(Do not mind the html ...)

Any help to solve this problem is welcome! This is my code with the print for the fifth column set as comment (#):

#!/Python27/python

#!/usr/bin/python

import mysql.connector
import sys

from mysql.connector import errorcode

print """Content-type: text/html\n

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org  /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html lang="nl">
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />

<title> mytitle </title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>

<table width=100%>

<tr>
<td class="title" colspan="10" align="center">
<img src="logo.gif"/>
<br/>

<h1 class="title"> myheader1 </h1>

<h2 class="subtitle"> myheader2 </h2>
</td>

<tr class="second">
<td class="menu">
<br/>
<div id="navigation">
    <nav>
    <ul>
        <li><a href = "Home.py"> Home </a> </li> <br />
        <li><a href = "Store.py"> Store </a> </li> <br />
        <li><a href = "blog.html"> Blog </a> </li> <br />
        <li><a href = "AboutUs.html"> About us </a> </li> <br />
    </ul>
</nav>
</div>   
</td>

<td class="main">
<br/>
<center> &nbsp; <strong><u> Store </u></strong></center>
<br/>
<center><table border="0"><tr><th> Refnr &nbsp; </th><th> Merk &nbsp; </th><th> Name &nbsp; </th><th> Description &nbsp; </th><th> Color &nbsp; </th><th></tr>
<tbody>"""

# create database connection
connection = mysql.connector.Connect(host='localhost',user='root',passwd='Mypasswd',database='Mydatabase' )

# get the cursor()
cur = connection.cursor()

# execute SQL query using execute() method.
cur.execute("""SELECT * FROM top""")

for row in cur.fetchall() :
Refnr = str(row[0])
Merk = str(row[1])
Name = str(row[2])
Description = str(row[3])
#Color = str(row[4]) 

print Refnr 
print '&nbsp'
print Merk
print '&nbsp'
print Name
print '&nbsp'
print Description   
print '&nbsp'
#print Color
print '<br>'

cur.close()
connection.close()

print """
</tbody>
</table>
</td>
</tr>

<tr>
<script type="text/javascript">
   document.write(Date());
</script>

</table>

</body>
</html>             
"""
share|improve this question
I don't see anything obviously wrong there. Are you sure that the query actually returns five columns, and that the fifth column is not NULL or the empty string? – Aya Apr 20 at 13:22
Please insert print [d[0].decode('utf8') for d in cur.description] after cur.execute.. line. What are you getting? – Alexander Afanasiev Apr 21 at 11:42
@Aya I'm sure that the fifth column is not null or empty. I've got 8 columns but I only printed out the first 5 because when I print 5 or more columns nothing is being printed. – Thomas1207 Apr 22 at 9:10
@AlexanderAfanasiev I tried your solution but I only get the 8 column names (headers) printed in unicode... – Thomas1207 Apr 22 at 9:12
Ok, select just 1 column color (SELECT color FROM top). What are you getting from print [row[0] for row in cur.fetchall()]? – Alexander Afanasiev Apr 22 at 9:25
show 1 more comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.