-3

Hi I'm having problems accessing my server i'm using mysql on my laptop apache2 and php. my problem is i can seem to connect to db but can't get any data out of the registered table inside the db. Thanks in advance always i'm running window7.

<?php
    $username = "root";
    $password = "deslap";
    $hostname = "localhost"; 
 #connection to the database seems to work and prints connected to MySQL
    $dbhandle = mysql_connect($hostname, $username, $password)or die("Unable to connect to MySQL");
    echo "<br />Connected to MySQL<br>";
 #select a database to work with
    $selected = mysql_select_db('registered',$dbhandle)or die("Could not select database");

 #execute the SQL query and return records.  
    $result = mysql_query("SELECT id, Name FROM registered");
    while($row = mysql_fetch_array($result))
    {
       echo "ID:".$row{'id'}." Name:".$row{'name'}."Email: ".$row{'Email'};
    }
?>
</body>
</html>
Desimon
  • 3
  • 2
  • 7
    Please, [don't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). *They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation)*. See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – Jay Blanchard Sep 26 '14 at 14:10
  • Add error reporting to the top of your file(s) right after your opening ` – Funk Forty Niner Sep 26 '14 at 14:12
  • Looking a little closer I wonder if the DB is named registered or the table is....or both? It seems that the DB is because your selection doesn't fail. Is the table name the same? – Jay Blanchard Sep 26 '14 at 14:20
  • 2
    *Hm...* yeah, there's been a few of those lately. @JayBlanchard - Error checking should determine that, whether it's correct or not. – Funk Forty Niner Sep 26 '14 at 14:22

1 Answers1

2

Change the curly brackets to box brackets (you can use either {} or [], but square brackets are conventional for working with array elements) -

echo "ID:".$row['id']." Name:".$row['Name']."Email: ".$row['Email'];

You're also selecting only 'id' and Name (change 'name' to 'Name') so Email will not be returned.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • 1
    *Both square brackets and curly braces can be used interchangeably for accessing array elements* - http://uk1.php.net/manual/en/language.types.array.php#99015 – George Sep 26 '14 at 14:13
  • True @George, but I like people to understand that they are working with array elements and use convention. – Jay Blanchard Sep 26 '14 at 14:15
  • 1
    A *dash* of error checking would be a fine addition to such a great recipe. Ye nailed it ;) – Funk Forty Niner Sep 26 '14 at 14:17
  • It didn't answer the question. Presumably the OP was aware they were working with arrays. If you felt the need to let the OP know this, there's a comments section. As it happens it now looks as though you *have* found the actual solution. – George Sep 26 '14 at 14:17
  • 1
    So, `("SELECT id, Name FROM registered")` to `("SELECT id, Email, Name FROM registered")` then. If OP can't figure it out ;) Gawd, it's not rocket science after all lol – Funk Forty Niner Sep 26 '14 at 14:19
  • I'm still a little slow this morning @George - I haven't complete my first cup of coffee. – Jay Blanchard Sep 26 '14 at 14:22
  • 1
    Enjoy your 2nd cup 'o coffee, I'm off to chase a lovely Italian woman with an Espresso in one hand and mine in the other ;) ciao. – Funk Forty Niner Sep 26 '14 at 14:28
  • Thank you Jay most helpful it did make a difference also with the error reporting it has moved it all on a bit for me thank you Fred for that. however now there are no errors so seems to be working but still not displaying any data from the database. – Desimon Sep 27 '14 at 09:02
  • @Desimon make sure to add error checking in the MySQL code too, adding an "or die" to `$result` and then using one of the functions to show the error like `mysql_error()`. *See my note above about switching from `mysql_` functions.* – Jay Blanchard Sep 29 '14 at 12:14