private messenging system (not tutorial) solved

Ask about a PHP problem here.
Post Reply
Thunderbob
Posts: 46
Joined: Sat Jun 30, 2012 12:31 pm

private messenging system (not tutorial) solved

Post 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);
Last edited by Thunderbob on Mon Jul 23, 2012 5:12 pm, edited 1 time in total.
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: private messenging system (not tutorial)

Post by Helx »

Remove the '$' in mysql_num_rows.
Thats the only thing I can see wrong.
Thunderbob
Posts: 46
Joined: Sat Jun 30, 2012 12:31 pm

Re: private messenging system (not tutorial)

Post 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
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: private messenging system (not tutorial)

Post 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)
Thunderbob
Posts: 46
Joined: Sat Jun 30, 2012 12:31 pm

Re: private messenging system (not tutorial)

Post by Thunderbob »

same errors =(

Thanks for taking your time out to help me (just wanted to say it before I forget).
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: private messenging system (not tutorial)

Post by Helx »

Try making line 1 like this:
<?php if($profileOwner); ?>
EDIT:
Then please re-paste the code you have :)
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: private messenging system (not tutorial)

Post 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.
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: private messenging system (not tutorial)

Post 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?
Thunderbob
Posts: 46
Joined: Sat Jun 30, 2012 12:31 pm

Re: private messenging system (not tutorial)

Post by Thunderbob »

solved it

it was the way I was connecting to the db.

thanks guys.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: private messenging system (not tutorial)

Post 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.
Post Reply