9 January 2009


PHP 5 : Declaring class level variable : Syntax error, unexpected T_NEW on line ...

In OO PHP you cannot declare an object type variable and assign it a new instance of an object at the same time.
For example, you will get the error Syntax error, unexpected T_NEW on line if you were to use the following code

class MyClass
{
var $mychild = new ChildClass();
}

class ChildClass
{
var somevar = 'hello world';
}

The reason for this error is that when declaring a class level variable it cannot be assigned a new instance of an object. Instead you have to declare the variable and then in the constructor (or some other function) you have to assign a new object value to the variable.

So, to solve the above problem we would have to do the following:

class MyClass
{
var $mychild;
function __construct()
{
$this->mychild = new ChildClass();
}
}
class ChildClass
{
var somevar = 'hello world';
}

23 October 2008


#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 ...

Is it MySQL or phpMyAdmin (arrrrgh - its hacking me off either way!) I've been trying to add an auto_increment field on a table for 10 minutes and keep getting the following error:

#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 'ADD INDEX ('my_field')' at line 1
I know my sql is correct. So what the uck is it???
What a frigging joke. The problem seems to be that I am using ' instead of ` (can you tell the difference?)
If you use a slanted apostrophe the command works... go figure!!!
ADD INDEX (`my_field`)

MySQL Error - #1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key

I was trying to add an extra field to an existing table using phpMyAdmin, as follows:

Table: my_table
Field: my_field
Type: INT(10)
Attributes: UNSIGNED
Null: NOT NULL
Extra: auto_increment

Pressing Save resulted in an error:

#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key

Hmmm, I don't have any other numeric fields in the table and none of the existing fields are set to auto_increment (*#%*##$%*!!!!!)

Did a bit of digging around on oogle and found nothing that helped me to resolve the problem (I'm sure the answer exists out there, but patience is a virtue I lack!)

I tried dropping the table and recreating using sql (rather then using phpMyAdmin - lucky I didn't have any data in it). Still the same error!

Tried various methods to no avail. In the end tried the following and it worked a charm:

ALTER TABLE `my_table` ADD `my_field` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
ADD INDEX (`my_field`);

Turns out that if you want to add an auto_increment field on a table the field MUST be indexed (primary, unique or otherwise) in the same command as it is created.

On closer inspection, the phpMyAdmin GUI I was using to create the field was trying to run the above sql in two parts, ie...

ALTER TABLE `my_table` ADD `my_field` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;

Followed by...

ALTER TABLE `my_table` ADD INDEX (`my_field`);

Because the index was not getting created at the same time as the ADD `my_field` command MySQL hurled up an error (a fault with phpMyAdmin me thinks!)

"How comes it all works fine if you add the auto_increment field when you manually create a table", I hear you ask?! Well, if you look at the list of indexes (after table is created) it would appear that either phpMyAdmin (or MySQL, I don't know which) automatically tries to create a primary index on the auto_increment field and if there is another field defined as the primary key then MySQL chucks up an error, in which instance if you define a non-primary or unique index on the auto_increment field all works well!

22 October 2008


Reset MySQL root user password

Try this, it took 3 attempts to work (don't know why - just happy it worked!!)

http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html

phpMyAdmin Error - #1045 - Access denied for user 'root'@'localhost' (using password: NO)

Started getting this error after setting the "root" user password (see previous post).

Turns out that once you set the password on the "root" user the php script for phpMyAdmin can't get into the MySQL database (as there is now a password set that it doesn't know about).

To resolve this problem, you have to update a file named "config.inc.php" (found in phpMyAdmin installation directory - if you can't find it search for it using Windows Explorer, its there).

Once you have found the file, open it using Notepad (or a php editor if you're posh!)

Find: $cfg['Servers'][$i]['password']='';

(Note: there may be a few extra spaces in the above in your particular file, OR, you may already have an old password instead)

Change this with the current "root" user password (if you don't know this then ????)

It should look something like:

$cfg['Servers'][$i]['password']='the-root-user-password';

Save and close the file and try opening phpMyAdmin again and all should be fine (hopefully).
If it isn't then you may have entered the wrong password, check by manually logging into MySQL to verify the password:

Find the directory you installed MySQL into (usually c:\mysql)
Go into DOS prompt (Start -> Run -> type in cmd and press enter)
Go the the mysql installation directory
Go to the bin directory

Then type in:

mysql -u root -p

MySQL will respond with:

Enter password:


Type in the password (you used above) and press enter. If you get an error then you are using the wrong password. If you don't get an error then check other settings (ie. firewall, hosts file, MySQL service etc)


phpMyAdmin - Your configuration file contains settings (root with no password) that correspond to the default MySQL privileged account

Just installed Wampserver 2 (which had Apache v2.2.8, php v5.2.6 & MySQL v5.0.51b) on my Windows XP Home machine.

The install ran smoothly and everything looks like it installed fine.

When I started up phpMyAdmin there was a red box at the bottom with the message:

Your configuration file contains settings (root with no password) that correspond to the default MySQL privileged account. Your MySQL server is running with this default, is open to intrusion, and you really should fix this security hole.


I had very little idea what that meant but the "security hole" part got me a bit panicky. So I searched the web but couldn't find anything that was simple to understand (I don't know why every Tom, Dick and Harry feels the need to show they are technically astute! Anyway, having visited 5 different sites I finally worked out what the problem was, quite simply put:

The MySQL database has a user called "root", which is the overall admin user (I'm assuming), pretty much capable of doing anything you want to the MySQL installation and databases within it (that is unless you have read 10 MySQL books and worked out what security should be enabled/disabled etc). Anyway, as you can imagine, this would be a pretty dangerous user to leave unprotected without a password. And this message is warning you of exactly that, that the super-dooper user that is "root", capable of destroying the MySQL universe and all creations within it, is unprotected at the moment as it does not have a password assigned to it and is potentially vulnerable to attack from those cyber buzzards who have nothing better to do (if you were connected to the net, that is!)

Simple huh!

So, all you have to do is assign a password to the "root" user and all your problems will be solved.

To do that start up the MySQL Console

(Note: Wampserver has an menu item to start it)

Find the directory you installed MySQL into (usually c:\mysql)
Go into DOS prompt (Start -> Run -> type in cmd and press enter)
Go the the mysql installation directory
Go to the bin directory
Then type in:

mysql -u root -p

MySQL will respond with:

Enter password:

Press enter (as there is no default password set - hence the red message)

Type in:

use mysql; (then press Enter)

update user set password=PASSWORD('your-new-password') where user='root';

(then press Enter - remember to change the 'your-new-password' bit above, other your new password will be 'your-new-password'... duh!!)

flush privileges; (then press Enter)
quit (then press Enter)
exit (then press Enter)
That's it, you're done!


IMPORTANT - REMEMBER THE PASSWORD YOU HAVE SET. AS WITH ALL THINGS COMPUTER RELATED, ITS NOT EASY TO RESET PASSWORDS!!