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.

6
  • mysql_select_db($connection) for one thing. Commented Jun 19, 2014 at 3:04
  • @Fred-ii-: No, that would be wrong. The OP's is right. php.net/manual/en/function.mysql-select-db.php Commented Jun 19, 2014 at 3:04
  • @LightnessRacesinOrbit Duh, sorry. My mistake lol I meant mysql_select_db($db,$connection) Commented Jun 19, 2014 at 3:06
  • Thanks, @Fred-ii-. I've already edited it. But there are some errors at $result. Undefined index of rs1, rs2, rs3, rs4, rs5. What should I use instead? Commented Jun 19, 2014 at 3:15
  • @Fred-ii-: Well, the second argument is optional, as explained on the same link.... Commented Jun 19, 2014 at 9:05

2 Answers 2

2

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

You're missing some " characters in your code.

Sign up to request clarification or add additional context in comments.

3 Comments

OP modified the question with missing ", wondering why. Edit: Undefined index now.
Thanks, @LightnessRacesinOrbit. I've already edited it. But there are some errors at $result. Undefined index of rs1, rs2, rs3, rs4, rs5. What should I use instead?
I'm not going to go through your problems one by one. SO is not a helpdesk. Consider asking a friend or chat room to help you.
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.