简体   繁体   中英

Connecting MYSQL using PHP

I can't run my codes.

It says:

syntax error, unexpected '$query' (T_VARIABLE).

Code

<?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>")

should have a semicolon.

Replace it with this:

  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM