The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.
I've been puzzling over this for the last 30 minutes, the PHP manual doesn't seem to be of much help either. Bascially, all I want to do is populate an MYSQL database with data first, as at the moment it's empty.
What's the function to insert data into a database?
> Didn't realise it could be done using the query features.
You have to use the query feature to do anything within a database table for MySQL, as the only language it knows is, surpise surpise, SQL. PHP simply passes on the message.
@ hides the ugly error messages you get. If a connection or SQL query fails, by default PHP will show an error message. The @ prevents this from being displayed on your web page.
Thats why I wrapped them in a little if statement, such as:
# Connect to MySQL.
if(! $connection = @mysql_connect($DB_HOST,$DB_USERNAME,$DB_PASSWORD))
die("
Can't connect to Database.
");... which can be read as If NOT a valid connection ... hide the PHP error message, stop processing and write the message provided to the screen.
If the connection fails (say you enter the wrong password), then the standard PHP error message is hidden, and a more meaningful and nicer looking mesage, "Can't connect to Database", is shown. As long as you place a unique message for each segment, you can then find out which part is causing problems, without the regular users of your web site seeing the PHP error messages.
Nice one mate, and I feel like such a dumb ass now. I was looking for some statement that read "mysql_table_data" or something like that, didn't realise it could be done using the query features.
Excellent!
Edit:Just wondering, why do you put '@' symbols in front of any sql function?
... assuming you know how to connect to the database in PHP, you just need to run the correct query:
$result = @mysql_query("INSERT INTO tablename VALUES('firstcolumn','secondcolumn')", $connection);
... where $connection is the variable used to create the connection to MySQL in the first place.
This will add data to each column in the Database in the order they are listed. If you wish to leave a particular column out, simply have a blank value for it or use the NULL keyword.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
If you've not mastered connection yet, this will be what you need:
$DB_HOST = "your_mysql_host_address"; # eg 'localhost'
$DB_USERNAME = "your_mysql_username";
$DB_PASSWORD = "your_mysql_password";
$DB_TO_USE = "your_mysql_database";
# Connect to MySQL.
if(! $connection = @mysql_connect($DB_HOST,$DB_USERNAME,$DB_PASSWORD))
die("
Can't connect to Database.
");# Select the database.
if (! $database = @mysql_select_db($DB_TO_USE,$connection))
die("
Unable to select database.
");# Query database to insert some data.
if (! $result = @mysql_query("INSERT INTO tablename VALUES('firstcolumn','secondcolumn')", $connection))
die("
Unable to insert data.
");# Close database connection.
mysql_close($connection);
If you do a quick copy-and-paste, and something doesn't work, chances are I've made a slight spelling mistake, sorry.
I've been puzzling over this for the last 30 minutes, the PHP manual doesn't seem to be of much help either. Bascially, all I want to do is populate an MYSQL database with data first, as at the moment it's empty.
What's the function to insert data into a database?