-8

大家好,我有一些关于mysql查询的问题。我有一些这样的代码

SELECT site.id, site.name_site, site.id_site,site.id_mast,site.address,site.types,
       site.longtitude,site.latitude,site.altitude, site.id_region as site, 
       region.name_region, region.id_region as region 
       FROM site as site 
       INNER JOIN region as region 
       ON site.id_region = region.id_region ORDER BY name_site limit 3;  

还有这项工作:

SELECT * FROM site, region WHERE site.id_region= region.id_region LIMIT 2;

在浏览器上什么都没有发生,mysql 上的 byte 可以工作,但 PHP 上没有。为什么我在脚本 php 上有一些错误或查询 mysql 有问题?请帮我。感谢所有提示。

<?php

$dbhost = '............';
$dbuser = '........';
$dbpass = '.......';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = ("SELECT * FROM site, region WHERE site.id_region= region.id_region LIMIT 2;");
mysql_select_db('db_site', $conn) or die ('Invalid query: ' . mysql_error());
?>
<h4><center>Title</center></h4>
<table border='2' cellspacing='0' cellpadding='0'>
<tr>
<td>id</td>
<td>id_site</td>
<td>id_mast</td>
<td>name_site</td>
<td>address</td>
<td>types</td>
<td>longtitude</td>
<td>latitude</td>
<td>altitude</td>
<td>id_region</td>
</tr>
<?php

$retval = mysql_query( $sql, $conn ) or die ('Error did not connection'. mysql_error());
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}

while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
print "<tr>";
print "<td>{$row['id']}</td>";
print "<td>{$row['id_site']}</td>";
print "<td>{$row['id_mast']}</td>";
print "<td>{$row['name_site']}</td>";
print "<td style='width:100;'>{$row['address']}</td>";
print "<td style='width:100;'>{$row['types']}</td>";
print "<td>{$row['longtitude']}</td>";
print "<td>{$row['latitude']}</td>";
print "<td>{$row['altitude']}</td>";
print "<td>{$row['id_region']}</td>";
print "</tr>";
}
print "</table>";
mysql_close($conn);
?>

感谢所有提示。谢谢

4

1 回答 1

0

尝试删除 printf: 中的简单引号,$row['id_region']$row[id_region]我建议您确定:

echo '<td>'.$row['id_region'].'</td>';

另外,请考虑使用 mysqli 而不是 mysql,因为它已被弃用

<?php

$dbhost = '............';
$dbuser = '........';
$dbpass = '.......';
$dbname = 'db_site';
$my = new Mysqli($dbhost, $dbuser, $dbpass, $dbname);

?>
<h4><center>Title</center></h4>
<table border='2' cellspacing='0' cellpadding='0'>
<tr>
<td>id</td>
<td>id_site</td>
<td>id_mast</td>
<td>name_site</td>
<td>address</td>
<td>types</td>
<td>longtitude</td>
<td>latitude</td>
<td>altitude</td>
<td>id_region</td>
</tr>
<?php

// create a db connexion
$sql = 'SELECT * FROM site, region WHERE site.id_region= region.id_region LIMIT 2;';
// make the query
$stmt = $my->query($sql);
// fetch the results, display them as you want
while($row = $stmt->fetch_assoc())
{
  echo '<tr>';
  echo '<td>'.$row['id'].'</td>';
  echo '<td>'.$row['id_site'].'</td>';
  echo '<td>'.$row['id_mast'].'</td>';
  echo '<td>'.$row['name_site'].'</td>';
  echo '<td style="width:100;">'.$row['address'].'</td>';
  echo '<td style="width:100;">'.$row['types'].'</td>';
  echo '<td>'.$row['longtitude'].'</td>';
  echo '<td>'.$row['latitude'].'</td>';
  echo '<td>'.$row['altitude'].'</td>';
  echo '<td>'.$row['id_region'].'</td>';
  echo '</tr>';
}
echo '</table>';
于 2013-10-30T12:10:46.270 回答