使用PHP MYSQL将行插入表中

时间:2014-04-06 05:09:42

标签: php mysql

以下php代码用于删除表(如果存在),创建表,使用表,然后在表中插入一行。

除了插入之外,一切都有效。我是PHP和MYSQL的新手,我尝试过不同类型的引号(单个,双,这一个:`)的许多排列,但无法将数据插入到表中。

有人能说清楚这有什么问题吗?

$retval = mysqli_query($conn,'INSERT INTO `performance` (manager, program, programid, yearmonth, performance) VALUES ("manager1", "program1","programid1", "199901", "-3.4")');

下面的php脚本给出了输出:

  

成功连接

     

表已成功删除。

     

DB成功使用。

     

表已成功创建。

     

无法插入数据。

所以一切都与插入有关。

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully<br />';

$retval = mysql_query('DROP TABLE IF EXISTS `managedfutures`.`performance`') or die(mysql_error());
if(! $retval )
{
  die('Could not drop table  ' . mysql_error());
}
echo "Table dropped successfully.";
echo "<br>";

$retval = mysql_query("USE managedfutures", $conn);
if(! $retval )
{
  die('Could not use DB' . mysql_error());
}
echo "DB used successfully.";
echo "<br>";


$sql = "CREATE TABLE performance( ".
       "performance_id INT NOT NULL AUTO_INCREMENT, ".
       "manager VARCHAR(255) NOT NULL, ".
       "program VARCHAR(255) NOT NULL, ".
       "programid VARCHAR(255) NOT NULL, ".
       "yearmonth VARCHAR(6) NOT NULL, ".
       "performance FLOAT NOT NULL, ".
       "PRIMARY KEY (performance_id )); ";

$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not create table: ' . mysql_error());
}
echo "Table created successfully.";
echo "<br>";

$retval = mysqli_query($conn,'INSERT INTO `performance` (manager, program, programid, yearmonth, performance) VALUES ("manager1", "program1","programid1", "199901", "-3.4")');
if(! $retval )
{
  die('Could not insert data. ' . mysql_error());
}
echo "Data inserted successfully.";
echo "<br>";

return;

感谢Mike W指出我混合了mysql和mysqli命令!我是php / mysql的新手并没有意识到两者之间存在差异。还有另一个错误,我在insert语句中输入一个数字作为字符串。即我写了“-3.4”而不是-3.4。

为了完整性,这里有固定版本。

$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($mysqli->connect_errno) {
    die("Failed to connect to MySQL: " . $mysqli->connect_error);
}
echo 'Connected successfully<br />';

$retval = mysqli_query($mysqli,"DROP TABLE IF EXISTS `performance`");
if(! $retval )
{
  die('Could not drop table  ' . $mysqli->query_error);
}
echo "Table dropped successfully.";
echo "<br>";

$sql = "CREATE TABLE performance( ".
       "performance_id INT NOT NULL AUTO_INCREMENT, ".
       "manager VARCHAR(255) NOT NULL, ".
       "program VARCHAR(255) NOT NULL, ".
       "programid VARCHAR(255) NOT NULL, ".
       "yearmonth VARCHAR(6) NOT NULL, ".
       "performance FLOAT NOT NULL, ".
       "PRIMARY KEY (performance_id )); ";

$retval = mysqli_query($mysqli, $sql);
if(! $retval )
{
  die('Could not create table: ' . $mysqli->query_error);
}
echo "Table created successfully.";
echo "<br>";

$retval = mysqli_query($mysqli, "INSERT INTO `performance` (`manager`, `program`,`programid`, `yearmonth`, `performance`) VALUES ('manager1', 'program1','programid1', '199901', -3.4)");
if(! $retval )
{
  die('Could not insert data. ' . $mysqli->query_error);
}
echo "Data inserted successfully.";
echo "<br>";

return;

2 个答案:

答案 0 :(得分:3)

您正在混合mysql_*()mysqli_*()来电。两者不同,不能一起使用。不推荐使用mysql_*() - 仅使用mysqli_*()

答案 1 :(得分:1)

您在代码中使用了mysql_query

$retval = mysql_query( $sql, $conn ); //**You have used mysql_query**
if(! $retval )
{
  die('Could not create table: ' . mysql_error());
}
echo "Table created successfully.";
echo "<br>";

突然看到一个mysqli_query( MAGIC !!! )。

$retval = mysqli_query($conn,'INSERT INTO `performance` (manager, program, programid, yearmonth, performance) VALUES ("manager1", "program1","programid1", "199901", "-3.4")');
           ^^
      // SUDDENLY you see mysqli_query