gpt4 book ai didi

php - undefined variable : query_params

转载 作者:行者123 更新时间:2023-11-29 17:52:52 28 4
gpt4 key购买 nike

有人可以帮我解决这个错误吗?因为,代码的结果是 json 格式的成功。但仍然有错误“ undefined variable :query_params”。

我想用这种 json 格式在我的 android 程序中制作 ListView 。在我的 android 中,它无法从我的 comments.php 中显示成功的 json 格式。我想,也许是因为这个错误“ undefined variable :query_params”。

enter image description here

这是我的comments.php

<?php


require("config.inc.php");

//initial query
$query = "Select * FROM comments";

//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}


$rows = $stmt->fetchAll();


if ($rows) {
$response["success"] = 1;
$response["message"] = "Post Available!";
$response["posts"] = array();

foreach ($rows as $row) {
$post = array();
$post["post_id"] = $row["post_id"];
$post["username"] = $row["username"];
$post["title"] = $row["title"];
$post["message"] = $row["message"];


//update our repsonse JSON data
array_push($response["posts"], $post);
}

// echoing JSON response
echo json_encode($response);


} else {
$response["success"] = 0;
$response["message"] = "No Post Available!";
die(json_encode($response));
}

?>

这是我的 android 代码,用于从 comments.php 读取 json 格式以制作 ListView 。 ListView 没有任何显示。

ReadComments.java

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ReadComments extends ListActivity {


private ProgressDialog pDialog;


private static final String READ_COMMENTS_URL = "http://192.168.56.101/webservice/comments.php";


private static final String TAG_SUCCESS = "success";
private static final String TAG_TITLE = "title";
private static final String TAG_POSTS = "posts";
private static final String TAG_POST_ID = "post_id";
private static final String TAG_USERNAME = "username";
private static final String TAG_MESSAGE = "message";

private JSONArray mComments = null;
private ArrayList<HashMap<String, String>> mCommentList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// note that use read_comments.xml instead of our single_post.xml
setContentView(R.layout.read_comments);
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// loading the comments via AsyncTask
new LoadComments().execute();
}

public void addComment(View v) {
Intent i = new Intent(ReadComments.this, AddComment.class);
startActivity(i);
}


public void updateJSONdata() {


mCommentList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);

try {
mComments = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);


String title = c.getString(TAG_TITLE);
String content = c.getString(TAG_MESSAGE);
String username = c.getString(TAG_USERNAME);


HashMap<String, String> map = new HashMap<String, String>();

map.put(TAG_TITLE, title);
map.put(TAG_MESSAGE, content);
map.put(TAG_USERNAME, username);


mCommentList.add(map);


}

} catch (JSONException e) {
e.printStackTrace();
}
}


private void updateList() {
ListAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.single_post, new String[] { TAG_TITLE, TAG_MESSAGE,
TAG_USERNAME }, new int[] { R.id.title, R.id.message,
R.id.username });

setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}

public class LoadComments extends AsyncTask<Void, Void, Boolean> {

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ReadComments.this);
pDialog.setMessage("Loading Comments...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

@Override
protected Boolean doInBackground(Void... arg0) {
updateJSONdata();
return null;

}

@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pDialog.dismiss();
updateList();
}
}
}

最佳答案

您看到“ undefined variable ”错误是因为您试图在定义变量应包含的内容之前在 execute() 语句中使用变量 $query_params。参见 this answer .

您发送到 execute() 语句的 $query_params 也不需要,看看您是如何在 $query 中不使用任何参数的。参见 this PHP manual on PDO Statement .

因此,要修复您的错误,只需更改此:

$result = $stmt->execute($query_params);

对此:

$result = $stmt->execute();

关于php - undefined variable : query_params,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21691901/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com