Jump to content


Photo

Storing php syntax variables in SQL


  • Please log in to reply
8 replies to this topic

#1 recset

recset
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 10 September 2006 - 10:12 PM

I have the following problem, I am trying to store a php path in a SQL table (the field is a 'TEXT' field), db structure is latin2.

Here is the short path that i am trying to store (the code is actually a parameter for a link):

wisdom2.php?MAXIMID=<?php echo $row_menu['ID']; ?>

However, I get this error message when I try to run a page that tries to extract the php code:

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 '? ORDER BY AUTHOR ASC' at line 1

Now, if I were to simply write the same piece of code in a php page, it works fine, does anyone have an answer to why it is not working when the piece of code is stored in a SQL Database?

(Pls don't respond w/ the obvious answer,  ;) I need it to be this way for a reason .... )

Thank you,

#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 10 September 2006 - 11:50 PM

Do you mean this is in your SQL statement, similar to this:

$sql = "INSERT INTO table (path) VALUES ('wisdom2.php?MAXIMID=<?php echo $row_menu['ID']; ?>')";

If so, then it's because of the single quotes around 'ID'.  Have you tried to use htmlentities() on the string first of all, that works?

That would insert:

wisdom2.php?MAXIMID=&lt;?php echo $row_menu[&#039;ID&#039;]; ?&gt;

Or just escaping the single quotes by doubling them up, like so:

$sql = "INSERT INTO table (path) VALUES ('wisdom2.php?MAXIMID=<?php echo $row_menu[''ID'']; ?>')"; // notice two single quotes around ID

Let me know how you get on.
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 recset

recset
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 11 September 2006 - 02:32 PM

Actually its not a Select statement, its actually meant to be added as a url parameter after an existing link.

For example on the page wisdom2.php, there is a link:
www.blanksite.com/URL PARAMETER here


The URL PARAMETER, is brought from the database which i am trying to get working.




#4 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 11 September 2006 - 02:36 PM

what is in the db and what is the final result you are looking for?

Please post exacting examples, not descriptions :)

e.g.: In the column 'column', the value is:
xyz

I would like to output:
abc


#5 recset

recset
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 11 September 2006 - 04:33 PM

Sorry, about that.

Ok, in the db is:
'wisdom2.php?MAXIMID=<?php echo $row_menu['ID']; ?>

What I want the final result is to change the url of the browser window (when clicked) to:
'wisdom2.php?MAXIMID=<?php echo $row_menu['ID']; ?>

the php tag would actually be a number of a recordset (depending on the row menu).

Thus, that output would be something like:
'wisdom2.php?MAXIMID=5





#6 recset

recset
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 16 September 2006 - 02:24 PM

Can anyone help out with this problem???? Please Help, thank you!!!

#7 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 16 September 2006 - 04:58 PM

Change it to:
'wisdom2.php?MAXIMID=%d
then do this:
echo sprintf($the_thing_you_got_from_the_db,$row_menu['ID']);


#8 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 16 September 2006 - 09:13 PM

when you have this error:

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 '? ORDER BY AUTHOR ASC' at line 1

-it is always complaining about a mysql_query(); statement somewhere when you get that error statement.  When it says line 1 like that, it usually means that the mysql_query() is located in an include file.  The error is not caused by the code you presented.  Is there an include coded in the first line of your source code?

you should be able to search your source code for "ORDER BY AUTHOR ASC" and that will tell you the source code line the error occurred on.

#9 recset

recset
  • Members
  • PipPip
  • Member
  • 29 posts

Posted 17 September 2006 - 01:03 AM

Mainewoods,

I found the line:

$query_quotes = sprintf("SELECT * FROM wisdom_backup WHERE MAXIMID = %s ORDER BY AUTHOR ASC", $colname_quotes);

The whole reason for this is to clear a url parameter that is added to the URL in the browser everytime a user 'votes' on a record.  Thus, if the user doesn't vote, then this line is irrelevant. But, when the user 'votes' it adds a URL parameter to the browser, so that if the user decides to vote on another record, it will count 2 votes (1 for the new vote hit, and 1 for the previous vote hit, because each url parameter is now in the url shown in the browser). 

So essentially, after the user votes, I just want to clear the URL parameter.

Does this make sense? Or am I over-thinking this?






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users