jspstorm Posted September 13, 2009 Share Posted September 13, 2009 Hi, I have written the following code to display posts from my forum in plain text, I'm hoping to end up with: Post #, Subject, Message, and Username. Currently, the script displays all this fine, but the username stored in the phpbb3_posts table is just the poster_id, a number which corresponds to user_id in the phpbb3_users table. So my question is, how do I make the script find the user_id which is equal to the poster_id of the post, and then find the username in the same row as the user_id it just found? <?php $username="xxxxx"; $password="xxxxx"; $database="phpbb3"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM phpbb3_posts"; $result=mysql_query($query); $num=mysql_numrows($result); echo "<b><center>Database Output</center></b><br><br>"; $i=0; while ($i < $num) { $field1=mysql_result($result,$i,"post_id"); $field2=mysql_result($result,$i,"post_subject"); $field3=mysql_result($result,$i,"post_text"); $field4=mysql_result($result,$i,"poster_id"); $usernm=mysql_query("SELECT user_id, username FROM phpbb3_users WHERE echo "<b>$field1<br>$field2</b><br>$field3<br>$field4<hr><br>"; $i++; } mysql_close(); ?> http://123.255.41.116/test.php --> This is the current result of the script. Link to comment https://forums.phpfreaks.com/topic/174049-mysql-searching-with-php/ Share on other sites More sharing options...
ngreenwood6 Posted September 13, 2009 Share Posted September 13, 2009 Please use the code tags its alot easier to read. it is the "#" symbol incase you didnt know. Any ways try this: <?php $username="xxxxx"; $password="xxxxx"; $database="phpbb3"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM phpbb3_posts"; $result=mysql_query($query); echo "<b><center>Database Output</center></b><br><br>"; while($row = mysql_fetch_array($result)) { $field1 = $row['post_id']; $field2 = $row['post_subject']; $field3 = $row['post_text']; $field4 = $row['poster_id']; $username_query = 'SELECT user_id, username FROM phpbb3_users where user_id='.$field4.; $username_result = mysql_query($result); echo '<b>'.$field1.'<br>'.$field2.'</b><br>'.$field3.'<br>'.$field4.'<hr><br>'.$username_result['username']; } mysql_close(); ?> edit: forgot to put the ['username'] on the end Link to comment https://forums.phpfreaks.com/topic/174049-mysql-searching-with-php/#findComment-917469 Share on other sites More sharing options...
ngreenwood6 Posted September 13, 2009 Share Posted September 13, 2009 I just noticed a syntax error in my code so I figured I would update since I couldnt edit it anymore: <?php $username="xxxxx"; $password="xxxxx"; $database="phpbb3"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM phpbb3_posts"; $result=mysql_query($query); echo "<b><center>Database Output</center></b><br><br>"; while($row = mysql_fetch_array($result)) { $field1 = $row['post_id']; $field2 = $row['post_subject']; $field3 = $row['post_text']; $field4 = $row['poster_id']; $username_query = 'SELECT user_id, username FROM phpbb3_users where user_id='.$field4; $username_result = mysql_query($result); $field5 = $username_result['username']; echo '<b>'.$field1.'<br>'.$field2.'</b><br>'.$field3.'<br>'.$field4.'<hr><br>'.$field5; } mysql_close(); ?> Link to comment https://forums.phpfreaks.com/topic/174049-mysql-searching-with-php/#findComment-917486 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.