0

I am trying to set up a json object.array to retrieve the data from this mysql query. the data required is posted from java and that seems to work fine, but I get parsing errors. I tried testing the php file and I get the "Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\GetVerse.php on line 8" . previously I had a ';' at the end of the sql query and it said unexpected ';' at the end of line 7. now it i get the error message for line 8: $row = mysql_fetch_array($result); . What seems to be the problem?

<?php
mysql_connect("localhost","root","password");
mysql_select_db("hftwmvirtualdb");
$Booknum = mysql_real_escape_string($_POST['Booknum']); 
$Chapternum = mysql_real_escape_string($_POST['Chapternum']); 
$Versenum = mysql_real_escape_string($_POST['Versenum']); 
$result = mysql_query("SELECT VERSETEXT FROM booktable WHERE BIBLEID = '".$Booknum."' AND CHAPTERID = '".$Chapternum."' AND VERSENO = '".$Versenum."' LIMIT 0,1"
$row = mysql_fetch_array($result);
echo json_encode($row); 
mysql_close();  
?>
3
Contributors
10
Replies
11
Views
5 Years
Discussion Span
Last Post by pritaeas
0

Is it possible to check whether the php script is correct without the running of the java code to post the required data?

Edited by benson.tairuakinsola

0

Is it possible to check whether the php script is correct

If you use a smart IDE, then you can prevent such typos easily. See this thread.

Edited by pritaeas

0

I corrected that error & After checking the script through a browswer I'm now receiving the following warning messages:


    1. Notice: Undefined index: Booknum in C:\wamp\www\GetVerse.php on line 4

    1. Notice: Undefined index: Chapternum in C:\wamp\www\GetVerse.php on line 5

    1. Notice: Undefined index: Versenum in C:\wamp\www\GetVerse.php on line 6

    1. Notice: Undefined variable: sql in C:\wamp\www\GetVerse.php on line 8

    1. Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in C:\wamp\www\GetVerse.php on line 8

    1. Notice: Undefined variable: output in C:\wamp\www\GetVerse.php on line 9

I assume, 1-3 are because I did not submit the post data but does is the same reason for messages 4-6?

0

1 - 3, you're correct.

5, will be because your query's incorrect due to the missing $_POST parameter values.

4 and 6 do not seem to appear to correspond with the code you have posted, so it's not possible to tell. I cannot see variables called $sql or $output.

0

I have corrected a table name in the sql and updated the PHP code. I have previously created a similar php script that gets all data in a table & encodes in Json; with line 8-11 the same. Why is is causing so trivial when i'm trying to mysql query to select a cell. The new PHP Code as stated:

<?php
mysql_connect("localhost","root","password");
mysql_select_db("hftwmvirtualdb");
$Booknum = mysql_real_escape_string($_POST['Booknum']); 
$Chapternum = mysql_real_escape_string($_POST['Chapternum']); 
$Versenum = mysql_real_escape_string($_POST['Versenum']); 
$sql = mysql_query("SELECT 'VERSETEXT' FROM 'booktable' WHERE 'BOOKID' = $Booknum AND 'CHAPTERID' = $Chapternum AND 'VERSENO' = $Versenum");
while($row=mysql_fetch_assoc($sql)) $output[]=$row;
  print(json_encode($output));
  mysql_close();
?>

I thought output was defined in line 8: "while($row=mysql_fetch_assoc($sql)) $output[]=$row;" so that it can be encoded by Json array/object. If this code is incorrect, How do I get the result of the query for parsing?

Edited by benson.tairuakinsola

0

ok so after adding echo mysql_error(); before the while statement. I recieved:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''booktable' WHERE 'BOOKID' = '' AND 'CHAPTERID' = AND 'VERSEID' =' at line 1
Which means the $sql didn't read the post data. I thought that was the correct syntax

0

$sql = mysql_query("SELECT 'VERSETEXT' FROM 'booktable' WHERE 'BOOKID' = $Booknum AND 'CHAPTERID' = $Chapternum AND 'VERSENO' = $Versenum");

Should be:

$sql = mysql_query("SELECT `VERSETEXT` FROM `booktable` WHERE `BOOKID` = $Booknum AND `CHAPTERID` = $Chapternum AND `VERSENO` = $Versenum");

In short, replace the single quotes with backticks. Use single quotes around values, backticks around tables and columns.

Since the POST is empty, show us your form or ajax call too.

Edited by pritaeas

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.