0

I'm trying to display my table, t2d_10 from database, chr10 using PHP. My table has 5 columns which is rs1, rs2, rs3, rs4, rs5. This is my code:

<?php 

 // set database server access variables: 
 $host = "localhost"; 
 $user = "root"; 
 $pass = ""; 
 $db = "chr10";

 // open connection 
 $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 

 // select database 
 mysql_select_db($db, $connection) or die ("Unable to select database!"); 

 // create query 
 $query = "SELECT * FROM t2d_10"; 

 // execute query 
 $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

 // see if any rows were returned 
 if (mysql_num_rows($result) > 0) { 
     // yes 
     // print them one after another 
     echo "<table cellpadding=10 border=1>"; 
     while($row = mysql_fetch_row($result)) { 
         echo "<tr>"; 
         echo "<td>".$row['rs1']."</td>"; 
         echo "<td>".$row['rs2']."</td>"; 
         echo "<td>".$row['rs3']."</td>"; 
         echo "<td>".$row['rs4']."</td>";
         echo "<td>".$row['rs5']."</td>";
         echo "</tr>"; 
     } 
     echo "</table>"; 
 } 
 else { 
     // no 
     // print status message 
     echo "No rows found!"; 
 } 

 // free result set memory 
 mysql_free_result($result); 

 // close connection 
 mysql_close($connection); 

?>

Error:

Notice: Undefined index: rs1 in C:\wamp\www\ch\run_db.php on line 28

Notice: Undefined index: rs2 in C:\wamp\www\ch\run_db.php on line 29

Notice: Undefined index: rs3 in C:\wamp\www\ch\run_db.php on line 30

Notice: Undefined index: rs4 in C:\wamp\www\ch\run_db.php on line 31

Notice: Undefined index: rs5 in C:\wamp\www\ch\run_db.php on line 32

I'm fairly new to this PHP. Can anyone please help me with this? Thanks in advance.

nOOr
  • 13
  • 4

2 Answers2

2

The syntax highlighting and the error message both tell you exactly what's wrong.

You're missing some " characters in your code.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

mysql_fetch_row() will be an indexed array only.

Either use mysql_fetch_array() or mysql_fetch_assoc()

Check this: Difference between mysql_fetch_array and mysql_fetch_row?

Community
  • 1
  • 1
Mr X
  • 1,637
  • 3
  • 29
  • 55