我的工作在PHP和MySQL的第一次。我遇到了mysqli的问题,我无法弄清楚我做错了什么。mysqli的错误FETCH_ASSOC()
我得到其中您可以查看这里的错误:http://www.polancophp.com/polanco-oop.php 或
Fatal error: Call to a member function fetch_assoc() on a non-object in E:\HostingSpaces\jacobspd\polancophp.com\wwwroot\polanco-oop.php on line 37
下面是代码:
<?php
class Member {
public $memberid;
public $businessname;
public $username;
public $email;
public $memberurl;
public $addr1;
public $addr2;
public $city;
public $state;
public $country;
public $postalcode;
public $domain;
public $firstname;
public $lastname;
public function __construct($InMemberID)
{
$this->get_data($InMemberID);
}
public function get_data($InMemberID){
$connect_str = new mysqli('xxx','xxx','xxx','xxx');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query_str = 'SELECT * FROM members WHERE MemberID = $InMemberID';
$result = $connect_str->query($query_str);
echo $result;
while (($row = $result->fetch_assoc()) !== null) {
$businessname = $row["BusinessName"];
$username = $row["UserName"];
$email = $row["LoginEmail"];
$memberurl = "";
$addr1 = $row["Address1"];
$addr2 = $row["Address2"];
$city = $row["City"];
$state = $row["State"];
$country = $row["Country"];
$postalcode = $row["PostCode"];
$domain = "polancophp.com";
$firstname = $row["FirstName"];
$lastname = $row["LastName"];
}
/* close connection */
mysqli_close($connect_str);
}
public function get_member_url(){
echo $this->domain.'/'.$this->memberurl;
}
public function get_member_email(){
echo $this->email;
}
public function get_member_address(){
echo $this->addr1.' '.$this->city.', '.$this->state.' '.$this->country.' '.$this->postalcode;
}
public function get_member_fullname(){
echo $this->firstname.' '.$this->lastname;
}
}
$Member = new Member(2);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Polanco's First Object Orientated Programming</title>
<link rel="stylesheet" type="text/css" href="common.css" />
<style type="text/css">
.pll {padding-left:10px}
</style>
</head>
<body>
<h1>Member Details</h1>
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td>Name:</td>
<td class="pll"><?php $Member->get_member_fullname(); ?></td>
</tr>
<tr>
<td>Email:</td>
<td class="pll"><?php $Member->get_member_email(); ?></td>
</tr>
<tr>
<td>Profile URL:</td>
<td class="pll"><a href="<?php $Member->get_member_url(); ?>"><?php $Member->get_member_url(); ?></a></td>
</tr>
<tr>
<td>Profile Address:</td>
<td class="pll"><?php $Member->get_member_address(); ?></td>
</tr>
</table>
</body>
</html>
任何帮助将不胜感激。
错误提示$ result不是mysqli_result,它通常意味着查询失败。尝试使用mysqli_error()打印错误,看看你是否得到了:'$ result = $ connect_str-> query($ query_str)或者die('Error:'。$ connect_str-> error());' – 3ventic
感谢分享你的mysql登录凭证。您可能想要更改您的服务器上的**现在** ... –
我已更改服务器凭据.. :) – neojakey