簡體   English   中英

如何在PHP Query中編寫LIKE語句?

[英]How to write a LIKE Statement in PHP Query?

我正在創建一個可以使用輸入單詞的最后一個字母檢索單詞的應用程序。 例如,在我的現有數據庫中,序列是Appl e > E lephan t > T rai n > N erv e > E ducatio n > N ewspape r >我將輸入R ecipe,因為這是前一個單詞的最后一個字母。 我的問題是我無法得到答復。 應用程序必須提供一個單詞,該單詞以前一個單詞的最后一個字母開頭。 所以在tblcheckword中響應必須是E mphasis或E mpire,因為在我的查詢中它是在rand()和LIMIT到1

SELECT word FROM tblcheckword WHERE word LIKE '$word.%' ORDER BY rand() NOT IN (SELECT ig_word FROM tbltempgame) LIMIT 1;

我正在使用LIKE語句來獲取輸入單詞的最后一個字母。 但我仍然得到錯誤。

這是我的錯誤

這是我的PHP文件“checkplsv2.php”

<?php
$con = mysqli_connect("localhost", "root", "1234", "check") or die ("Error ".mysqli_error($con));
$word = $_POST["word"];
$sql = "SELECT word FROM tblcheckword where word like '$word.%' order by rand() not in (select ig_word from tbltempgame) LIMIT 1";
$result = mysqli_query($con, $sql) or die("Error in selecting ".mysqli_error($con));

$response = array();
if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_array($result))
    {
        array_push($response, array("id"=>$row[0],"ig_word"=>$row[1],"meaning"=>$row[2]));
        $retrieve = $row[1];
    }
    echo json_encode(array("server_response" => $retrieve));
}else{
    echo "Retrieve failed";
}

mysqli_close($con);


?>

這是我的MainActivity.java

package com.example.keam.lastnato;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class MainActivity extends Activity {

String id;
String word;
String ig_word;
InputStream is = null;
String result = null;
String line = null;
String data = "";
TextView disp;
String url = "http://192.168.22.16/checkplsv2.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText e_word = (EditText) findViewById(R.id.editText1);
    Button select = (Button) findViewById(R.id.button1);
    select.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            word = e_word.getText().toString();
            select();
        }
    });
}

void select() {

    class readThis extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            nameValuePairs.add(new BasicNameValuePair("word", word));

            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(params[0]);
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                Log.e("pass 1", "connection success ");
            } catch (Exception e) {
                Log.e("Fail 1", e.toString());
                Toast.makeText(getApplicationContext(), "Invalid IP Address",
                        Toast.LENGTH_LONG).show();
            }

            try {
                BufferedReader reader = new BufferedReader
                        (new InputStreamReader(is, "iso-8859-1"), 8);

                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                is.close();
                result = sb.toString();
                Log.e("pass 2", "connection success ");
            } catch (Exception e) {
                Log.e("Fail 2", e.toString());
            }


            return result;
        }

        @Override
        protected void onPostExecute(String result) {

            try {
                JSONObject json_data = new JSONObject(result);
                ig_word = json_data.getString("server_response");
                Toast.makeText(getBaseContext(), "Name: " +   
                ig_word,Toast.LENGTH_SHORT).show();
                TextView disp = (TextView) findViewById(R.id.textView);
                disp.setText(ig_word);

            } catch (JSONException e) {
                Log.e("Fail 3", e.toString());
                Log.e("log_tag", "Failed data was: " + result);
            }

        }

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }

    }

    readThis hello = new readThis();
    hello.execute(new String[]{url});
    }
}

這是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.keam.lastnato.MainActivity">

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="39dp"
    android:padding="11dp"
    android:hint="Player's Turn"
    android:ems="10"
    android:inputType="text">

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="24dp"
    android:onClick="parseJSON"
    android:padding="11dp"
    android:text="Select" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />
</RelativeLayout>

這是我在WAMP phpmyadmin中的tblcheckword和tbltempgame

數據庫名稱=“檢查”

這是我在WAMP phpmyadmin中的tblcheckword和tbltempword

這是我的logcat錯誤。

09-04 19:23:52.051 11820-11820/com.example.keam.lastnato E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.keam.lastnato.MainActivity.access$super
09-04 19:23:52.051 11820-11820/com.example.keam.lastnato E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.keam.lastnato.MainActivity.access$super
09-04 19:23:52.051 11820-11820/com.example.keam.lastnato E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method com.example.keam.lastnato.MainActivity.access$super
09-04 19:23:52.051 11820-11820/com.example.keam.lastnato E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method com.example.keam.lastnato.MainActivity.access$super
09-04 19:23:52.061 11820-11820/com.example.keam.lastnato E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method com.example.keam.lastnato.MainActivity.access$super
09-04 19:23:52.071 11820-11820/com.example.keam.lastnato E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.keam.lastnato.MainActivity.access$super
09-04 19:23:52.071 11820-11820/com.example.keam.lastnato E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.keam.lastnato.MainActivity.access$super
09-04 19:23:52.071 11820-11820/com.example.keam.lastnato E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.keam.lastnato.MainActivity.access$super
09-04 19:23:52.081 11820-11820/com.example.keam.lastnato E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method com.example.keam.lastnato.MainActivity.access$super
09-04 19:23:52.091 11820-11820/com.example.keam.lastnato E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method com.example.keam.lastnato.MainActivity.access$super
09-04 19:23:52.091 11820-11820/com.example.keam.lastnato E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.keam.lastnato.MainActivity.access$super
09-04 19:24:01.960 11820-12003/com.example.keam.lastnato E/pass 1: connection success 
09-04 19:24:01.960 11820-12003/com.example.keam.lastnato E/pass 2: connection success 
09-04 19:24:01.960 11820-11820/com.example.keam.lastnato E/Fail 3: org.json.JSONException: Value Retrieve of type java.lang.String cannot be converted to JSONObject
09-04 19:24:01.960 11820-11820/com.example.keam.lastnato E/log_tag: Failed data was: Retrieve failed

你的sql不正確。

嘗試這個:

 $sql = "SELECT `word` FROM `tblcheckword`WHERE `word` LIKE ".$word."% NOT IN(select ig_word from tbltempgame)  ORDER BY RAND()  LIMIT 1";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM