+ -
当前位置:首页 → 问答吧 → 杜老师,16章一个问题一个例子报错,请回答

杜老师,16章一个问题一个例子报错,请回答

时间:2008-03-24

来源:互联网

先说一下,您第16章php和mysql接口,写的不错

3,修改记录报错,您看一下
<?
if (isset($_POST['btnModify'])){
//验证表单省略
$sql="update animl set where id='$_POST[id]'";
$result= mysqli_query($sql);
if($result){

echo"修改成功";

}else{

echo "修改失败";

}


}
//查询当前记录
$query= "select*from syt";
//执行查询
if ($result = $mysqli->query($query)) {
//显示返回记录行数
if($rseult->num_rows>0){
//如果有记录,显示记录内容
echo "<table border=1 >";
while($row= $result->fetch_array()) {
echo "<tr>";
echo "<td><input name='id' type='text' id='id' vaule='row[0]' /><td>";
echo "<td><input name='country' type='text' id='county' vaule='row[1]' /><td>";
echo "<td><input name='animal' type='text' id='animal' vaule='row[2]' /><td>";

echo "<td><input name='btnmodify' type='submit' id='btnModify' vaule='修改' /><td>";
echo "</trr>";
}
}
$result->close();
}
?>

报这个错误,什么原因
Fatal error: Call to a member function query() on a non-object in D:\www\phpjichu\mysql\mody.php on line 30

作者: 山日比   发布时间: 2008-03-23

请确认php.ini中mysqli.so或mysqli.dll选项是否打开。

作者: phpcast   发布时间: 2008-03-24

如果确认打开。
请确认$mysqli这个对象是否创建,格式如下:
$mysqli = new mysqli("localhost", "my_user", "my_password", "animal");

作者: phpcast   发布时间: 2008-03-24

谢谢您回复,我创建对象了,你看一下,还是不行啊
<?php
/**************************
@Filename: filename.php
@Version : 0.0.1
@Author  : leo
@Update  : 2007-11-01
@Content : PHP by Editplus
**************************/
//数据库参数
$host="localhost";
$user="root";
$pass="root";
$db="we";

//创建mysqli对象,打开数据库
$mysqli=new mysqli($hodt,$user,$pass,$db);


if (isset($_POST['btnModify'])){
//验证表单省略
$sql="update animl set where id='$_POST[id]'";
$result= mysqli_query($sql);
if($result){

echo"修改成功";

}else{

echo "修改失败";

}


}
//查询当前记录
$query= "select*from syt";
//执行查询
if ($result = $mysqli->query($query)) {
//显示返回记录行数
if($rseult->num_rows>0){
//如果有记录,显示记录内容
echo "<table border=1 >";
while($row= $result->fetch_array()) {
echo "<tr>";
echo "<td><input name='id' type='text' id='id' vaule='row[0]' /><td>";
echo "<td><input name='country' type='text' id='county' vaule='row[1]' /><td>";
echo "<td><input name='animal' type='text' id='animal' vaule='row[2]' /><td>";

echo "<td><input name='btnmodify' type='submit' id='btnModify' vaule='修改' /><td>";
echo "</trr>";
}
}
$result->close();


}


?>

作者: 山日比   发布时间: 2008-03-24

复制PHP内容到剪贴板
PHP代码:

<?php 
// set server access variables 
$host = "localhost"; 
$user = "root"; 
$pass = "root"; 
$db = "testdb"; 
// create mysqli object 
// open connection 
$mysqli = new mysqli($host, $user, $pass, $db); 
// check for connection errors 
if (mysqli_connect_errno()) { 
    die("Unable to connect!"); 

// if id provided, then delete that record 
if (isset($_REQUEST['id'])) { 
// create query to delete record 
    $query = "DELETE FROM symbols WHERE id = ".$_GET['id']; 
     
// execute query 
    if ($mysqli->query($query)) { 
    // print number of affected rows 
    echo $mysqli->affected_rows."条记录被影响"; 
    } 
    else { 
    // print error message 
    echo "Error in query: $query. ".$mysqli->error; 
    } 

// query to get records 
$query = "SELECT * FROM symbols"; 
// execute query 
if ($result = $mysqli->query($query)) { 
    // see if any rows were returned 
    if ($result->num_rows > 0) { 
        // yes 
        // print them one after another 
        echo "<table cellpadding=10 border=1>"; 
        while($row = $result->fetch_array()) { 
            echo "<tr>"; 
            echo "<td>".$row[0]."</td>"; 
            echo "<td>".$row[1]."</td>"; 
            echo "<td>".$row[2]."</td>"; 
            echo "<td><a href=".$_SERVER['PHP_SELF']."?id=".$row[0].">删除</a></td>"; 
            echo "</tr>"; 
        } 
    } 
    // free result set memory 
    $result->close(); 

else { 
    // print error message 
    echo "Error in query: $query. ".$mysqli->error; 

// close connection 
$mysqli->close(); 
?>

作者: phpcast   发布时间: 2008-03-24

请参考上例,希望你能顺利。

作者: phpcast   发布时间: 2008-03-24