<?php
$dbhost='localhost:3306';
$dbuser='root';
$dbpass='';
$con=mysql_connect($dbhost,$dbuser,$dbpass);
if(!$con) {
die("Couldnotconnect:".mysql_error());
}
mysql_select_db('shop');
$sql="SELECTCD_ID,Title,Artist,Year,Company,Price,Quantity,TypeFROMcd_data";
$result=$con->query($sql); //somethingiswronghere
$numRows=$result->num_rows; //somethingiswronghere
if($result->num_rows) { //somethingiswronghere
//outputdataofeachrow
//somethingiswronghere
while($row=$result->fetch_assoc($sql)) {
echo"id:".$row["CD_ID"]."Name:".$row["Title"]."".$row["Artist"].$row["Year"].$row["Company"].$row["Price"]."Euro".$row["Quantity"].$row["Type"];
}
}
else {
echo"0results";
}
mysql_close($con);
?>
答案 0 :(得分:1)
您的代码中存在很多问题:
SELECTCD_ID,Title,Artist,Year,Company,Price,Quantity,TypeFROMcd_data
我认为你的查询应该是这样的:
SELECT CD_ID,Title,Artist,Year,Company,Price,Quantity,Type FROM cd_data
以下是使用 MYSQLi面向对象的代码的完整示例:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT CD_ID,Title,Artist,Year,Company,Price,Quantity,Type FROM cd_data";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//your stuff
}
}
else
{
echo "0 results";
}
$conn->close();