Created
October 3, 2012 05:47
-
-
Save cofearabi/3825265 to your computer and use it in GitHub Desktop.
(php) connect mysql database and display all fields and records of table.cgi-program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<META HTTP-EQUIV="content-type" CONTENT="text/html; | |
charset=utf-8"> | |
</head> | |
<?php | |
$server = '127.0.0.1'; | |
$username = 'root'; | |
$password = 'root'; | |
$link = mysql_connect($server, $username, $password); | |
if (!$link) { | |
die('can not connect : ' . mysql_error()); | |
} | |
echo 'successfully connected'; | |
$db_selected = mysql_select_db('stock_db',$link); | |
if (!$db_selected){ | |
die('fail to select database'.mysql_error()); | |
} | |
$result = mysql_query('SELECT * FROM stock'); | |
if (!$result) { | |
die('query fail'.mysql_error()); | |
} | |
$numFields = mysql_num_fields($result); | |
print("<br>num of fields:"); | |
print($numFields); | |
$nameFields = array(); | |
for($i=0;$i<$numFields;$i++){ | |
$nameFields[] = mysql_field_name($result, $i); | |
} | |
// print($nameFields[0]); | |
// print($nameFields[1]); | |
// print($nameFields[2]); | |
// print($nameFields[3]); | |
print("<br>"); | |
print("<table border=1>"); | |
print("<tr>"); | |
for($j=0;$j<$numFields;$j++){ | |
print("<td>"); | |
print($nameFields[$j]); | |
print("</td>"); | |
} | |
print("</tr>"); | |
while ($row = mysql_fetch_assoc($result)) { | |
print("<tr>"); | |
for($j=0;$j<$numFields;$j++){ | |
print("<td>"); | |
print($row[$nameFields[$j]]); | |
print("</td>"); | |
} | |
print("</tr>"); | |
} | |
print("</table>"); | |
mysql_close($link); | |
?> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment