mySQLi和PHP的问题

时间:2013-07-24 01:51:35

标签: php mysqli

我正在尝试创建一个内部邮件系统。

我的代码如下:

$useraccess = $loggedInUser->username; 
$sql = "SELECT id, user_name FROM ".$db_table_prefix."users WHERE   user_name='$useraccess'"; 
$query = $mysqli->query($sql) or die ("Error");   

while ($row = mysqli_fetch_array($query)) { 
$pid = $row["id"]; 
$username = $row["user_name"]; 
} 

$query->close();

$sqlCommand = "SELECT COUNT(id) AS numbers FROM ".$db_table_prefix."messages_inbox WHERE userid='$pid'"; 
$query = $mysqli->query($sql) or die ("Error");   
$result = mysqli_fetch_assoc($query); 
$inboxMessages = $result['numbers']; 
?>

<h1>Inbox</h1>

<?php
$sql="SELECT * FROM ".$db_table_prefix."messages_inbox WHERE userid='$pid' ORDER by id  DESC";
$result=$mysqli->query($sql);
$count=mysqli_stmt_num_rows($result);
?>
<p>
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<th style="text-align: left; ">Title</th>
<th style="text-align: right; ">Sender</th>
<th style="text-align: right; "><b>Actions</b></th>
</tr>
<?php
while($rows=mysqli_field_count($result)) {
?>
<?php if ($rows['viewed'] == 0) { // show messages in bold?>
<tr>
<td style="text-align: left; "><a href="messages-view-in.php?in=<?php echo $rows['id'];   ?>"><b><?php echo $rows['title']; ?></b></a></td>
<td style="text-align: right; "><?php echo $rows['from_username']; ?></td>
<td style="text-align: right; "><a href="messages-inbox-delete.php?message=<?php echo    $rows['id']; ?>">Delete</a></td>
</tr>
<?php } else if ($rows['viewed'] == 1) { ?>
<tr>
<td style="text-align: left; "><a href="messages-view-in.php?in=<?php echo $rows['id'];  ?>"><?php echo $rows['title']; ?></a></td>
<td style="text-align: right; "><?php echo $rows['from_username']; ?></td>
<td style="text-align: right; "><a href="messages-inbox-delete.php?message=<?php echo      $rows['id']; ?>">Delete</a></td>
</tr>
<?php } ?>
<?php } ?>
</table>
<?php if ($inboxMessages > 0) { ?>
<?php } else 
{ print "<div id=\"errors\">Currently you do not have messages in your Inbox</div>"; 
} ?>

这是我正在尝试显示来自数据库的数据的收件箱。它连接没有问题,否则它会抛出错误,所以我相信这是我的语法。

我收到以下错误。

Warning: mysqli_stmt_num_rows() expects parameter 1 to be mysqli_stmt, object given in /opt/lampp/htdocs/project/inbox.php on line 28

Warning: mysqli_field_count() expects parameter 1 to be mysqli, object given in /opt/lampp/htdocs/project/inbox.php on line 38

Notice: Undefined index: numbers in /opt/lampp/htdocs/project/inbox.php on line 20

1 个答案:

答案 0 :(得分:0)

使用mysqli_num_rows代替,如下所示:

$count=mysqli_num_rows($result);

参考文档:http://php.net/manual/en/mysqli-result.num-rows.php

要使用mysqli_stmt_num_rows,您需要更改几行:

$sql="SELECT * FROM ".$db_table_prefix."messages_inbox WHERE userid='$pid' ORDER by id  DESC";
if ($stmt = $mysqli->prepare($sql)) {
    mysqli_stmt_execute($stmt);
    mysqli_stmt_store_result($stmt);
    $count = mysqli_stmt_num_rows($stmt)
    $stmt->close();
}

以下是mysqli_stmt_num_rows的文档:http://php.net/manual/en/mysqli-stmt.num-rows.php