Welcome Guest ( Log In | Register)



2 Pages V   1 2 >  
Reply to this topicStart new topic
> Question~about Mysql, ALTER TABLE `xxx` ORDER BY `no`;
kakingho
post Nov 30 2005, 02:46 PM
Post #1


Member [Level 1]
****

Group: Members
Posts: 52
Joined: 17-July 05
From: hong kong
Member No.: 9,520



CODE
mysql_query(ALTER TABLE `xxx` ORDER BY `no`;);


Why i cant run the above code?!

But i can directly type and run this code in phpmyadmin's sql query
CODE
ALTER TABLE `xxx` ORDER BY `no`;


ohmy.gif


Notice from jlhaslip:
Modified title


This post has been edited by jlhaslip: Dec 2 2005, 03:13 PM
Go to the top of the page
 
+Quote Post
no9t9
post Nov 30 2005, 06:37 PM
Post #2


Privileged Member
*********

Group: Members
Posts: 773
Joined: 4-November 04
Member No.: 2,118



Well, assuming that what you have written here is exactly what you've put on your web page... then i can see that you should have quotes around your query string. Also, I think you don't need the semi colon in the query string.

I always use a variable as the query string. something like $query="blah"; and then mysql_query($query); I think this is better.
Go to the top of the page
 
+Quote Post
truefusion
post Nov 30 2005, 10:02 PM
Post #3


Ephesians 6:10-17
Group Icon

Group: [MODERATOR]
Posts: 1,893
Joined: 22-June 05
From: The World of Gentoo
Member No.: 8,528
T17 GFX Crew



My guess is cause you placed a semicolon inside the query, which makes php think you've ended the line. So, try this:

CODE
mysql_query(ALTER TABLE `xxx` ORDER BY `no`);
Go to the top of the page
 
+Quote Post
Spectre
post Dec 1 2005, 04:48 AM
Post #4


Privileged Member
*********

Group: Members
Posts: 874
Joined: 30-July 04
Member No.: 246



A semicolon can be placed within quotes and PHP will treat it simply as a string. As no9t9 noted, the query should be in quotes between the brackets of the mysql_query() function.

CODE
mysql_query('ALTER TABLE `xxx` ORDER BY `no`;');
Go to the top of the page
 
+Quote Post
Spectre
post Dec 1 2005, 04:53 AM
Post #5


Privileged Member
*********

Group: Members
Posts: 874
Joined: 30-July 04
Member No.: 246



Oh, it may also be worth noting that when sending queries to MySQL via PHP, they do not need to be terminated in themselves via a semicolon, as it is done automatically. So mysql_query('blah'); would have exactly the same result as mysql_query('blah;');
Go to the top of the page
 
+Quote Post
saga
post Dec 1 2005, 07:20 AM
Post #6


Premium Member
********

Group: Members
Posts: 165
Joined: 12-September 05
Member No.: 11,777



you should remember that mysql_query() is a function that accepts strings... therefore as what Spectre have said you should put it in between ' ' as you would in a normal string... but if you use ' inside your query then use the " instead...

like this one -> mysql_query("SELECT * FROM tablename WHERE name='you'");

or your could always code it this way which is more often used mehtod of coding

query = "SELECT * FROM tablename WHERE name='you'";
return = mysql_query(query);

i hope this helps... if not.. well too bad smile.gif
Go to the top of the page
 
+Quote Post
Spectre
post Dec 1 2005, 08:09 AM
Post #7


Privileged Member
*********

Group: Members
Posts: 874
Joined: 30-July 04
Member No.: 246



Certainly true, although I would still recommend using single quotes where possible and simply escaping any other single quotation marks which are required to appear in the query:

CODE
'SELECT * FROM tablename WHERE name=\'you\'';


Using double quotes slows the operation down. Although it probably wouldn't affect small sites or scripts that aren't used a great deal, those which operate constantly will start to notice it. Although it's sometimes easier and more practical to use double quotes, in my opinion, it's usually a better idea and general good coding practice to use single quotes and simply escape any instances required to appear in the string, as well as adding variables outside of it (eg. 'SELECT * FROM table WHERE field =\'' . $variable . '\' LIMIT 3').
Go to the top of the page
 
+Quote Post
kakingho
post Dec 2 2005, 06:36 AM
Post #8


Member [Level 1]
****

Group: Members
Posts: 52
Joined: 17-July 05
From: hong kong
Member No.: 9,520



well~

CODE
mysql_query(ALTER TABLE `xxx` ORDER BY `no`;);

CODE
mysql_query(ALTER TABLE `xxx` ORDER BY `no`);

CODE
mysql_query('ALTER TABLE `xxx` ORDER BY `no`');

CODE
mysql_query('ALTER TABLE \'xxx\' ORDER BY \'no\'');

CODE
mysql_query("ALTER TABLE `xxx` ORDER BY `no`");

CODE
mysql_query("ALTER TABLE 'xxx' ORDER BY 'no'");


I finished testing.
All of above cannot work!!
I want to know whether php can use order function. ohmy.gif
Go to the top of the page
 
+Quote Post
Spectre
post Dec 2 2005, 10:24 AM
Post #9


Privileged Member
*********

Group: Members
Posts: 874
Joined: 30-July 04
Member No.: 246



PHP communicates directly with MySQL, and provided the user you are accessing the database with has the correct privelages, any command can be executed. Which would lead me to think you're doing something wrong somewhere else in the code.