Page 1 of 1

private messenging system (not tutorial) solved

Posted: Sun Jul 22, 2012 1:45 am
by Thunderbob
Hello forum,Temor and jacek,

before I get into my issue I just wanted to say I took Temor's advice and I completely scrapped the old code (that I used from a tutorial online somewhere) and am using a totally new one built from scrap. I decided to get some professional help and picked up a friend named Tim who's been helping me code the pages. The site is coming along extremely well and we fixed the (linking to the profile page) issue that we had a while back. Check it out when you're not busy eh?

Ok so now I am putting together a pm system for users to interact among each other without the whole world knowing.Of course I am here because everything is not coming along like milk and honey.

Here is the code I am working with.
<?php if($profileOwner):?>
            <div id="view4" class="tabcontent">
              

<h3><table width="800" border="0"> </h3>
<tr>
    <td> See who's trying to message you.</td>
</tr>
</table>
<table width="800" border="0"> 
<tr>
    <td><?php require_once"inbox.php"; ?></td>
</tr>
</table>
<br>
<?php 
require_once"dbconnect.php";
mysql_connect("secret launch code");
$sql ="SELECT * FROM pm_inbox WHERE userid ='$pid' ORDER by id DESC";
$result = mysql_query($sql);

$count = $mysql_num_rows($result);

?>

</table>
<table width="800" border="0"> 
<form name-"form1" method="post" action="inbox.php">
<tr>
    <td width="41" align ="center">#</td>
    <td width="490">Title</td>
    <td width="255">From</td>
</tr>
  <?php
   while($rows = mysql_fetch_array($result)){

?>
<?php if ($rows['viewed'] == 0) { // show messages in bold ?>
<tr>
    <td width="41" align="center"> <input type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>" /> </td>
    <td width="490"><a href"pm_view_in.php?in=>?php $rows echo $rows['id']; ?>"><b><?php echo $rows['title']; ?> </b> </a></td>
    <td width="255"><?php echo $rows['from_username']; ?></td>
</tr>
<?php } else if ($rows['viewed'] == 1) { ?>
<tr>
    <td width="41" align="center"> <input type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>" /></td>
    <td width="490"><a href"pm_view_in.php?in=>?php $rows echo $rows['id']; ?>"><?php echo $rows['title']; ?></a></td>
    <td width="255"><?php echo $rows['from_username']; ?></td>
</tr> 

<?php } ?>
<?php } ?>

<tr> 
<td colspan="3" align="center"> </td> <?php if (inboxMessages > 0) { ?> <input type="submit" name="delete" id="delete" value="delete selected messages" /> <?php } else { Print "There are no messages in your inbox";} ?> </td> 
</table>

</h3>
            </div>
<?php endif;?>
Code for the inbox.php
<?php 
session_start();
require_once 'dbconnect.php';
$sqlCommand = "SELECT id_user, username FROM fgusers3 WHERE username='" . $_SESSION['username'] . "'";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$pid = $row["id_user"];
$username = $row["username"];
}
mysqli_free_result($query);

// check for new messages
$sqlCommand = "SELECT COUNT(id) AS numbers FROM pm_inbox WHERE userid='$pid' AND viewed='0'";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$result = mysqli_fetch_assoc($query);

$inboxMessagesNew = $result['numbers'];

// check for all messages in the inbox
$sqlCommand = "SELECT COUNT(id) AS numbers FROM pm_inbox WHERE userid='$pid'";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$result = mysqli_fetch_assoc($query);

$inboxMessagesTotal = $result['numbers'];

// check for all messages in the outbox
$sqlCommand = "SELECT COUNT(id) AS numbers FROM pm_outbox WHERE userid='$pid'";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$result = mysqli_fetch_assoc($query);

$outboxMessages = $result['numbers'];

?>

