如何解决mysqli_fetch_array()期望参数1为mysqli_result,php中的字符串 [英] How to solve mysqli_fetch_array() expects parameter 1 to be mysqli_result,string in php

查看:352
本文介绍了如何解决mysqli_fetch_array()期望参数1为mysqli_result,php中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是php的新手,我被困在给定特定ID的情况下找到max(id).

I'm new to php and I get stuck to find the max(id) by given a specific id.

活动A 中,有一个listView.当列表检测到长按时,它将检查id是否为max id.如果是,则将其删除;否则,将显示无法删除列表.

In activity A, there has a listView. When the list detect long press, it will check whether the id is the max id. If yes, it will deleted, if not, it will display list cannot be deleted.

这是我尝试过的.

listViewUpdate.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                    public boolean onItemLongClick(final AdapterView<?> p, View v, final int po, long id) {

iD = details1.get(po).getID();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Delete");
builder.setMessage("Are you sure you want to delete?");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int ii) {
checkMaxID(ID,iD); // ID is foreign key
// delete(iD);
objadapter.removeItem(po);                            

    }
});

public void checkMaxID(final int foreignKey,final String iD)
{
class check extends AsyncTask<Void,Void,String>{
// ProgressDialog loading;
@Override
protected void onPreExecute() {
 super.onPreExecute();
// loading = ProgressDialog.show(Edit_Staff.this,"Updating...","Wait...",false,false);
    }

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
// loading.dismiss();
Toast.makeText(getActivity(), s, Toast.LENGTH_LONG).show();
}

@Override
protected String doInBackground(Void... params) {
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put(Configs.KEY_ID, iD);
hashMap.put(Configs.KEY_TWD, String.valueOf(foreignKey));
RequestHandler rh = new RequestHandler();
String s = rh.sendPostRequest(Configs.URL_CHECK_ID, hashMap);
return s;
    }
}

check ue = new check();
ue.execute();
    }

checkID.php

<?php 
    if($_SERVER['REQUEST_METHOD']=='POST'){

        //Getting values

        $id = $_GET['id'];
        $foreignKey = $_GET['twd'];

        //Creating an sql query
        $sql = "SELECT MAX(id) FROM work_details WHERE twd = '$foreignKey'";
        $row = mysqli_fetch_row($sql);
        $maxId=$row[0];

        //Importing our db connection script
        require_once('dbConnect.php');

        //Executing query to database
        if($id==$maxId){

        $sql = "DELETE FROM work_details WHERE id=$id;";    

        }else{
            echo 'list cannot be deleted';
        }

        //Closing the database 
        mysqli_close($con);
    }
?>

当我在确认对话框中单击是"时,出现错误.

When I click yes in the confirmation dialog, I get error.

错误

有人可以帮忙吗?谢谢

推荐答案

在此代码中,您没有执行查询,因此无法提取.

In this code you did not execute the queries so you cannot fetch.

尝试这种方法:

<?php
    if(isset($_GET['id'], $_GET['twd'])){
        /*Importing our db connection script*/
        require_once('dbConnect.php');
        /* check connection */
        if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }

        $sql = sprintf("SELECT MAX(id) as MaxId FROM work_details WHERE twd = '%s'", 
                       mysqli_real_escape_string($_GET['twd']));

        if ($result = mysqli_query($con, $sql)) {
            /* fetch associative array */
            if ($row = mysqli_fetch_row($result)) {
                if($row[0] === $_GET['id']){
                    $sql = sprintf("DELETE FROM work_details WHERE id='%s';", 
                                  mysqli_real_escape_string($_GET['id']));
                    if ($result = mysqli_query($con, $sql)) {
                        echo 'success';
                    }else{
                        echo 'failed';
                    }
                }

            }

            /* free result set */
            mysqli_free_result($result);
        }
        /* close connection */
        mysqli_close($con);
    }
?>

这篇关于如何解决mysqli_fetch_array()期望参数1为mysqli_result,php中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