TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

April 19, 2024, 02:31:30 AM

Login with username, password and session length
Members
  • Total Members: 3,885
  • Latest: Growner
Stats
  • Total Posts: 195,164
  • Total Topics: 21,219
  • Online today: 108
  • Online ever: 3,540 (September 03, 2022, 01:38:54 AM)
Users Online
  • Users: 0
  • Guests: 74
  • Total: 74

Advice about php article not displaying the desired results

Started by woe, October 18, 2007, 08:39:34 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

woe

Hello,
  I am new to SMF and TP, but understand the basics of PHP and MySQL.  That said, I have the following code which is not displaying the desired results.
  The HTML code is working and displays the column headers.  The while statement is not displaying anything.
  I'm sure that it is something simple, but any suggestions would be greatly appreciated.


/* Make connection to Database */
$dbh = mysql_connect($dbhost, $dbusername, $dbpassword) or die ( "<H3>Server unreachable</H3>");
mysql_select_db($default_dbname) or die ( "<H3>database not existent</H3>");

/* check connection */
if (!$dbh) {
  die('Could not connect: ' . mysql_error());
}
?>
<table cellpadding="0" cellspacing="0" border="0" width="100%" style="table-layout: fixed;">
  <tr>
    <td> Name </td>
    <td> Race </td>
    <td> Class </td>
    <td> Vocation </td>
    <td> Note </td>
  </tr>
<?php
  $result 
mysql_query('SELECT Name, Race, Class, Sex, Rank, Vocation, Note FROM roster'$dbh);
    
/* fetch object array */
    
while ($row mysql_fetch_row($result)) {
      
$vocation $row[5];
      echo 
"<tr>";
        echo 
"<td> $row[0] </td>"/* Name */
        
echo "<td> $row[1] </td>"/* Race */
        
echo "<td> $row[2] </td>"/* Class */
        
echo "<td> $vocation_array[$vocation] </td>"/* Vocation */
        
echo "<td> $row[11] </td>"/* Note */
      
echo "</tr>";
    }
  
/* free result set */
  
mysql_free_result($result);
  
mysql_close();
  
?>

</table>

jacortina

I really haven't even attempted to use the 'alternate syntax' form of PHP in Articles.

Is this table in the same Database as your SMF/TP Tables?

woe


G6Cad

I edited your first post and placed the code in code tags, the reason is that if there is any HTML code added, they will display wrong in the post if you miss to place them in the code tags.

jacortina

OK. Well, make sure it's a PHP Article, and try (of course, you have to supply the DB values or include the globals for them):

$dbh = mysql_connect($dbhost, $dbusername, $dbpassword) or die ( "<H3>Server unreachable</H3>");
mysql_select_db($default_dbname) or die ( "<H3>database not existent</H3>");
if (!$dbh) die('Could not connect: ' . mysql_error());

echo '
<table cellpadding="0" cellspacing="0" border="0" width="100%" style="table-layout: fixed;">
  <tr>
    <td> Name </td>
    <td> Race </td>
    <td> Class </td>
    <td> Vocation </td>
    <td> Note </td>
  </tr>';
 
  $result = mysql_query('SELECT Name, Race, Class, Sex, Rank, Vocation, Note FROM roster', $dbh);
    /* fetch object array */
    while ($row = mysql_fetch_row($result)) {
      $vocation = $row[5];
      echo "<tr>";
        echo "<td> $row[0] </td>"; /* Name */
        echo "<td> $row[1] </td>"; /* Race */
        echo "<td> $row[2] </td>"; /* Class */
        echo "<td> $vocation_array[$vocation] </td>"; /* Vocation */
        echo "<td> $row[11] </td>"; /* Note */
      echo "</tr>";
    }
  /* free result set */
  mysql_free_result($result);

  echo '</table>';

mysql_close();

woe

Guess I should have specified that this code is the first lines of the php code, where 'host name' is actually the full host name.

$dbhost = 'host name';
$dbusername = 'user name';
$dbpassword = 'password';
$default_dbname = 'default dbname';

error_reporting(0);


Changing the HTML code to be in an echo block did not have any effect on the output.

The article is a php article.

I appreciate your help with this.

jacortina

You need to put those lines before the lines of the block I gave.

But you have to put the real values in. Try this and see of the array print yields any results.

$dbhost = 'host name';
$dbusername = 'user name';
$dbpassword = 'password';
$default_dbname = 'default dbname';

error_reporting(0);

$dbh = mysql_connect($dbhost, $dbusername, $dbpassword) or die ( "<H3>Server unreachable</H3>");
mysql_select_db($default_dbname) or die ( "<H3>database not existent</H3>");
if (!$dbh) die('Could not connect: ' . mysql_error());

echo '
<table cellpadding="0" cellspacing="0" border="0" width="100%" style="table-layout: fixed;">
  <tr>
    <td> Name </td>
    <td> Race </td>
    <td> Class </td>
    <td> Vocation </td>
    <td> Note </td>
  </tr>';
 
  $result = mysql_query('SELECT Name, Race, Class, Sex, Rank, Vocation, Note FROM roster', $dbh);
    /* fetch object array */
    while ($row = mysql_fetch_row($result)) {

print ( '<pre>' )  ;
print_r($row);
print ( '</pre>' )  ;

      $vocation = $row[5];
      echo "<tr>";
        echo "<td> $row[0] </td>"; /* Name */
        echo "<td> $row[1] </td>"; /* Race */
        echo "<td> $row[2] </td>"; /* Class */
        echo "<td> $vocation_array[$vocation] </td>"; /* Vocation */
        echo "<td> $row[6] </td>"; /* Note */
      echo "</tr>";
    }
  /* free result set */
  mysql_free_result($result);

  echo '</table>';

mysql_close();

woe

The print array is not yielding any results.

This is very strange. :-\

If the database is not being accessed you would think there would be an error.

jacortina

OK. Are all the column names correct (including upper/lower case)? And the table name?

woe

I feel stupid.
One of the column names was not capitalized, even though all the other were.

Sometimes it just takes another set of eyes to point out your mistakes. :-)