0
<?php 
mysql_connect("localhost", "root", "pass") or 
  die("Could not connect: " . mysql_error());

mysql_select_db("smcs"); 

$result = mysql_query("SELECT * FROM others");  
$row = mysql_fetch_row($result);
echo $row[0]; 
?>

Each time I echo anything it shows number 1.

I don't know if there is a 1 stuck from other file in the input stream?

4
  • Welcome to Super User! Please don't use Super User for programming questions. Your question will automatically be migrated to Stack Overflow, our sister site for developers... Apr 2, 2012 at 22:07
  • How to show the whole row?? assume I have three column "id" "author" "topic".
    – Iman25
    Apr 2, 2012 at 22:08
  • Enumerate over $row with a for loop, so you get $row[1], $row[2] and so on. You might also be interested in the end of the example here for a better approach. I've posted a whole answer below... Apr 2, 2012 at 22:09
  • Make sure to check the mysql_fetch_assoc together with extract($row), it makes it a lot easier. :) Apr 2, 2012 at 22:17

1 Answer 1

1

1 is the first value from the first row from your table.

Hint: Identity column.


Don't use * in queries, you'll end up selecting more data than you'll want, which is slower.


If you want to get all the values, enumerate over $row with a for loop.

You might also be interested in the end of the example here for a better approach:

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//       then create $userid, $fullname, and $userstatus so you don't need $row.
while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

// Make sure that we be nice to our valuable memory.
mysql_free_result($result);
2
  • the code above is just to test but what I really want to do is to view each entry in the table from the db and put on table on webpage and put beside each row a radio bottom to modify the row. I'm in progress if you have any info that could help. I intend to build simlp cms
    – Iman25
    Apr 3, 2012 at 2:20
  • @lman25: Feel free to ping me on chat (type @Tom) in chat.stackexchange.com/rooms/118/root-access. Apr 3, 2012 at 6:45

Not the answer you're looking for? Browse other questions tagged .