Jump to content

Absolutely stuck, desperate!


monkeymaker

Recommended Posts

Hi all, I know this is my first post so please go easy I'm still learning PHP.

 

Basically, I keep getting 'SQL Selection error: Unknown database 'localhost'

 

I have tried a basic 'insert' and 'search' and both attempts return this error.

 

search.php below

 

<?php include 'studentfunc.php';   //import the class in this web page  ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Retrieving Pet Data</title>
</head>

<body>
<?php
$db1 = new dbstudent();
$db1->openDB();
$sql="SELECT * from student";
$result=$db1->getResult($sql);
echo "<table border='1'>";
echo "<tr><th>Pet ID</th><th>Name</th><th>Info</th><th>Info</th></tr>";

  while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>{$row['cid']}</td><td>{$row['name']}</td>";
echo "<td>{$row['address']}</td>";
echo"<td><a href='dispownerinfo.php?owner_id={$row['sid']}'>{$row['name']}</a></td>";
echo "</tr>";
}
echo "</table>";

$db1->closeDB();
?>
  </body>
</html>

 

 

 

<?php //Save it as db_config.php
define('DB_HOST','localhost');
define('DB_NAME','test_work');
define('DB_USER','root');
define('DB_PASSWORD','');
?>

 

studentfunc.php below

 

<?php

require("db_config.php");

class dbstudent {
    /* DB connection handle */

    private $conn;

    function insert_student($sid, $name, $address, $postcode, $photo) {
        $esc_name = mysql_real_escape_string($name, $this->conn);
        $esc_address = mysql_real_escape_string($address, $this->conn);
        $esc_postcode = mysql_real_escape_string($postcode, $this->conn);
        $esc_photo = mysql_real_escape_string($photo, $this->conn);
        $sql = "insert into student (sid , name, address, postcode, photo)
values ('{$sid}', '{$esc_name}', '{$esc_address}', '{$esc_postcode}', '{$esc_photo}'";

        $result = mysql_query($sql, $this->conn);

        if (!$result) {
            die("SQL Insertion error: " . mysql_error());
        } else {
            $numofrows = mysql_affected_rows($this->conn);
            return $numofrows;
        }
    }

    function openDB() {
        $this->conn = mysql_connect(localhost, root);
        if (!$this->conn) {
            die("SQL Connection error: " . mysql_error());
        }
        $db_selected = mysql_select_db(DB_HOST, $this->conn);
        if (!$db_selected) {
            die("SQL Selection error: " . mysql_error());
        }
    }
       
        function getResult($sql) {
            $result = mysql_query($sql, $this->conn);
            if ($result) {
                return $result;
            } else {
                die("SQL Retrieve Error: " . mysql_error());
            }
        }

        function update_student($name, $address, $postcode, $photo, $sid) {
            $esc_name = mysql_real_escape_string($name, $this->conn);
            $esc_sid = mysql_real_escape_string($sid, $this->conn);
            $esc_address = mysql_real_escape_string($address, $this->conn);
            $esc_postcode = mysql_real_escape_string($postcode, $this->conn);
            $sql = "update student set name='{$esc_name}' where id={$esc_sid}";
            $result = mysql_query($sql, $this->conn);

            if (!$result) {
                die("SQL Error: " . mysql_error());
            }
            else {
                $numofrows = mysql_affected_rows($this->conn);
                return $numofrows;
            }
        } function closeDB() {
				mysql_close($this->conn);
			}
}


?>

 

I just dont get it! I am unimaginable grateful for any assistance here.

Link to comment
https://forums.phpfreaks.com/topic/223289-absolutely-stuck-desperate/
Share on other sites

For what I see openDB should be:

function openDB() {
        $this->conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        if (!$this->conn) {
            die("SQL Connection error: " . mysql_error());
        }
        $db_selected = mysql_select_db(DB_NAME, $this->conn);
        if (!$db_selected) {
            die("SQL Selection error: " . mysql_error());
        }
    }

Got it...thank you!! thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!thank you!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.