PHP error code

[Closed]
Report
Posts
1
Registration date
Thursday August 23, 2012
Status
Member
Last seen
August 23, 2012
-
Posts
8
Registration date
Friday May 24, 2013
Status
Member
Last seen
May 27, 2013
-
<?php
include '../../MySQl/Includes/Connection.php';
$query="SELECT *FROM bridal_info WHERE ID=1";
$result=mysql_query($query) or die (mysql_error());
while($person=mysql_fetch_array($result))
{
echo "<h3>"."Colour :" ." ". $person['Colour']."</h3>";
echo "<h3>"."Fabric :"." " .$person['Fabric']."</h3>";
echo "<h3>"."Price :". " ".$person['Price']."</h3>";
}
?>
This is my code and Error in my webpage is :No database is selected.
Please solve this problem.

2 replies

Posts
4233
Registration date
Thursday August 19, 2010
Status
Moderator
Last seen
August 3, 2016

Hello,

You have problem with the include file named Connection.php. Verify that the path of the file is correct or directly connect to your mysql database before your sql statement.

You can use PDO to do it, here is the code;

  <?php 
  
  try {
          $db = new PDO('mysql:host=localhost;dbname=your_database_name', 'your_username',  'your_password');
   }
   
   catch(PDOExeption $e) {
   die('Error connecting to the database' .$e->getMessage());
   }
   
   $query="SELECT * FROM bridal_info WHERE ID=1"; 
   $result= $db->query($query) or die (print_r($db->errorInfo())); 
   
       while($person = $result->fetch()) 
       { 
         echo "<h3>"."Colour :" ." ". $person['Colour']."</h3>"; 
         echo "<h3>"."Fabric :"." " .$person['Fabric']."</h3>"; 
         echo "<h3>"."Price :". " ".$person['Price']."</h3>"; 
       } 
   
   ?>

Posts
8
Registration date
Friday May 24, 2013
Status
Member
Last seen
May 27, 2013

may be problem in ur Connection.php

mysql_select_db("test")

check this