Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

problem with inserting data into database

Codes here !

Moderators: macek, egami, gesf

problem with inserting data into database

Postby lamiajoyee » Thu Jun 07, 2012 12:23 am

hello i want to insert data into a mySQL database by using the following code:
Code: Select all
<?php
   require_once("../includes/database.php");
   if(isset($database)){
      echo "true";   
   }
   else{
      echo "false";
   }
   $sql="INSERT INTO users (name,password,first_name,last_name )";
   $sql.="VALUES ('lami','hehe','lamia','mehreen')";
   $result=$database->query($sql);
?>

and
Code: Select all
<?php
   require_once("config.php");
   class MySQLDatabase
   {
      private $connection;   
      function _construct(){
         $this->open_connection();
      }
      public function open_connection() {
         $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
         if (!$this->connection) {
            die("Database connection failed: " . mysql_error());
         }
         else {
            $db_select = mysql_select_db(DB_NAME, $this->connection);
            if (!$db_select) {
               die("Database selection failed: " . mysql_error());
            }
         }
      }
      public function close_connection() {
         if(isset($this->connection))    {
            mysql_close($this->connection);
            unset($this->connection);
         }
      }
      public function query($sql){         
         $result = mysql_query($sql, $this->connection);
         $this->confirm_query($result);
         return $result;
      }
      public function mysql_prep( $value ) {
         $magic_quotes_active = get_magic_quotes_gpc();
         $new_enough_php = function_exists( "mysql_real_escape_string" );
         if( $new_enough_php ) {    
            if( $magic_quotes_active ) {
               $value = stripslashes( $value );
            }
            $value = mysql_real_escape_string( $value );
         }
         else {
            if( !$magic_quotes_active ) {
               $value = addslashes( $value );
            }
         }
         return $value;
      }
      private function confirm_query($result) {
         if (!$result) {
            die("Database query failed: " . mysql_error());
         }
      }
   }   
   $database=new MySQLDatabase();
   $db =& $database;
?>

but data isn't being inserted and warning shows up: "mysql_query() expects parameter 2 to be resource, null given". What is the problem and what can be done about it? please specify
lamiajoyee
New php-forum User
New php-forum User
 
Posts: 2
Joined: Thu Jun 07, 2012 12:08 am

Re: problem with inserting data into database

Postby egami » Thu Jun 07, 2012 4:41 am

Sorry, I'm not a big fan of OOP..

on a different php file.. try

<?php
$connect = mysql_connect("IPADDRESS","USERNAME","PASSWORD");
$db = mysql_select_db("DATABASENAME");

$table = "TABLENAMEHERE";

$query = "SELECT * FROM $table LIMIT 20";
$result = mysql_query($query) or die (mysql_error());

if (mysql_num_rows($result) > 0) {
echo "SQL READ QUERY WORKS! YAY! <br />";
}

$query = "INSERT INTO $table (fieldname1,fieldname2,fieldname3) VALUES ('$value1','$value2','$value3')";
$result = mysql_query($query) or die (mysql_error());
if (mysql_affected_rows() > 0) {
echo "SQL WRITE WORKS! YAY!<br/>";
}
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2197
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: problem with inserting data into database

Postby ejsexton82 » Thu Jun 07, 2012 10:14 pm

For some reason, $connection is not set. Possibly, a call to close_connection() was made earlier in the code, unsetting $connection.

Try opening the connection at the beginning of the query() function:

Code: Select all
public function query($sql){
   if(!isset($this->connection) || !is_resource($this->connection)) {
      $this->open_connection();
   }
   $result = mysql_query($sql, $this->connection);
   $this->confirm_query($result);
   return $result;
}
User avatar
ejsexton82
New php-forum User
New php-forum User
 
Posts: 86
Joined: Mon Jun 04, 2012 10:05 pm
Location: Ankara, Turkey

Re: problem with inserting data into database

Postby lamiajoyee » Fri Jun 08, 2012 10:21 am

ejsexton82 wrote:For some reason, $connection is not set. Possibly, a call to close_connection() was made earlier in the code, unsetting $connection.

Try opening the connection at the beginning of the query() function:

Code: Select all
public function query($sql){
   if(!isset($this->connection) || !is_resource($this->connection)) {
      $this->open_connection();
   }
   $result = mysql_query($sql, $this->connection);
   $this->confirm_query($result);
   return $result;
}


yes my problem is solved now.thank you very much.sorry for the late reply
lamiajoyee
New php-forum User
New php-forum User
 
Posts: 2
Joined: Thu Jun 07, 2012 12:08 am


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron