TinyPortal

Development => Resources => Topic started by: woe on October 18, 2007, 08:39:34 PM

Title: Advice about php article not displaying the desired results
Post by: woe on October 18, 2007, 08:39:34 PM
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>
Title: Re: Advice about php article not displaying the desired results
Post by: jacortina on October 18, 2007, 08:55:30 PM
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?
Title: Re: Advice about php article not displaying the desired results
Post by: woe on October 18, 2007, 09:01:28 PM
No, the data is in a separate database.
Title: Re: Advice about php article not displaying the desired results
Post by: G6Cad on October 18, 2007, 09:07:59 PM
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.
Title: Re: Advice about php article not displaying the desired results
Post by: jacortina on October 18, 2007, 09:12:19 PM
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();
Title: Re: Advice about php article not displaying the desired results
Post by: woe on October 18, 2007, 09:20:46 PM
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.
Title: Re: Advice about php article not displaying the desired results
Post by: jacortina on October 18, 2007, 10:11:06 PM
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();
Title: Re: Advice about php article not displaying the desired results
Post by: woe on October 18, 2007, 11:19:46 PM
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.
Title: Re: Advice about php article not displaying the desired results
Post by: jacortina on October 18, 2007, 11:23:28 PM
OK. Are all the column names correct (including upper/lower case)? And the table name?
Title: Re: Advice about php article not displaying the desired results
Post by: woe on October 18, 2007, 11:33:51 PM
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. :-)