php - Why does my mysql query always return an empty result? -


i'm trying read 1 row database table "cond" using id of row. in case have 1 row id=1. problem code returns $result empty. i've been trying fix long time , tried pdo or mysqli knowledge of coding limited. how can read row properly?

this php code:

$response = array(); require_once __dir__ . '/db_connect.php'; $db = new db_connect();     if (isset($_get["pid"])) { $pid = $_get['pid']; $result = mysql_query("select *from cond id = $pid");  if (!empty($result)) {     if (mysql_num_rows($result) > 0) {          $result = mysql_fetch_array($result);          $product = array();         $product["id"] = $result["id"];         $product["name"] = $result["name"];         $product["mon"] = $result["mon"];         $product["tue"] = $result["tue"];         $product["wed"] = $result["wed"];         $product["thu"] = $result["thu"];         $product["fri"] = $result["fri"];         $product["sat"] = $result["sat"];         $product["sun"] = $result["sun"];         $product["version"]=$result["version"];          // success         $response["success"] = 1;          // user node         $response["product"] = array();          array_push($response["product"], $product);          // echoing json response         echo json_encode($response);     } else {         // no product found         $response["success"] = 0;         $response["message"] = "no product found. result=0";          // echo no users json         echo json_encode($response);     } } else {     // no product found     $response["success"] = 0;     $response["message"] = "no product found.result=empty";      // echo no users json     echo json_encode($response); } } else { // required field missing $response["success"] = 0; $response["message"] = "required field(s) missing";  // echoing json response echo json_encode($response); }  ?>   

and java code:

list<namevaluepair> params2 = new arraylist<namevaluepair>(); params2.add(new basicnamevaluepair("pid", "1"));  jsonobject json = jsonparser.makehttprequest(url_product_detials, "get", params2); log.d("single product details", json.tostring()); 

thanks!

edit: using suggested pdo

after being suggested use pdo uploaded next code:

<?php  $id=$_post["pid"];   try { require_once 'conf.php'; $conn = mysqlconnector(); $stmt = $conn->prepare('select * cond id = :id'); $stmt->execute(array('id' => $id)); $result = $stmt->fetchall();  if ( count($result) ) {  foreach($result $row) {      echo json_encode($result);  }} else { echo "no rows returned."; } } catch(pdoexception $e) { echo 'error: ' . $e->getmessage(); } ?> 

but error:

error parsing data org.json.jsonexception: end of input @ character 0 of  threadid=12: thread exiting uncaught exception (group=0x40cd7930) fatal exception: asynctask #1 java.lang.runtimeexception: error occured while executing doinbackground() @ android.os.asynctask$3.done(asynctask.java:299) @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:352) @ java.util.concurrent.futuretask.setexception(futuretask.java:219) @ java.util.concurrent.futuretask.run(futuretask.java:239) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) @ java.lang.thread.run(thread.java:856)  caused by: java.lang.nullpointerexception @ com.goout.listclubs$getproductdetails.doinbackground(listclubs.java:456) @ com.goout.listclubs$getproductdetails.doinbackground(listclubs.java:1) @ android.os.asynctask$2.call(asynctask.java:287) @ java.util.concurrent.futuretask.run(futuretask.java:234) ... 4 more   activity com.goout.listclubs has leaked window com.android.internal.policy.impl.phonewindow$decorview{410fb410 v.e..... r......d 0,0-480,144} added here    android.view.windowleaked: activity com.goout.listclubs has leaked window com.android.internal.policy.impl.phonewindow$decorview{410fb410 v.e..... r......d 0,0-480,144} added here @ android.view.viewrootimpl.<init>(viewrootimpl.java:354) @ android.view.windowmanagerglobal.addview(windowmanagerglobal.java:216) @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:69) @ android.app.dialog.show(dialog.java:281) @ com.goout.listclubs$getproductdetails.onpreexecute(listclubs.java:435) @ android.os.asynctask.executeonexecutor(asynctask.java:586) @ android.os.asynctask.execute(asynctask.java:534) @ com.goout.listclubs.oncreate(listclubs.java:87) @ android.app.activity.performcreate(activity.java:5104) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) @ android.app.activitythread.performlaunchactivity(activitythread.java:2262) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2358) @ android.app.activitythread.access$600(activitythread.java:153) @ android.app.activitythread$h.handlemessage(activitythread.java:1247) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5227) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:511) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:795) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:562) @ dalvik.system.nativestart.main(native method) 

so guess php code wrong?

an empty response means there error in query.

mysql won't tell error unless asked explicitly. so, have check result of every mysql function interacting server , if result false - check mysql_error().

also, using outdated mysql library. quit , start using pdo, has built-tin error reporting.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -