Using PHP with MySQL examples

I will continue to add to the php and mysql examples. These are just some simple examples. Remember just put them in between php tags and place the code in a php file. All of them you will have to add your username, password, database and table name(except for view tables).

Truncate a table

This will erase all data in your mysql table but keeps the structure.

$host="localhost"; // Host name
$username=" "; // Mysql username
$password=" "; // Mysql password
$db_name=" "; // Database name
$tbl_name=" "; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="TRUNCATE TABLE '$tbl_name' "; //might need to change the quotes
$result=mysql_query($sql) or trigger_error(mysql_error().$sql);

Create a Table Example

This assumes you already created a database.

// Make a MySQL Connection
$con = mysql_connect("localhost", "user name", "password") or die(mysql_error());
mysql_select_db("database name") or die(mysql_error());

$sql = "CREATE TABLE newTableName
username varchar(20),
passwd char(40),
email  varchar(100),
facebookID varchar(20),
tweetID varchar(20),
about varchar(300)


View mysql Tables

Scroll to the bottom see all the tables in your database. This is code produced from someone else. Props to that person.

Due to formatting issues I put it in a text file.


View Data in a mysql table


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Graveyard Database Logo

Has everything you want to know about grave yards. Plus has many celebrites and world leaders graves. Pssst it is my other site

Google + Profile
Check out Android Photo Mapping, its a app/site I been working on
Fabulou wholesale Petr Mrazek jersey is back. Best choice to get the wholesale Petr Mrazek jersey is suitable for all style of people.