Jump to content

Recommended Posts

hey guys im trying to clean my strings with this function

 

///////////////////////////////////Prevents SQL injection
    function clean($str) {
		$str = @trim($str);
		if(get_magic_quotes_gpc()) {
		$str = stripslashes($str);
		/*$str = htmlspecialchars($str);*/
	}
	return mysql_real_escape_string($str);		
    }

 

my script is connected to database and inserts data in field too but using the clean function im getting this error

 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established

Link to comment
https://forums.phpfreaks.com/topic/241581-function-giving-eror/
Share on other sites

here is my include page

 

<?php
session_start();
error_reporting(E_ALL & ~E_NOTICE);	
include  'config.php';
    include  'functions/db_functions.inc.php';
    include  'functions/gn_functions.inc.php';
?>

 

my config page

 

<?php
    // Database information
    $host   = 'localhost';
    $user   = 'root';
    $password   = 'demo';
    $database   ='abcd';

?>

 

my db_functions.inc.php

 

<?php
//////////////////////database conntction////////////////
    function db_connect() {
       global $host;
       global $user;
       global $password;
       global $database;
       
        if(!$conn = mysql_connect($host, $user, $password)) {
            die("Could't connect to database server.....".mysql_error());
        } else {
            mysql_select_db($database, $conn) or die(mysql_error());
            return $conn;
        }
    }
/////////////////////////////////////SQL Execution      
    function executeSql($sql){
        $conn = db_connect();          
        $result = mysql_query($sql, $conn)
                      or die(mysql_error($conn));
        return $result;       
    }

?>

 

A MySQL connection is required before using mysql_real_escape_string() otherwise an error of level E_WARNING is generated, and FALSE is returned.

 

from here: http://php.net/manual/en/function.mysql-real-escape-string.php

 

this is probably why you are getting an error

but if you look at my include page i put first config the login and tables info, then i put db connection function and after that i included general functions and that is where the mysql_real_escape_string is so dont you think i have already connected to db?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.