Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

PHP Generic MySQL DB Connection - Quick & Dirty

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "a";
$tname = "b";

$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$c='';
$i='';
$v='';
foreach ($_POST as $key => $value) {
$c.="$key TEXT,";
$i.="$key,";
$v.="$value,";
}
$c=substr($c,0,-1);
$i=substr($i,0,-1);
$v=substr($v,0,-1);
$sql = "CREATE TABLE IF NOT EXISTS $tname (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
$c
)";

if (mysqli_query($conn, $sql)) {
echo "Table $tname created successfully
";
} else {
echo "Error creating table: " . mysqli_error($conn)."
";
}
$sql = "INSERT INTO $tname ($i)
VALUES ($v)";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully
";
} else {
echo "Error: " . $sql . "
" . mysqli_error($conn)."
";
}mysqli_close($conn);

?>


This post first appeared on SMARTMANOJ, please read the originial post: here

Share the post

PHP Generic MySQL DB Connection - Quick & Dirty

×

Subscribe to Smartmanoj

Get updates delivered right to your inbox!

Thank you for your subscription

×