【发布时间】:2015-08-17 03:35:27
【问题描述】:
我正在使用“python3.4”和“Mysql.connector”来连接数据库。并访问Xampp Mysql,在本地主机上运行代码。 下面给出的代码是我用来连接数据库和获取数据的代码
#!"C:\python34\python.exe"
import sys
import mysql.connector
print("Content-Type: text/html;charset=utf-8")
print()
/* code to connect datbase*/
conn = mysql.connector.connect(host='localhost',port='8051',
database='test',
user='root',
password='Tom')
cursor = conn.cursor()
if conn.is_connected():
print('sucessfull...Connected to MySQL database')
cursor.execute(" SELECT * FROM testtable1 ")
print ("<html>")
print("<p>")
print("hai")
print("</p>")
print("<body")
print ("<table border='2'>")
print ("<tr><td>")
for row in cursor.fetchall():
print (row)
print ("</td></tr>")
print ("</table>")
print ("</body>")
print ("</html>")
它给出的输出为: 成功...连接到 MySQL 数据库
我无法在表格中显示数据。没有表格也无法打印从表格中获取的数据。 在 localhost 中以http://localhost:804/cgi-bin/test.py 运行代码 由于 xampp 或 apache 是否有任何问题。 请给点建议
【问题讨论】:
-
@evans_murithi 我编辑了请看
标签: python mysql apache xampp localhost