#!/usr/bin/python
#==============================================================================================
# Get data from a MySql database from 2 different tables
# You can either use the same cursor if you retrieve all the data at once 
# Or you can use 2 different cursors if you want to interweave the reading from the 2 tables 
#==============================================================================================
import pymysql as mydb                  #Mysql 3x database driver
  
print("Content-Type: text/html \n")     #required http response header (w/ extra line)

host   = 'localhost'               #the local host
userid = 'demo'                    #the userid
pswd   = 'demo'                    #the password    
db     = 'demo'                    #the name of the mysql database

try:
    conn = mydb.connect(host=host,user=userid,password=pswd,database=db)     #connect to host     
except mydb.Error as e:
    print("Could not establish connection", e, '<br>') 
    exit()

print('<br><br>')

cursor1 = conn.cursor()                         #create a cursor 
sql     = 'select * from student'               #create your query
cursor1.execute(sql);                           #execute the query
result  = cursor1.fetchall()                    #get all the rows at once
print(result)

print('<br><br>')

cursor2 = conn.cursor()                         #create cursor 2 
sql2    = 'select * from instructor'            #create your query 2
cursor2.execute(sql2);                          #execute the query
result2  = cursor2.fetchall()                   #get all the rows at once
print(result2)

cursor1.close()                             #close the cursor 1
cursor2.close()                             #close the cursor 2
conn.close()                                #close the connection


#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/staff/sultan/public_html/cgi-bin/python')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================