Jump to content
xisto Community
Sign in to follow this  
kakingho

Question~about Mysql ALTER TABLE `xxx` ORDER BY `no`;

Recommended Posts

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

ALTER TABLE `xxx` ORDER BY `no`;

:P

 

Notice from jlhaslip:
Modified title

Edited by jlhaslip (see edit history)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

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

Share this post


Link to post
Share on other sites

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.

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

Share this post


Link to post
Share on other sites

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;');

Share this post


Link to post
Share on other sites

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 codingquery = "SELECT * FROM tablename WHERE name='you'";return = mysql_query(query);i hope this helps... if not.. well too bad :P

Share this post


Link to post
Share on other sites

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:

'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').

Share this post


Link to post
Share on other sites

well~

 

mysql_query(ALTER TABLE `xxx` ORDER BY `no`;);
mysql_query(ALTER TABLE `xxx` ORDER BY `no`);
mysql_query('ALTER TABLE `xxx` ORDER BY `no`');
mysql_query('ALTER TABLE \'xxx\' ORDER BY \'no\'');
mysql_query("ALTER TABLE `xxx` ORDER BY `no`");
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. :)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

210158[/snapback]

So.....what's wrong with the code??

Please correct it~~ :)

Share this post


Link to post
Share on other sites

You need to provide more information. How can we tell what is wrong from a single line of code? What is the error message you are getting?? Do you even have a table called xxx and in that table is there a column called no?

Share this post


Link to post
Share on other sites

You need to provide more information.  How can we tell what is wrong from a single line of code?  What is the error message you are getting??  Do you even have a table called xxx and in that table is there a column called no?

210215[/snapback]


Of course i have that table and colunm.

I had said before

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

ALTER TABLE `xxx` ORDER BY `no`;

Actually, there is only a main code.

You asked the above question seem to be kidding me. ;)

Why don't you read my article completely before replying.

Why don't you ask me whether my connection password is correct or not.

 

Anyway, forgive my non-enough information. :P

Here is the whole file.

There is no error message with nothing happen.

<?$ip="localhost";$user="xxx";$pw="xxx";$link=mysql_connect($ip,$user,$pw)or die ("Could not connect"); mysql_select_db("xxx");mysql_query(ALTER TABLE `xxx` ORDER BY `no`);mysql_close($link);?>
But i can directly type and run this code in phpmyadmin's sql query

ALTER TABLE `xxx` ORDER BY `no`;

So I want to know whether php can use order function.

What's going on??! :)

Share this post


Link to post
Share on other sites

of course i will ask stupid questions if you simply post a statement like "what's wrong... Please correct..." Do you expect people to be mind readers? People need information to in order to resolve problems.

1) in your sql query statement you don't need the quotes around the table name or the column name (but this should't be causing the problem).
2) you talked about user name and password... well did you check them? do they match in your control panel? did you grant permissions to that user to your database you are trying to access?
3) if there is no error message how do you know there was an error? if you've already run the query in phpmyadmin, the table will already have been ordered according to the query. if you run the same query again, nothing will happen
4) make sure you are selecting the right database in your mysql_select_db function
5) the code looks fine to me, so it is not a coding issue
6) use mysql_error funtion so that it tells you what the error is (if there is one).
7) And last, I used the 'ALTER TABLE xxx ORDER BY yyy' function and it works fine.

<?$ip="localhost";$user="xxx";$pw="xxx";$link=mysql_connect($ip,$user,$pw) or die ("Error:".mysql_error()); mysql_select_db("xxx") or die ("Error:".mysql_error());mysql_query(ALTER TABLE `xxx` ORDER BY `no`) or die ("Error:".mysql_error());mysql_close($link);?>

Share this post


Link to post
Share on other sites

Here is my full report. :P

 

User name , password, permission,etc ..

all are normal~

 

1. can run successfully ( this code is for testing only)

<?$ip="localhost";$user="xxx";$pw="xxx";$link=mysql_connect($ip,$user,$pw) or die ("Error:".mysql_error()); mysql_select_db("xxx") or die ("Error:".mysql_error());//mysql_query(ALTER TABLE `xxx` ORDER BY `no`) or die ("Error:".mysql_error());mysql_query("UPDATE xxx SETip='test' ,password='test' where no=41");mysql_close($link);?>

2. can run this code in phpmyadmin's sql query directly
ALTER TABLE `xxx` ORDER BY `no`

3. cannot run this code in phpmyadmin's sql query and a error is shown

mysql_query(ALTER TABLE `xxx` ORDER BY `no`)
error message

#1064 - 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 'mysql_query(ALTER TABLE `xxx` ORDER BY `no`)' at line 1

4. After run this file, a error is shown.

<?$ip="localhost";$user="xxx";$pw="xxx";$link=mysql_connect($ip,$user,$pw) or die ("Error:".mysql_error()); mysql_select_db("xxx") or die ("Error:".mysql_error());mysql_query(ALTER TABLE `xxx` ORDER BY `no`) or die ("Error:".mysql_error());//mysql_query("UPDATE xxx SETip='test' ,password='test' where no=41");mysql_close($link);?>

error message:

Parse error: parse error, unexpected T_STRING in /home/xxx/public_html/test.php on line 9

line 9 means

mysql_query(ALTER TABLE `xxx` ORDER BY `no`) or die ("Error:".mysql_error());
* I manage the table into descending order before every testing.

* phpmyadmin version: 2.6.4-pl2

* php version: 4.3.10

* PERL version: 5.8.4

 

please help to solve this strange problem~~ :P

Share this post


Link to post
Share on other sites

Everyone mentioned it! You used

mysql_query(ALTER TABLE `xxx` ORDER BY `no`) or die ("Error:".mysql_error());
when you're supposed to have the quotes!

Try this on line 9.
mysql_query("ALTER TABLE 'xxx' ORDER BY 'no'") or die ("Error:".mysql_error());

P.S. Please understand this! Replace the line, OK?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.