MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old August 3rd, 2003, 06:15 AM
andyyylz andyyylz is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 14 andyyylz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Help Urgently Php/mysql

Hi All:
I am designing a web page using PHP/MYSQL to allow user to input any query you want. I set a var to store the SQL query users input and then use mysql_query($var) to execute.
But I will get error, if I run below:
SELECT title,price FROM paper WHERE periodicity='weekly' ORDER BY price DESC
Obviously, it runs well in command line. but if this statement is stored into a var, it will not work, just because the single quote can not be handled in variable.
How can sort out the problem
Anyone can help ME?????


Thank you all

Reply With Quote
  #2  
Old August 3rd, 2003, 09:31 AM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Hi Andy,

You should be able to store your query in a variable like so:

$var = "SELECT title, price FROM paper WHERE periodicity='weekly' ORDER BY price DESC"

What does your code look like? Can you post the snipper that's giving you problems? Also, when testing your code, be sure to echo out the actual contents of $var, so that you can see how the PHP engine is receiving it.
__________________
____________________________________________
Developer Shed Weekly Writer | DevArticles Forum Moderator
Build Your Own KlipFolio Klip With PHP
FrankManno.com - Under Construction
Design Interactive Group - Under Construction

Reply With Quote
  #3  
Old August 4th, 2003, 07:08 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Umm, and you might want to be careful about letting people execute whatever queries they want to. What if somebody types "DELETE FROM price" or "DROP TABLE price"? You should consider validating queries prior to submitting them to the mysql server (checking for vars that begin with DELETE or contain semicolons and additional text), and you should also make sure the database user your script logs in as has only the permissions it needs. (Chances are that it doesn't need DROP or ALTER, for example).

Reply With Quote
  #4  
Old August 4th, 2003, 07:46 AM
andyyylz andyyylz is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 14 andyyylz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I meet the problem is that MYSQL can not handle the single or double quotes correctly.

Reply With Quote
  #5  
Old August 4th, 2003, 08:20 AM
harvey_r01 harvey_r01 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Bournemouth
Posts: 37 harvey_r01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
try this...
[PHP]
<?php

if($request) {

$sql2 = mysql_query($sql) or die('Query Error'); // prob better to use $_GET['sql']

while ($row = mysql_fetch_object($sql2)) {

// Do Some Output using $row-> and field name

}
}
?>

<HTML>
<HEAD>
<TITLE>Sql Run</TITLE>
</HEAD>
<BODY>
<FORM name="request" method="post" action="<?=$PHP_SELF?>">
<INPUT type="text" name="sql">
</FORM>
</BODY>
</HTML>

I Think that should do it, but this is as long as you know what you want to output...

Harvey

Reply With Quote
  #6  
Old August 4th, 2003, 09:46 AM
andyyylz andyyylz is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 14 andyyylz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I set:
$var="select title,price FROM paper WHERE periodicity='weekly' ORDER BY price DESC"
Then transfer the value to next page, I use echo $var
It shows:
select title,price FROM paper WHERE periodicity=\'weekly\' ORDER BY price DESC
and inform me that there is error in \'weekly\' .
How to sort out it?

Reply With Quote
  #7  
Old August 4th, 2003, 10:48 AM
harvey_r01 harvey_r01 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Bournemouth
Posts: 37 harvey_r01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
try this...

$var = mysql_query("select title,price FROM paper WHERE periodicity='weekly' ORDER BY price DESC") or die ("Query Error:" . mysql_errno() . mysql_error());

You need to ensure that you specify your string into the mysql_query() function, so that it completes the query.

Hope this helps...

Harvey

Reply With Quote
  #8  
Old August 4th, 2003, 11:16 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
You might also try using the stripslashes function to remove slashes from in front of the quotes. Those're what's actually giving your mysql server problems.

Reply With Quote
  #9  
Old August 4th, 2003, 02:19 PM
andyyylz andyyylz is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 14 andyyylz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you very much. I sorted it out.
Thank all of you

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > Help Urgently Php/mysql


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway