Marco Marco - 5 months ago 23
MySQL Question

What's wrong here in my query?

My mysql db is empty, no table created and data written on it, help please, thanks! Here it is, sorry im a newbie:

$servername = "server";
$username = "username";
$password = "pass";
$dbname = "db name";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// sql to create table
$sql = "CREATE TABLE IF NOT EXISTS data (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
link text NOT NULL,
title text NOT NULL,
description text NOT NULL,
internal_link text NOT NULL,
eg_date TIMESTAMP
)ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1";
$sql = "INSERT INTO data (title)
VALUES
('$title')";

mysqli_close($conn);

Answer Source

use this :

$servername = "server";
$username = "username";
$password = "pass";
$dbname = "db name";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}else{
    // sql to create table
    $sql = "CREATE TABLE IF NOT EXISTS data (
id INT(6) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
link text NOT NULL,
title text NOT NULL,
description text NOT NULL,
internal_link text NOT NULL,
eg_date TIMESTAMP) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1";
    if(mysqli_query($conn,$sql)){
        $sql = "INSERT INTO data (title) VALUES ('".$title."')";
        mysqli_query($conn,$sql);
    }else{
        echo "Error creating table: " . mysqli_error($conn)."<br/>";
    }

}
mysqli_close($conn);

But attention that you should use these codes in a temporary file. Use just for one time!