-4

I want to display things from database in table in html. I found this example on somewhere on intertet and followed whole guide until I came there . It shows table and all but instead putting informations that are on database it just puts in "$data[1]" and "$data[0]". Thanks it advance

<html>
<head>
<title>Search data</title>
</head>
<body>
<table>
  <tr>
    <td align="center">EMPLOYEES DATA</td>
  </tr>
  <tr>
    <td>
      <table border="1">
      <tr>
        <td>NAME</td>            
        <td>ADDRESS</td>
      </tr>
<?php
//the example of searching data 
with the sequence based on the field name
//search.php
mysql_connect("localhost","root","");//database connection
mysql_select_db("employees");

$order = "SELECT * FROM data_employees ORDER BY name";
//order to search data
//declare in the order variable

$result = mysql_query($order);  
//order executes the result is saved
//in the variable of $result

while($data = mysql_fetch_row($result)){
  echo("<tr><td>$data[1]</td><td>$data[0]</td></tr>");
}
?>
    </table>
  </td>
</tr>
</table>
</body>
</html>
Typo boy
  • 85
  • 1
  • 9
  • 1
    If you can, you should [stop using `mysql_*` functions](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). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). You should also add error checking, such as `or die(mysql_error())` to your queries. Or you can find the issues in your current error logs. – Jay Blanchard Jun 05 '15 at 13:39
  • 2
    *"it just puts in "$data[1]" and "$data[0]"."* - is the file `.php` extension? server running? and is this part of the code? `with the sequence based on the field name` shouldn't be there. – Funk Forty Niner Jun 05 '15 at 13:41
  • **Install a web server** or if on a hosted site, rename your file to `file.php`. there; fixed. – Funk Forty Niner Jun 05 '15 at 13:50
  • are these comments even being read *Sam?* - @JayBlanchard – Funk Forty Niner Jun 05 '15 at 13:51
  • *I don't know Ralph.* Seems everyone left @Fred-ii- – Jay Blanchard Jun 05 '15 at 13:52
  • @Fred-ii- I've made both , .php and .html , server is running and it's still not working . I'm not good with this sql and php , it's new thing to me – Typo boy Jun 05 '15 at 13:53
  • Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Jun 05 '15 at 13:53
  • @Fred-ii- where it will report errors ? I can't see any reports of errors – Typo boy Jun 05 '15 at 13:57
  • if you're not seeing anything on screen, then you have server problems. check your logs. If this doesn't work `` then you'll know where to look. far as I'm concerned, your code checks out. minus the `with the sequence based on the field name` – Funk Forty Niner Jun 05 '15 at 13:58
  • anyway, you have another answer below, ask them http://stackoverflow.com/a/30668347/ – Funk Forty Niner Jun 05 '15 at 13:59
  • @Fred-ii- looks like server is fine . it says hello world – Typo boy Jun 05 '15 at 14:00
  • Do this now then ` Search data` followed by the rest of your code. – Funk Forty Niner Jun 05 '15 at 14:01
  • Guys i got it , a comment in line 19 . look at it , i forgot to type "//" . – Typo boy Jun 05 '15 at 14:02
  • I'm sorry , my english skills are not good , I didnt knew what that means – Typo boy Jun 05 '15 at 14:09

3 Answers3

0

You are calling array variables in double quotes, where array variable can't display like this as normal variables. They also considered as string, so it displays the array variables. There are two ways to display the actual value.

Way1:
echo "<tr><td>".$data[1]."</td><td>".$data[0]."</td></tr>";
Way2:
echo "<tr><td>{$data[1]}</td><td>{$data[0]}</td></tr>";

I Prefer way1 as the second way leads to low execution speed.

0

You see this

//the example of searching data 
with the sequence based on the field name
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

that should have been commented out.

I told you twice in comments.

"it just puts in "$data[1]" and "$data[0]"." - is the file .php extension? server running? and is this part of the code? with the sequence based on the field name shouldn't be there. – Fred -ii- 25 mins ago

and

if you're not seeing anything on screen, then you have server problems. check your logs. If this doesn't work then you'll know where to look. far as I'm concerned, your code checks out. minus the with the sequence based on the field name – Fred -ii- 8 mins ago

that is the error, not the other answer(s).

  • Let the record be clear here.

Error reporting told you where the error was, as I stated in comments.

Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything. Also add or die(mysql_error()) to mysql_query(). Probably get a deprecated notice, who knows. check for errors. – Fred -ii- 14 mins ago


Just an added note:

Your present code is open to SQL injection. Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
-2

this is really a standart question.....full of answers in the web. you need to call the values correctly...

while($data = mysql_fetch_row($result)){
  echo("<tr><td>".$data[1]."</td><td>".$data[0]."</td></tr>");
}

just like here:

Show values from a mySQL database table inside a html table in a page

but you should use mysqli:

http://tut.php-quake.net/en/mysql-php.html

Community
  • 1
  • 1
messerbill
  • 5,499
  • 1
  • 27
  • 38
  • It's still not working , but thanks for answer. – Typo boy Jun 05 '15 at 13:49
  • so try the following: `while($data = mysql_fetch_array($result)){ echo("".$data[1]."".$data[0].""); }` @Jay Blanchard: this is correct, but it is a bit dirty, so i never would use this – messerbill Jun 05 '15 at 13:53
  • but the post i posted does – messerbill Jun 05 '15 at 13:56
  • how you can see from the OP's voted "solved problem answer" this was no serverside problem.....but thanks to god you downrated all these other answers xD – messerbill Jun 05 '15 at 14:03
  • 1
    You can see from the OP's comments that he had a typo @messerbill so it was a syntax error. As I stated, none of the answers here would fix the OP's problem. You know you can delete this answer and get your points back. – Jay Blanchard Jun 05 '15 at 14:05