Php 学习如何从MYSQL读取数据
我试图学习php和mysql,我试图从我的数据库中读取数据,在那里我在线跟踪了一些东西,遇到了一个错误,这是我的代码Php 学习如何从MYSQL读取数据,php,mysql,Php,Mysql,我试图学习php和mysql,我试图从我的数据库中读取数据,在那里我在线跟踪了一些东西,遇到了一个错误,这是我的代码 <?php include 'includes/conn.php'; $query =sprintf("Select * from customer"); $result = mysql_query($query); if (!$result) { $message ='from you see this then it's not
<?php
include 'includes/conn.php';
$query =sprintf("Select * from customer");
$result = mysql_query($query);
if (!$result)
{
$message ='from you see this then it's not connecting!'.mysql_error() ."\n";
$message .= 'everything' .$query;
die($message);
}
while ($customer = mysql_fetch_assoc($result))
{
echo $row['cust_id'];
echo $row['fname'];
echo $row['lname'];
echo $row['gender'];
echo $row['dob'];
}
mysql_free_result($result);
?>
替换
$message ='from you see this then it's not connecting!'.mysql_error() ."\n";
这里您的字符串是中的单引号,它是造成问题的原因
与
$message ="from you see this then it's not connecting!".mysql_error() ."\n";
…要显示结果,请更改以下内容:
while ($customer = mysql_fetch_assoc($result))
为此:
while ($row = mysql_fetch_assoc($result))
请查看下面的示例,了解如何使用php读取my mysql数据。
$sql=“从MyGuests中选择id、firstname、lastname”;
$result=mysqli\u查询($conn,$sql);
如果(mysqli_num_行($result)>0){
//每行的输出数据
while($row=mysqli\u fetch\u assoc($result)){
echo“id:.$row[“id”]。-Name:.$row[“firstname”]。$row[“lastname”]。
”;
}
}否则{
回显“0结果”;
}
第一个错误就在这里$message='从您看到的这个,然后它就不连接了!'。mysql\u错误()。“\n”代码>“并遇到错误”-那么您遇到了什么错误?你想让我们猜猜看吗?告诉我们!不要使用mysql函数,然后调试您自己的$con=mysql\u connect($dbhost,$dbuser,$dbpass)或die(mysql\u error());等您可以在php.netUse IDE上找到带有代码高亮显示的示例
while ($row = mysql_fetch_assoc($result))
Please review the below example for reading data my mysql using php.
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}