Listing tables

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Citizen99
Forum Commoner
Posts: 32
Joined: Wed Dec 24, 2003 6:52 am
Location: Where noone understand me...

Listing tables

Post by Citizen99 »

I have little problem. I write script, it allows to select table and show it, but on the begining when there is no tb_name selected - alert "SELECT * FROM sql_error" appear. Id like to put there alert "please select table" or smthnig if no tb_name is selected how to do that ?

Code: Select all

<?

$host='localhost'; 
$kto='me'; 
$pass='pass'; 
$db_name='db'; 

$moja=mysql_connect($host,$kto,$pass) 
or die("Nie mozna polaczyc sie z serverem"); 
mysql_select_db("$db_name") 
or die("Nie mozna sie polaczyc z baza"); 

print "<FORM>";
print "<SELECT name='tb_name'>\n"; 
$listatab = mysql_list_tables($db_name); 
while ($row = mysql_fetch_row($listatab))&#123; 
foreach ($row as $tb_list) 
&#123; 
print "<OPTION value='$tb_list'>$tb_list</OPTION>\n"; 
&#125; 
&#125; 

print "<INPUT type='submit' value=' Wybierz '>";
print "</SELECT></FORM><BR>\n";

$sql="SELECT * FROM $tb_name"; 
print "<B>Zapytanie:</B> $sql <BR>\n";
    
$wynik=mysql_query($sql) or die("sql_error"); 

print "<TABLE border =1><TR bgcolor=gray>\n"; 

$pole = mysql_list_fields($db_name, $tb_name, $moja); 
$kolumna = mysql_num_fields($pole); 
   
   for ($i = 0; $i < $kolumna; $i++) 
   print ("<TD>".mysql_field_name($pole, $i)."</TD>\n"); 
   
   while ( $costam=mysql_fetch_row($wynik))&#123; 
   
   print "<TR>\n"; 
   foreach ($costam as $cosinnego) 
   &#123; 
   print "<TD>$cosinnego</TD>\n"; 
   &#125; 
   &#125; 

mysql_close($moja); 

?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

//....skipped
if(!empty($tb_name)){
  $sql="SELECT * FROM $tb_name"; 
  print "<B>Zapytanie:</B> $sql <BR>\n"; 
  $wynik=mysql_query($sql) or die("sql_error");
}else{
  die("Wybierz tablizi");
}
//...skipped
Citizen99
Forum Commoner
Posts: 32
Joined: Wed Dec 24, 2003 6:52 am
Location: Where noone understand me...

Post by Citizen99 »

thx very much :] works very good
Post Reply