4 Replies - 1797 Views - Last Post: 09 December 2012 - 08:52 PM Rate Topic: -----

#1 Navya10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-November 12

No Database Selected error on php page

Posted 09 December 2012 - 05:48 PM

Hi,

I am getting "No Database selected" eroor,Here's the code:
<?php
	$Username = $_POST['username'];
	$Password = $_POST['password'];
	
	
	// 3020ta, 3020
	if (($Username != '3020ta' AND $Password != '3020') && ($Username != 'asdf' AND $Password != '') && 
			($Username != '1000' AND $Password != '2000') &&  ($Username != '1100' AND $Password != '1200')&& 
			($Username != '1111' AND $Password != '2222'))
	{
		//echo $error_message
	
		echo '<a href = "Login.html"> Back To Login Page</a>';
	}
	else
	{
	require 'database.php';
	
	$sql = "SELECT * FROM akkiran";
	$result = mysql_query($sql) or die(mysql_error());
	echo "<table border='1'>";
	echo "<tr><th> Crt.no.</th> <th>Student id</th> <th>Password</th> </tr>";
	while($row = mysql_fetch_array($result))
	{
		echo "<tr><td>"; 
		echo $row['Crt.no.'];
		echo "</td><td>"; 
		echo $row['student_id'];
		echo "</td><td>"; 
		echo $row['password'];
		echo "</td></tr>"; 
	 
	}
}
	 echo "</table>";
	
?>



and here's the code for database.php:

<?php
	$dsn = 'mysql:host=127.0.0.1;dbname=ITEC3020';
	$username = 'hhhhhh';
    $password = '123456';
	
	try
	{
		$db= new PDO($dsn, $username, $password);
	}
	catch (PDOException $e)
	{
		$error_message = $e->getMessage();
		include('database_error.php');
		exit();
	}
?>



When I don't use require 'database.php' and
use
$connect = mysql_connect("127.0.0.1", "hhhhhh", "123456");
mysql_select_db("ITEC3020");


it works!!.. But I have to use require database.php..Where am I going wrong?..
Any help would be appreciated.
Thank You

Is This A Good Question/Topic? 0
  • +

Replies To: No Database Selected error on php page

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: No Database Selected error on php page

Posted 09 December 2012 - 07:09 PM

Hey.

If you are going to use PDO (which I'd highly recommend!), you have to use PDO to query the database, using either PDO::query() or (preferably) using PDO prepared statements via PDO::prepare(). The mysql_query function, and all the other mysql_* functions, will not work if you use PDO to connect to the database. It's a completely separate system.
Was This Post Helpful? 0
  • +
  • -

#3 Navya10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-November 12

Re: No Database Selected error on php page

Posted 09 December 2012 - 07:55 PM

I checked the links, but I don't understand, What changes should I implement here?
Thanks
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: No Database Selected error on php page

Posted 09 December 2012 - 08:33 PM

If you want to use PDO, then you can't use functions like mysql_query, mysql_fetch_assoc, or anything starting with mysql_. You'll have to replace any such calls with the PDO equivalents.

For example, with the old-school MySQL API extension (what your code is using now), you would do things like this:
<?php
mysql_connect("localhost", "user", "pwd") or die(mysql_error());
mysql_select_db("mydb");

$sql = "SELECT id, email FROM users
        WHERE name = " . mysql_real_escape_string($_GET["name"]);
$result = mysql_query($sql) or die(mysql_error());

if (mysql_num_rows($result) == 1) {
    $row = mysql_fetch_assoc($result);
    echo $row["id"] . ", " . $row["email"] . "<br>\n";
}



But in PDO you could do the same thing like this:
<?php
$dsn = "mysql:host=localhost;dbname=mydb";
$pdo = new PDO($dsn, "user", "pwd");

$sql = "SELECT id, email FROM users
        WHERE name = ?";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(1, $_GET["name"]);

if ($stmt->execute()) {
    $row = $stmt->fetch(PDO::FETCH_OBJ);
    if ($row) {
        echo $row->id . " => " . $row->email . "<br>\n";
    }
}



You see, it's a completely different approach. It's much cleaner, and relies on a common OOP API rather than cloning the old MySQL C API functions. If you feel that the OOP approach is beyond your current skill level, you might want to consider using MySQLi instead. It offers much the same functionality as the PDO extension, but it also offers procedural functions that mimic the old-school MySQL API. It's far easier for developers used to the mysql_ functions, and those who are perhaps not well versed in OOP yet, to move over to MySQLi than over to PDO. It's worth looking into.
Was This Post Helpful? 1
  • +
  • -

#5 Navya10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-November 12

Re: No Database Selected error on php page

Posted 09 December 2012 - 08:52 PM

Understood, Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1