I have an assignment for school: I need to make a user database.
First I need to know how to get information from tables in my database.
Then I need to write usernames to the database.
Let’s start with the first one:
I am working with MAMP.
I got this sample from my teacher, I added some echos in there.
< ?php
$dbhost = 'localhost'; // Unlikely to require changing
$dbname = 'chrisdb'; // Modify these...
$dbuser = 'root'; // ...variables according
$dbpass = 'root'; // ...to your installation
$db_server=mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
echo("1");
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
echo("2");
mysql_select_db($dbname)
or die("Unable to select database: " . mysql_error());
echo("3");
?>
All the echos gets printed. Does that mean I have access to the server?
How do I access a table in the db and get the value of it?
And how do I write to the database?
I am not in search for the best possible code, more like the simplest possible code.
I appreciate all help you can give me. If you do help, will you please explain what your code means?
You write to the database by sending an
INSERTstatement and read using aSELECTstatement. See the documentationI personally hate W3Schools, but this article will get you started on how to insert data into a database.
You should note that
mysql_functions are being deprecated and you should usemysqli_orPDOinstead.