Warning in mysql_real_escape_string

Good day!

I am new in template in php like calling the .html webpage in php and I encountered warning in mysql_real_escape_string

here is my code:


<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
include('includes/config.sender.php');
include('includes/template.inc');


session_start();

  if (isset($_SESSION['logged_in'])) {
     header('Location:machine1.php');
     die();
  }


 if (isset($_POST['submit'])) {
	$username=$_POST['username']; 
	$password=$_POST['password'];


	$username = mysql_real_escape_string($username);
	$password = mysql_real_escape_string(sha1($password));
	
	//$username = $_DB->getEscaped($username);
	//$password = $_DB->getEscaped(sha1($password));


	//mysql_query("UPDATE machine_problem_rhoda_user SET password = '$password' WHERE username = '$username'");
	
	$sql_update = "UPDATE machine_problem_rhoda_user SET 
					password = '$password', 
			   WHERE username = '$username'";
	
	$sql_select = "SELECT
					username,
					password
			   FROM
					machine_problem_rhoda_user
			   WHERE
			   		username='$username'
					AND
					password='$password'
					";
					
	$result = $_DB->opendb($sql_select);

	$result=mysql_query($sql_select);
	
	$count=mysql_num_rows($result);

	if($count==1){  
		$_SESSION['logged_in'] = true;
		header("location:machine1.php");
	}
	else {
	echo "<center>";
	echo "Wrong Username or Password";
	echo "</center>";
	}
}

$tpl = new Template('.', 'keep');
$tpl->set_file(array('handle' => 'html/index.html'));
$tpl->parse('handle', array('handle'));
$tpl->p('handle');
?>

And I got this warning:

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2) in /opt/zeva/releases/ZEVA.sandbox/machine_problem/rhoda/index.php on line 20

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /opt/zeva/releases/ZEVA.sandbox/machine_problem/rhoda/index.php on line 20

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2) in /opt/zeva/releases/ZEVA.sandbox/machine_problem/rhoda/index.php on line 21

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /opt/zeva/releases/ZEVA.sandbox/machine_problem/rhoda/index.php on line 21

You need to connect to your database before you use mysql_real_escape_string.

So, connect first, then use mysql_real_escape_string. The errors should be gone.

Maybe this example from PHP: mysql_fetch_assoc - Manual can help you:

<?php

$conn = mysql_connect("localhost", "mysql_user", "mysql_password");

if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}
  
if (!mysql_select_db("mydbname")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}

$sql = "SELECT id as userid, fullname, userstatus 
        FROM   sometable
        WHERE  userstatus = 1";

$result = mysql_query($sql);

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//       then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

mysql_free_result($result);

?>