-6

this is the code that I have been using to try and connect to a database and retrieve data from it but it's not showing images properly. All other content is displayed properly.

//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>';

}


?>
hakre
  • 193,403
  • 52
  • 435
  • 836
  • You're failing to check the result of `mysql_query`. It returns `false` when there is an error instead of returning the statement handle that `mysql_fetch_array` expects. This is also a duplicate of every single last one of those questions in the Related sidebar. **Heads up!** Future versions of PHP are *deprecating and removing* the `mysql_` family of functions. If you're still learning PHP, now would be a great time to [switch to PDO](http://php.net/book.pdo) or [mysqli](http://php.net/book.mysqli). – Charles Dec 10 '12 at 04:51
  • its `mysql_select_db` instead of `mysql_selectdb` – Pankit Kapadia Dec 10 '12 at 04:52
  • `selelct * from data` should be `select * from data` – Ashwini Agarwal Dec 10 '12 at 04:53
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – hakre Dec 10 '12 at 06:07

2 Answers2

3

You have 2 typos:

$query="selelct * from data";  

should be:

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

and

mysql_selectdb($db);  

should be:

mysql_select_db($db);
KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
  • 1
    FWIW, the odd instance of `mysql_selectdb` is a PHP4-ism. The extra underscore was added later, and an alias was added. Seeing it is a sure-fire sign that the user has been reading hilariously out-of-date tutorials, or never improved upon their original PHP knowledge. – Charles Dec 10 '12 at 05:03
0

A few oversights or issues in your code. Including repeating yourself for no known reason, and failing to test your variables/actions as you progress.

<?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>';
  }
}
Luke Stevenson
  • 10,357
  • 2
  • 26
  • 41