Problem when posting into the DB

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

48 Replies - 965 Views - Last Post: 15 July 2013 - 05:40 AM Rate Topic: -----

#31 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Problem when posting into the DB

Posted 15 July 2013 - 01:22 AM

please post the full error messages.
Was This Post Helpful? 0
  • +
  • -

#32 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 102
  • Joined: 12-March 12

Re: Problem when posting into the DB

Posted 15 July 2013 - 01:26 AM

Attached Image
Was This Post Helpful? 0
  • +
  • -

#33 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Problem when posting into the DB

Posted 15 July 2013 - 01:27 AM

this may sound strange, but do this dump twice at the current position.
Was This Post Helpful? 0
  • +
  • -

#34 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 102
  • Joined: 12-March 12

Re: Problem when posting into the DB

Posted 15 July 2013 - 01:32 AM

Same error with a new line:
object(PDO)[1]
null
Was This Post Helpful? 0
  • +
  • -

#35 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Problem when posting into the DB

Posted 15 July 2013 - 01:36 AM

same error, but with the explanation for it.
Was This Post Helpful? 0
  • +
  • -

#36 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 102
  • Joined: 12-March 12

Re: Problem when posting into the DB

Posted 15 July 2013 - 01:42 AM

How can I fix it then..?
Was This Post Helpful? 0
  • +
  • -

#37 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Problem when posting into the DB

Posted 15 July 2013 - 01:46 AM

by knowing what the problem/reason is and then finding out, what part/block/line of your code causes it.
Was This Post Helpful? 0
  • +
  • -

#38 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 102
  • Joined: 12-March 12

Re: Problem when posting into the DB

Posted 15 July 2013 - 01:55 AM

Isn't it the line we are already hunting? :P/>

This post has been edited by Yeronox: 15 July 2013 - 01:55 AM

Was This Post Helpful? 0
  • +
  • -

#39 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Problem when posting into the DB

Posted 15 July 2013 - 01:57 AM

what we considered until now is the line that caused the error message, not the line(s) that is the reason for it.
Was This Post Helpful? 0
  • +
  • -

#40 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 102
  • Joined: 12-March 12

Re: Problem when posting into the DB

Posted 15 July 2013 - 02:06 AM

Ok, so: the problem is that it returns a null value?
I`m sure the connection to the DB is fine and that is the line that let me get the error
return self::getDB()->prepare($query);

class DB is almost the same as kruithne wrote:
class DB
	{
	// Made this private again, really shouldn't call this outside the DB wrapper.
	private static function getDB()
	{
	if (self::$connection === null)
	{
		try
		{
			// Correctly placed the settings array in the PDO constructor.
			global $connection;
			self::$connection = new PDO(
				DATABASE_DSN,
				DATABASE_USER,
				DATABASE_PASSWORD,
				array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
			);
		}
		catch (PDOException $e)
		{
			die($e->getMessage()); // We failed, die with the message.
			// This should be correctly handled by an error handler in production code.
		}
		return self::$connection;
	}
	}

	public static function prepare($query)
	{
		//var_dump(self::getDB()); var_dump(self::getDB());
		return self::getDB()->prepare($query);
	}

	private static $connection;
	}

Was This Post Helpful? 0
  • +
  • -

#41 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Problem when posting into the DB

Posted 15 July 2013 - 02:16 AM

Quote

Ok, so: the problem is that it returns a null value?

does NULL has any methods? since it is not an object–no.


Quote

I`m sure the connection to the DB is fine and that is the line that let me get the error

correct, that line is where the error comes into effect, but it’s not the underlying cause for the error.

so now you know that the error’s reason must be within getDB(). now you need to do a step-by-step check to follow what your code does in each line (and I means that literally and not what the code is supposed to do) and compare what the code actually does vs. what is is supposed to do.


Quote

class DB is almost the same as kruithne wrote:

yepp, almost.
Was This Post Helpful? 0
  • +
  • -

#42 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 102
  • Joined: 12-March 12

Re: Problem when posting into the DB

Posted 15 July 2013 - 04:57 AM

I`ve deleted the
if (self::$connection === null)
and I`ve used
return DB::getDB()->lastInsertId();
and now it`s working as I`ve set getDB() as public.
Otherwise I get errors.
And now when I echo it, it shows "0".
I need to fix that lastInsertId() error.
Was This Post Helpful? 0
  • +
  • -

#43 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Problem when posting into the DB

Posted 15 July 2013 - 05:09 AM

View PostYeronox, on 15 July 2013 - 01:57 PM, said:

I`ve deleted the
if (self::$connection === null)

but then you can leave that class be, as it serves no more purpose (you can equally well pass the PDO instance around)

View PostYeronox, on 15 July 2013 - 01:57 PM, said:

I need to fix that lastInsertId() error.

lastInsertId() only returns something from the last insert. upon creating a new connection (since you do not have a Singleton any more), that information (from the last connection) is lost.
Was This Post Helpful? 0
  • +
  • -

#44 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 102
  • Joined: 12-March 12

Re: Problem when posting into the DB

Posted 15 July 2013 - 05:14 AM

:( I don`t know how to fix it then.
Was This Post Helpful? 0
  • +
  • -

#45 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Problem when posting into the DB

Posted 15 July 2013 - 05:15 AM

just use the PDO variable and not the class.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4