<?php if (!$_SESSION['username']) { ?>
Private Messenger System: <a href="pm_inbox.php">Inbox</a> <?php if ($inboxMessagesNew > 0) { print "<b>(".$inboxMessagesNew.")</b>"; } else{} ?> <?php print $inboxMessagesTotal; ?>, <a href="pm_outbox.php">Outbox</a> <?php print $outboxMessages; ?>, <a href="pm_send.php">Send New Message</a> <?php } else { print "You must be logged in first"; }?>
The error: Fatal error: Function name must be a string in /home/content/66/9481266/html/source/testing.php on line 324

line 324 refers to
$count = $mysql_num_rows($result);

Re: private messenging system (not tutorial)

Posted: Sun Jul 22, 2012 1:51 am
by Helx
Remove the '$' in mysql_num_rows.
Thats the only thing I can see wrong.

Re: private messenging system (not tutorial)

Posted: Sun Jul 22, 2012 2:46 am
by Thunderbob
dope!

haha thanks

now I'm getting this

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/content/66/9481266/html/source/testing.php on line 324

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/content/66/9481266/html/source/testing.php on line 337

line 324 is
 mysql_num_rows($result);
line 337 is
 while($rows = mysql_fetch_array($result)){

I made a change
// show messages in bold ?>
I moved the ?> to the next line

Re: private messenging system (not tutorial)

Posted: Sun Jul 22, 2012 3:00 am
by Helx
Its a problem with '$result' which recurses to '$sql'. The one thing I noticed was this:
$sql ="SELECT * FROM pm_inbox WHERE userid ='$pid' ORDER by id DESC";
It needs to be `id` not id (back ticks)

Re: private messenging system (not tutorial)

Posted: Sun Jul 22, 2012 3:02 am
by Thunderbob
same errors =(

Thanks for taking your time out to help me (just wanted to say it before I forget).

Re: private messenging system (not tutorial)

Posted: Sun Jul 22, 2012 3:18 am
by Helx
Try making line 1 like this:
<?php if($profileOwner); ?>
EDIT:
Then please re-paste the code you have :)

Re: private messenging system (not tutorial)

Posted: Sun Jul 22, 2012 3:24 am
by Temor
As abcedea said the " expects parameter 1 to be resource, boolean given " error usually means the SQL statement contains an error.
Adding
echo mysql_error();
under the query that is failing will show you a more detailed error message.
Add it underneath where you call mysql_query.

/Edit; By the way. I encourage you to make a few changes to how you write your SQL.
  • Do not use *. Instead choose the information you want to get
  • Wrap your table and column names in backticks ( ` ).
  • Wrap your variables in curly brackets ( { } ).
$sql = "SELECT `username`,`first_name`,`last_name` WHERE `user_id` = {$user_id}";
Following these steps adds to the readability of the code and makes it easier to spot any errors.

Also note that you do not need quotes ( ' ) around variables containing an integer. They are only needed when the variable contains a string.

Re: private messenging system (not tutorial)

Posted: Sun Jul 22, 2012 4:26 am
by Helx
Temor wrote:Do not use *. Instead choose the information you want to get
Would you access the information the same way?

e.g.
echo $result["table"];
Or is there no need for that?

Re: private messenging system (not tutorial)

Posted: Sun Jul 22, 2012 6:44 am
by Thunderbob
solved it

it was the way I was connecting to the db.

thanks guys.

Re: private messenging system (not tutorial)

Posted: Sun Jul 22, 2012 3:21 pm
by Temor
Thunderbob wrote:solved it

it was the way I was connecting to the db.

thanks guys.
Glad you solved it.
abcedea wrote:
Temor wrote:Do not use *. Instead choose the information you want to get
Would you access the information the same way?

e.g.
echo $result["table"];
Or is there no need for that?
Yes, you're accessing it in the exact same way.
* = ALL
so if you have `first_name` and `last_name` in your table, * would select both. But for someone not familiar with your code, it's impossible to know what that query is getting. So that's why its better to show it by writing the names out.