Login  |  Register
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome, Guest. Please login or register.
Did you miss your activation email?

May 28, 2020, 09:49:10 PM

Login with username, password and session length
Members
Stats
  • Total Posts: 190002
  • Total Topics: 20832
  • Online Today: 128
  • Online Ever: 917
  • (January 21, 2020, 09:02:26 AM)
Users Online
Users: 0
Guests: 78
Total: 78

Author Topic: Advice about php article not displaying the desired results  (Read 4689 times)

0 Members and 1 Guest are viewing this topic.

woe

  • Guest
Advice about php article not displaying the desired results
« on: October 18, 2007, 01: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.


Code: [Select]
/* 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>
« Last Edit: October 18, 2007, 02:06:58 PM by G6 »

jacortina

  • Guest
Re: Advice about php article not displaying the desired results
« Reply #1 on: October 18, 2007, 01: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?

woe

  • Guest
Re: Advice about php article not displaying the desired results
« Reply #2 on: October 18, 2007, 02:01:28 PM »
No, the data is in a separate database.

Offline G6Cad

  • Friends
  • *
  • Posts: 12643
    • FamiljeGodis
Re: Advice about php article not displaying the desired results
« Reply #3 on: October 18, 2007, 02: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.

jacortina

  • Guest
Re: Advice about php article not displaying the desired results
« Reply #4 on: October 18, 2007, 02: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):

Code: [Select]
$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

  • Guest
Re: Advice about php article not displaying the desired results
« Reply #5 on: October 18, 2007, 02: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.

Code: [Select]
$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.
« Last Edit: October 18, 2007, 02:24:13 PM by woe »

jacortina

  • Guest
Re: Advice about php article not displaying the desired results
« Reply #6 on: October 18, 2007, 03: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.

Code: [Select]
$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

  • Guest
Re: Advice about php article not displaying the desired results
« Reply #7 on: October 18, 2007, 04: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.

jacortina

  • Guest
Re: Advice about php article not displaying the desired results
« Reply #8 on: October 18, 2007, 04:23:28 PM »
OK. Are all the column names correct (including upper/lower case)? And the table name?

woe

  • Guest
Re: Advice about php article not displaying the desired results
« Reply #9 on: October 18, 2007, 04: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. :-)