这是我用来尝试连接到数据库并从中检索数据的代码,但它没有正确显示图像。所有其他内容都会正确显示。
//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>';
}
?>
答案 0 :(得分:3)
你有两个拼写错误:
$query="selelct * from data";
应该是:
$query="select * from data"; //select not selelct
和
mysql_selectdb($db);
应该是:
mysql_select_db($db);
答案 1 :(得分: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>';
}
}