PHP错误:未选择数据库

时间:2013-10-11 23:08:11

标签: php mysql

我有一个简单的PHP脚本用于存储一些MYSQL数据库的值,但我得到NO DATABASE选择错误。我真的不明白错误在哪里。一些帮助将受到高度赞赏。

这是脚本:

<?php

echo "connection receiving";
$value0=$_GET['value0'];


$opendb=mysql_connect("localhost","root","") or die(mysql_error());
$db = mysql_select_db('arduino',$opendb);

if ($opendb){
  echo " database open.";
  if (!$db) {
    echo mysql_error();
    }
  $query = "INSERT INTO analoog0 VALUES(curdate(), curtime(), '$value0');";
  /* Run the query */
  $result= mysql_query($query)or die(mysql_error());
  mysql_close($opendb);
  echo "values written = $value0";
}


?>

当我运行 /write.php?value0=25 时,我得到未知数据库'arduino'未选择数据库

enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个:

$con = mysqli_connect("localhost", "root", "");

// check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$value0 = $_GET['value0'];
mysqli_query($con, "INSERT INTO arduino.analoog0 VALUES(curdate(), curtime(), '" . $value0 . "')");
echo "values written = " . $value0;
mysqli_close($con);