-6

这是我一直用来尝试连接到数据库并从中检索数据的代码,但它没有正确显示图像。所有其他内容均正确显示。

//This php quote is test2.php
<?php

$dbhost='localhost';
$dbuser='root';
$dbpass='';
$db='dynamic';

$con = mysql_connect("localhost","elemental","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }



mysql_selectdb($db);

?>


<?php
include 'test2.php';

$query="selelct * from data";
$result=mysql_query($query);

while ($data=mysql_fetch_array($result)) {

echo '<h3>' . $data['id'] . '</h3>';
    echo '<h3>' . $data['name'] . '</h3>';

}


?>
4

2 回答 2

3

你有2个错别字:

$query="selelct * from data";  

应该:

$query="select * from data";  //select not selelct

mysql_selectdb($db);  

应该:

mysql_select_db($db);
于 2012-12-10T04:52:11.743 回答
0

您的代码中有一些疏忽或问题。包括无缘无故地重复自己,以及在你进步的过程中未能测试你的变量/行动。

<?php

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbbase = 'dynamic';

// WAS
// $con = mysql_connect("localhost","elemental","");
// why have your variables above if you hardcode them?
if( !( $con = mysql_connect( $dbhost , $dbuser , $dbpass ) ) ){
  error_log( 'MySQL Server Connection Failed - '.mysql_error() );
  // Never echo your errors publicly
  die( 'Cannot connect to database, but I am not going to show you why publicly.' );
}
if( !mysql_select_db( $dbbase ) ){
  error_log( 'MySQL Database Connection Failed - '.mysql_error() );
  // Never echo your errors publicly
  die( 'Cannot connect to database, but I am not going to show you why publicly.' );
}

include( 'test2.php' );

// WAS
// $query="selelct * from data";
// check your spelling!
$query = 'SELECT * FROM data';

if( !( $result = mysql_query( $query ) ) ){
  error_log( 'MySQL Query Failed - '.mysql_error() );
  die( 'Cannot query the database, but I am not going to show you why publicly.' );
}
if( !mysql_num_rows( $result ) ){
  echo 'No Records Found';
}else{
  while( $d = mysql_fetch_array( $result ) ){
    echo '<h3>'.$d['id'].'</h3><h3>'.$d['name'].'</h3>';
  }
}
于 2012-12-10T05:36:24.903 回答