簡體   English   中英

使用PHP連接MYSQL

[英]Connecting MYSQL using PHP

我無法運行代碼。

它說:

語法錯誤,意外的'$ query'(T_VARIABLE)。

<?php 
$hostname="localhost";
$username="";
$password="";
$dbname="thesis";
$usertable="product";
$yourfield="product_id";

msql_connect($hostname,$username,$password) or die ("<html><script>
language='Javascript'>alert('Unable to connect to     database!.'),history.go(-1)</script></html>")

$query = "SELECT * FROM $usertable";
$result = mysql_query($query);

if($result)
{
  while ($row = mysql_fetch_array($result))
  {
     $name = $row["$yourfield"];
     echo "Name: ".$name."</br>";
  }
}
?>
   msql_connect($hostname,$username,$password) or die ("<html><script>
    language='Javascript'>alert('Unable to connect to     database!.'),history.go(-1)</script></html>")

應該有一個分號。

替換為:

  msql_connect($hostname,$username,$password) or die ("<html><script>
    language='Javascript'>alert('Unable to connect to     database!.'),history.go(-1)</script></html>");
<?php
$hostname = "localhost";
$username = "";
$password = "";
$dbname = "thesis";
$usertable = "product";
$yourfield = "product_id";
$mysqli = new mysqli($hostname, $username, $password, $dbname);

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

/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT * FROM $usertable")) {
    while ($row = mysql_fetch_array($result))
  {
     $name = $row["$yourfield"];
     echo "Name: ".$name."</br>";
  }

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

$mysqli->close();

http://php.net/manual/en/mysqli.query.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM