GetDotted Domains

Viewing Thread:
"Argh. PHP help needed!"

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.

Sun 19/09/04 at 21:04
Regular
Posts: 10,364
Sigh.

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?
Sun 19/09/04 at 23:54
Regular
"NULL"
Posts: 1,384
To extract the data again, use the command mysql_fetch_assoc() or mysql_fetch_array(). The examples in the PHP manual are very good at explaining how to use these functions.
Sun 19/09/04 at 22:04
Regular
"It goes so quickly"
Posts: 4,083
gamesfreak wrote:
> 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.
Sun 19/09/04 at 21:49
Regular
Posts: 10,364
cjh = God.

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?
Sun 19/09/04 at 21:23
Regular
"It goes so quickly"
Posts: 4,083
[URL]http://uk.php.net/manual/en/function.mysql-query.php[/URL]

... 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.
Sun 19/09/04 at 21:04
Regular
Posts: 10,364
Sigh.

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?

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Easy and free service!
I think it's fab that you provide an easy-to-follow service, and even better that it's free...!
Cerrie
I've been with Freeola for 14 years...
I've been with Freeola for 14 years now, and in that time you have proven time and time again to be a top-ranking internet service provider and unbeatable hosting service. Thank you.
Anthony

View More Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, or Opera will provide a better, safer browsing experience for you.