Main Content

Setting up a MySQL database from PHP

Archive - Originally posted on "The Horse's Mouth" - 2009-03-08 21:51:13 - Graham Ellis

Yes ... training on a Sunday ;-) ... I've been setting up a MySQL database from a PHP script and testing it. A couple of bits of source to share.

1. The setup of the database:

<?php
mysql_connect("localhost","womble","wimbledon");
mysql_query("create database gerald");
mysql_select_db("gerald");
mysql_query("create table course(first text, second text)");
mysql_query('insert into course values("Upper","Lower")');
mysql_query('insert into course values("Smiff","Yones")');
?>


2. A page to test that it's there:

<?php
mysql_connect("localhost","womble","wimbledon");
mysql_select_db("gerald");
$rs = mysql_query("select * from course");
while ($rown = mysql_fetch_assoc($rs)) {
  $html .= "$rown[first] ... $rown[second]<br>";
  }
?>
<html>
<body>
Here it is <br>
<?= $html ?>
</body>
</html>



(We had to set it up this way as there is only FTP access to the server ... learn more about these things on our PHP courses and MySQL courses