PHP和mysql连接无法正常工作

时间:2017-09-25 06:06:54

标签: php html mysql

我已经编写了以下代码来连接php中的mysql,但我没有得到输出。

region

数据库名称为" yieldofvanillin"它有五列。我获得输出连接成功。之后没有输出。请让我知道代码中的错误。

3 个答案:

答案 0 :(得分:0)

我删除了错误。我在评论中提到。代码参考PHP Manual。你应该阅读本手册(强烈推荐)

<?php
$mysqli = new mysqli("localhost", "root", "pravin", "yieldofvanillin");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$query = "SELECT * FROM vanillin WHERE Microorganism = '$name' ";

if ($result = $mysqli->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        echo $row["Substrate"];
        echo $row["products"];
        echo $row["Microorganism"];
        echo $row["yield"];
        echo $row["Reference"];

    }

    /* free result set */
    $result->free();
}

答案 1 :(得分:0)

你正在混合使用mysqli和mysql库。

代码应为:

<?php
$servername = "localhost";
$username = "root";
$password = "pravin";
$mysql_conn = new mysqli($servername, $username, $password);
    if (mysqli_connect_errno()) {
         die("Connection failed: ". mysqli_connect_error());
 }

echo "Connected successfully";
$name = $_POST["microorganism"];
echo $name;
$db_selected = mysqli_select_db($mysql_conn,'yieldofvanillin');
if (!$db_selected){
    die ('Can\'t use  : ' . mysqli_error($mysql_conn));
}
$query = "SELECT * FROM vanillin WHERE Microorganism = '$name' ";
$result = mysqli_query( $mysql_conn,$query);
while ($line = mysqli_fetch_assoc($result)) {
    echo $line["Substrate"];
    echo $line["products"];
    echo $line["Microorganism"];
    echo $line["yield"];
    echo $line["Reference"];
}

mysqli_close($mysql_conn);
?>

答案 2 :(得分:0)

删除代码中的错误。仔细阅读php手册。

<?php
       $servername = "localhost";
       $username = "root";
       $password = "pravin";
      $db = "yieldofvanillin";

     // Create connection
        $mysqli = new mysqli($servername, $username, $password, $db);

    /* connection string*/
     if ($mysqli->connect_errno) {
       die("Connection failed: " . $mysqli->connect_error);
       exit();
    }

     $query = "SELECT * FROM vanillin WHERE Microorganism = '$name' ";

    if ($result = $mysqli->query($query)) {


     while ($row = $result->fetch_assoc()) {
          echo $row["Substrate"];

      }


     $result->free();
   }

 $mysqli->close();

&GT;

您的输出未显示,因为mysql_fetch_array不正确。因为您正在混合mysql_和mysqli_函数,并且您调用mysqli中不存在的myql_fetch_array。 MySQL和MySQLi是两种不同的PHP扩展,它们不能混合使用。因为前者在mysqli中被弃用