Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

luk's avatar
Level 1

updateConnection is not working

in my project each user have connectionID and i want to update them in the following way i have updateConnection method

public function updateConnection($connectionID, $userID){
		$statement = $this->db->prepare(
			"UPDATE `user` SET `connectionID` = :connectionID WHERE `userID` = :userID"
		);
		$statement->bindParam('connectionID', $connectionID, PDO::PARAM_INT);
		$statement->bindParam('userID', $userID, PDO::PARAM_INT);
		$statement->execute();

	}

and when i "var_dump($userObj->updateConnection($user->connectionID,$user->userID));" IT RETURN null for me ! i dont know why it is not working ? any one who can help me please?

0 likes
23 replies
Sinnbeck's avatar

You are not returning anything so there is nothing to dump

Which part to you expect it to dump? The statement? The userId ?

1 like
luk's avatar
Level 1

@Sinnbeck but it dosent update the connectionID in the database? why? i used "$this->userObj->updateConnection($conn->resourceId,$data->userID);" to update the connectioid of the login user but it is not working

Sinnbeck's avatar

@habte hard to say. Does it work if you use hard-coded values? And are you checking the actual database or?

Sinnbeck's avatar

@habte you can also ensure that you are getting the expected values with var_dump($connectionID, $userID);`

luk's avatar
Level 1

@Sinnbeck yeah i checke them and return "string(1) "0" string(1) "2"" valu which is right ........but still it is not updating

Sinnbeck's avatar

@habte so even if you hard-coded the update statement it does not work? Or didn't you test that?

1 like
Sinnbeck's avatar

@habte something like (comment out the binding)

$statement = $this->db->prepare(
			"UPDATE `user` SET `connectionID` = 2 WHERE `userID` = 3"
		); 
1 like
Sinnbeck's avatar

@habte are you sure the method is called at all? Do you get output if you use var_dump inside the method?

1 like
luk's avatar
Level 1

@Sinnbeck i used

public function updateConnection($connectionID, $userID){
		$statement = $this->db->prepare(
			"UPDATE `users` SET `connectionID` = :connectionID WHERE `userID` = :userID"
		);
		$statement->bindParam('connectionID', $connectionID, PDO::PARAM_INT);
		$statement->bindParam('userID', $userID, PDO::PARAM_INT);
		$statement->execute();
		var_dump($connectionID, $userID);

	}

and in the home.php i call the method like this

var_dump($userObj->updateConnection($user->connectionID,$user->userID));

and the output is "string(1) "0" string(1) "2" NULL " the two is for the var_dump inside the method and null is for the home.php var_dump !

Sinnbeck's avatar

@habte remove that var_dump around the method. It makes zero sense there

1 like
Sinnbeck's avatar

Try this after execute

var_dump($statement->rowCount());
1 like
Sinnbeck's avatar

And are you 100% sure the table is named user and not users? I see you changed that in your latest example? So maybe it works after that

1 like
luk's avatar
Level 1

@Sinnbeck i removed it ! but i don't understand why is is not working?

luk's avatar
Level 1

@Sinnbeck it is users and i changed that ! but still i is not working

Sinnbeck's avatar

@habte and how many rows does it claim to have changed?

Also regarding the user vs users. That should trigger an error. Make sure pdo is running in exception mode

$pdo = new PDO('mysql:host=localhost;dbname=someDatabase', 'username', 'password', array(
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
luk's avatar
Level 1

@Sinnbeck i tray

var_dump($statement->rowCount());
```and the output is "int(0)"
Sinnbeck's avatar

@habte I have no clue currently then. Wrong database? Does the hard-coded query work if you copy it to your database manager?

luk's avatar
Level 1

@Sinnbeck the database is correct and it is working .........the error is in the method i don't know why it is not working

Please or to participate in this conversation.