MySQL Database
Q1. Do you support MySQL?
Q2. Can I access MySQL from within Perl or PHP?
Q3. How do I use MySQL?
Q4. How do I access the MySQL command line interface from my shell account?
Q5. Table creation and data insertion example
Q6. Perl-CGI access example
Q7. PHP access example
Q1. Do you support MySQL? Yes. Hosting packages Professional 150 and above have access to MySQL. You may create a database and set its password by logging into your control panel and going to the MySQL database management section. [BACK]
Q2. Can I access MySQL from within Perl or PHP? Yes. The necessary modules required to access MySQL from these languages are installed on our servers. [BACK]
Q3. How do I use MySQL? Like HTML, we provide the tools necessary to use MySQL, but you are responsible for learning how to use it. Please see the MySQL website for official documentation. You may also find great tutorials on MySQL, PHP, Perl, and more at DevShed. [BACK]
Q4. How do I access the MySQL command line interface from my shell account? Use the command: mysql your_database_name -u your_username -p where in the command above, your_database_name is your assigned database name and your_username is your assigned username.
If you choose to specify a host name as one of the arguments, you should use localhost and not your site's domain name. [BACK]
Q5. Table creation and data insertion example Here is a simple example of how to create a table and insert data after connecting to MySQL via the command line interface.
# create the table
CREATE TABLE sample_table (
id int(10) unsigned NOT NULL auto_increment,
firstname varchar(30),
lastname varchar(30),
PRIMARY KEY (id)
);
# insert some data
INSERT INTO sample_table VALUES (1,'Joe','Wiggles');
INSERT INTO sample_table VALUES (2,'Mary','Wiggles');
INSERT INTO sample_table VALUES (3,'John','Doe');
INSERT INTO sample_table VALUES (4,'Ed','Bigster');
INSERT INTO sample_table VALUES (5,'Jane','Candi');
|
[BACK]
Q6. Perl-CGI access example Here is a Perl-CGI example of how to access and display the contents of the table created in MySQL FAQ question 5:
#!/usr/bin/perl
use DBI;
print "Content-Type: text/html\n\n";
$db = "your_mysql_database";
$dbuser = "your_mysql_username";
$dbpassword = "your_mysql_password";
$dbh = DBI->connect("DBI:mysql:$db", $dbuser, $dbpassword)
|| die "Couldn't connect to database: ". DBI->errstr;
$sth = $dbh->prepare("SELECT id,firstname,lastname FROM sample_table");
$sth->execute() || die "Couldn't execute statement: " . $sth->errstr;
print "<p>Contents of database:<br>\n";
print "<table border=1>\n";
print "<tr><th>ID</th><th>First Name</th><th>Last Name</th></tr>\n";
while (($id, $firstname, $lastname) = $sth->fetchrow_array())
{
print "<tr><td>$id</td><td>$firstname</td><td>$lastname</td></tr>\n";
}
print "</table>\n";
$sth->finish;
|
Remember that all CGI files must end in .cgi and be tagged as executable
before they will
run. The easiest way to do this is to go to http://www.yourdomain.com/FP-off (where yourdomain.com is your site's real domain name). Experienced users may also use the chmod command directly. [BACK]
Q7. PHP access example Here is a PHP example of how to access and display the contents of the table created in MySQL FAQ question 5>:
<?
$host = "localhost";
$dbuser = "your_mysql_username";
$dbpassword = "your_mysql_password";
$db = "your_mysql_database";
mysql_connect($host, $dbuser, $dbpassword);
mysql_select_db($db);
$sql = "SELECT id,firstname,lastname FROM sample_table";
$result = mysql_query($sql);
$result_rows = mysql_numrows($result);
?>
<p>Contents of database:<br>
<table border=1>
<tr><th>ID</th><th>First Name</th><th>Last Name</th></tr>
<?
for ($i=0; $i<$result_rows; $i++)
{
echo "<tr><td>" . mysql_result($result, $i, "id") . "</td><td>";
echo mysql_result($result, $i, "firstname") . "</td><td>";
echo mysql_result($result, $i, "lastname") . "</td></tr>\n";
}
?> </table> <?
mysql_close();
?>
|
[BACK]
|