mysql auto_increment

Web hosting discussion, programming, and shared and dedicated servers.
2 posts Page 1 of 1
by lunny » Tue Jan 08, 2013 12:18 pm
Hi,

I am trying to create a MySQL table with a primary key field. I have a create table statement that looks like this:

$sql = "CREATE TABLE Test
(
id int NOT NULL AUTO_INCREMENT,
individual varchar(100),
primary_contact varchar(100)
)";

but then I get this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /nfs/www/WWW_pages/lunny/drakesbayoyster.com/howtohelp/create_table_endorsements_test.php on line 42

Is the AUTO_INCREMENT function supported? It seems like a standard feature of MySQL tables. When I leave out the AUTO_INCREMENT part, the table is created just fine.

Thanks!
Jonathan
by toast0 » Tue Jan 08, 2013 10:43 pm
to be an auto_increment column it needs to be part of a key. what you usually want to do is something like this:

Code: Select all

if (!mysql_query($sql, $link)) {
  error_log ("mysql error: " . mysql_error($link));
  echo "There was an error";
  exit;
}
And then you'll get back a helpful message. If you can't access error logs easily, then you can add it to the echo; but then you may expose part of the query which can help an attacker break your site. (PS, I hope you're using prepared statements/bind parameters so you don't have to worry about escaping user data)
2 posts Page 1 of 1

Who is online

In total there are 20 users online :: 0 registered, 0 hidden and 20 guests (based on users active over the past 5 minutes)
Most users ever online was 999 on Mon May 10, 2021 1:02 am

Users browsing this forum: No registered users and 20 guests