Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8897835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:27:06+00:00 2026-06-15T00:27:06+00:00

I’m having a weird problem of connection between Java, PHP and MySQL. I’m not

  • 0

I’m having a weird problem of connection between Java, PHP and MySQL.

I’m not having direct access to my MySQL database on the webserver through Java. So, I converted the mechanism to run a PHP script, which does the job for me and return the results to my Java Application.

It’s working pretty fine for most of the queries. But I’m not getting expected results when the queries include character fields in the query.

I include the PHP and Java Code snippets for reference.

Please find me the thing, where I’m doing wrong…

PHP Script on the server…

<?php
$query=$_POST["query"];
echo" Query = ".$query."\n";
if($query){
$link=mysql_connect("mysql2.000webhost.com","a7946109_other","mypassword");
if($link){
mysql_select_db("a7946109_other");
$result=mysql_query($query);
if($result){
while($row=mysql_fetch_row($result)){ // for each row in results
foreach($row as $value){                           // for each column in row
echo "\t".$value;
}
echo"\n";
}
}
}else{
echo " MySQL connect failed ! \n";
}
}else{
echo " No Query ! \n";
}
exit();  // exit without auto_append_file
?>

Java Program…

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.applet.*;
import java.security.*;

public class MySQL_POST {
    public static void main(String r[]){
    HttpURLConnection conn=null;
    try{
    URL url=new URL("mywebsite.com/postdb.php");
    String agent="Applet";
    String query="query=" + r[0];
    String type="application/x-www-form-urlencoded";
    conn=(HttpURLConnection)url.openConnection();
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty( "User-Agent", agent );
    conn.setRequestProperty( "Content-Type", type );
    conn.setRequestProperty( "Content-Length", ""+query.length());

    OutputStream out=conn.getOutputStream();
    out.write(query.getBytes());
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String inputLine;
    while((inputLine=in.readLine())!=null){
        System.out.print(inputLine+"\n");
    };
    in.close();
    int rc = conn.getResponseCode();
    System.out.print("Response Code = "+rc+"\n");
    String rm=conn.getResponseMessage();
    System.out.print("Response Message = "+rm+"\n");
    }catch(Exception e){
    e.printStackTrace();
    }finally{
    conn.disconnect();
    }
    }

    }

I Have a table named TEST, with fields SID INT(3), SNAME VARCHAR(20), SLNAME VARCHAR(20)

It has got two entries in the table.

When I execute the following queries using Java, I get different types of outputs as follows.

> java MYSQL_POST "SELECT * FROM TEST"

 Query = SELECT * FROM TEST
 1 Test1 Test1L
 2 Test2 Test2L
Response Code = 200
Response Message = OK

Which is want I expected.

> java MYSQL_POST "SELECT SNAME FROM TEST WHERE SID = 1"

 Query = SELECT SNAME FROM TEST WHERE SID = 1
 Test1
Response Code = 200
Response Message = OK

Which is also as I expected.

Here comes the weird one.

> java MYSQL_POST "SELECT SID FROM TEST WHERE SNAME = 'Test1'"

  Query = SELECT SID FROM TEST WHERE SNAME = \'Test1\'
 Response Code = 200
 Response Message = OK

Which doesn’t result in any result, which as expected should be 1

The queries, which involve characters like SNAME = 'Test1' or any other or not working. Except these queries, others are fine.

Please anyone help me in fixing this.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:27:07+00:00Added an answer on June 15, 2026 at 12:27 am

    The server running your PHP script has the magic quotes setting on. This automatically escapes quotes in incoming data from POST/GET/etc with backslashes. Using magic quotes is not recommended and they have been deprecated since PHP 5.4.0. Disable them and add proper sanitation to your code – it’s a huge security risk to execute queries straight from external input.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've tracked down a weird MySQL problem to the two different ways I was
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have been unable to fix a problem with Java Unicode and encoding. The
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.