初心者なので、ご指導お願いします。
<?php// Mysql_class.phpclass Mysql{ private $localhost; private $root; private $password; public $database; public function __construct($localhost,$root,$password,$database){ //让下面的方式中,若要用到$localhost 变量 $this->localhost = $localhost; //就用$this->localhost 代替。 $this->root = $root; $this->password = $password; $this->database = $database; } public function Connect(){ mysql_connect ($this->localhost,$this-root,$this->password); mysql_select_db ($this->database); mysql_query ("set names utf8"); } public function Close(){ mysql_close(); } public function myarray($result){ //形参 return mysql_fetch_array($result); } public function myquery($sql){ return @mysql_query($sql); } public function myrows($result){ return mysql_num_rows($result); } public function myselect($users){ return $this->myquery("select * from $users"); }}$db = new Mysql("localhost","root","","stu_system");
<center><table cellpadding="18"> <tr> <th>id</th> <th>name</th> <th>sex</th> <th>phone</th> </tr><?php include_once "mysql_class.php"; $result = $db->myselect("users"); if(is_array($result)){ while($row=$db->myarray($result)){?> <tr> <td width="8%" align="center"> <?php echo $row['id'] ?></td> <td width="18%" align="center"><?php echo $row['name'] ?></td> <td width="8%" align="center"> <?php echo $row['sex'] ?></td> <td width="18%" align="center"><?php echo $row['phone'] ?></td> <td> <a href="modify.php? id = <?php echo $row['id'] ?>& name = <?php echo $row['name']?>& sex = <?php echo $row['sex'] ?>& phone= <?php echo $row['phone'] ?>"> 修改</a> <!-- ?什么意思 为GET传输方式--> <a href="delete.php? id = <?php echo $row['id'] ?>"> 删除</a> </td> </tr></table></center><?php } } else echo"no result"; mysql_close();?>
if(is_array($result)){ この判断には問題があります。
$result は配列ではなくリソースである必要があります
ブール値 false の var_dump(is_array($result)); が可能です。
次のように変更します: if($db->myrows($result)>0){
Connect メソッドは実行されません
myquery メソッドは実行されません
myselect メソッドはリソースを返します
Connect メソッドは実行されません
なし myquery メソッドの実行
myselect メソッドはリソースを返します
<?phpclass Mysql{ private $localhost; private $root; private $password; public $database; private $link; private $res; public function __construct($localhost,$root,$password,$database){ //让下面的方式中,若要用到$localhost 变量 $this->localhost = $localhost; //就用$this->localhost 代替。 $this->root = $root; $this->password = $password; $this->database = $database; } public function Connect(){ $this->link = mysql_connect($this->localhost, $this->root, $this->password); mysql_select_db ($this->database, $this->link ); mysql_query ("set names utf8"); } public function Close(){ mysql_close(); } public function myarray($result){ //形参 return mysql_fetch_array($result); } public function myquery($sql){ $this->res = mysql_query($sql) or die (mysql_error()); return $this->res; } public function myrows($result){ return mysql_num_rows($result); } public function myselect($users){ return $this->myquery("select * from $users"); }} $db = new Mysql("localhost","root","","stu_system");$db->Connect();<center><table cellpadding="18"> <tr> <th>id</th> <th>name</th> <th>sex</th> <th>phone</th> </tr><?php include_once "mysql_class.php"; $result = $db->myselect("users"); while($row=$db->myarray($result)){?> <tr> <td width="8%" align="center"> <?php echo $row['id'] ?></td> <td width="18%" align="center"><?php echo $row['name'] ?></td> <td width="8%" align="center"> <?php echo $row['sex'] ?></td> <td width="18%" align="center"><?php echo $row['phone'] ?></td> <td> <a href="modify.php? id = <?php echo $row['id'] ?>& name = <?php echo $row['name']?>& sex = <?php echo $row['sex'] ?>& phone= <?php echo $row['phone'] ?>"> 修改</a> <!-- ?什么意思 为GET传输方式--> <a href="delete.php? id = <?php echo $row['id'] ?>"> 删除</a> </td> </tr></table></center><?php } mysql_close();