1

I'm trying to make a table that shows the results from a query of MySQL, but I'm having a hard time to get it right...

I had the PHP code to show the content of the database table with this script;

<?php   
 // Grab the data from our people table
 $sql = "SELECT * FROM people ORDER BY ID";
 $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error());
 while ($row = mysql_fetch_assoc($result)) {
   echo "<div class=\"picture\">";
   echo "<p>";
   // Note that we are building our src string using the filename from the database
   echo "<img src=\"content/uploads/" . $row['filename']
         . "\" alt=\"\" height=\"125\" width=\"200\" /><br />" . "<br />";
   echo $row['fname'] . " " . "<br />" . "<br />";
   echo "</p>";
   echo "</div>";
 }
?>

but that of course doesn't have tables which is pretty ugly as it displays everything underneath each other... so I tried to make a table for it and after a lot of research I found a script which should have displayed the content but I can't seem to implement it into my own code and ended up with the error:

Could not access DB: No database selected

Using this code:

<?php
$sql="SELECT * FROM people ORDER BY ID";
$result=mysql_query($sql) or die ("Could not access DB: " . mysql_error());
$num=mysql_numrows($result);mysql_close();?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td>
<font face="Arial, Helvetica, sans-serif">Value1</font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif">Value2</font>
</td>
</tr>
<?php
$i=0;while ($i < $row) {$f1=mysql_fetch_assoc($result,$i,"field1");
$f2=mysql_fetch_assoc($result,$i,"field2");
$f3=mysql_fetch_assoc($result,$i,"field3");
$f4=mysql_fetch_assoc($result,$i,"field4");
$f5=mysql_fetch_assoc($result,$i,"field5");?>
<tr>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font>
</td>
</tr>
<?php
$i++;}
?>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
FGOD
  • 103
  • 2
  • 10
  • 2
    Please, [don't use `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 use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). You will also want to [Prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Nov 10 '14 at 13:31
  • 1
    How are you connecting to your database? It seems that you have missing code for that. – Jay Blanchard Nov 10 '14 at 13:32
  • i have a seperated conn.php to connect to the database at the beginning of the page... that holds all the database login info and works with the first code. i know i should try to use PDO or MySQLi but as a noob this i learning to get it right first and when i have it implemented i will look into the mySQLi codes and see what needs changing ;) – FGOD Nov 10 '14 at 13:38
  • 1
    Is there any error checking in conn.php? Add error reporting to the top of your file(s) right after your opening ` – Jay Blanchard Nov 10 '14 at 13:40
  • this is in my conn.php, so no error reporting edit: i'm doing my best learning it but i'm having a hard time not to be overwhelmed,although i'm doing pretty good so far and am suprised i got much working myself – FGOD Nov 10 '14 at 13:42
  • 1
    Please do not dump code in comments, they are nearly unreadable like that. Edit your original post to add the new content. – Jay Blanchard Nov 10 '14 at 13:42

1 Answers1

1

Not sure what is going on here

mysql_fetch_assoc($result,$i,"field1")

Mysql_fetch_assoc only accepts one argument

The correct way to use it is as demonstrated in the php man page

while ($row = mysql_fetch_assoc($result)) 
{?>
  <tr>
    <td>
      <font face="Arial, Helvetica, sans-serif"><?php echo $row['value1']; ?></font>
    </td>
    <td>
      <font face="Arial, Helvetica, sans-serif"><?php echo $row['value2']; ?></font>
   </td>
  </tr>
<?php
}

If you had errors and warnings turned on, then you would get helpful error messages telling you what was wrong with your code. It is always recommended to turn them on for development.

Community
  • 1
  • 1
Anigel
  • 3,435
  • 1
  • 17
  • 23
  • it is indeed that part that gives the error, but after editing it into what you suggested, but that left me with a blank page... after some modifications it showed the pictures but still no text info with the pictures and it gives me again the no database selected. the table is created underneath the pictures with the no database selected error and it doesn't show the fname and lname rows from the database – FGOD Nov 10 '14 at 14:09
  • If the database columns are called fname and lname you will need to change cvalue1 and value2 to be those names instead – Anigel Nov 10 '14 at 14:16