yami Posted March 25, 2022 Share Posted March 25, 2022 $resulti = mysqli_query($connection , "INSERT INTO cart(idItem,idUser) VALUES('$idd','$iduser')"); if(!$resulti){ die('Query failed!' . mysqli_error($connection)); } I am just trying to insert some data into a db table but this error keeps showing..any ideas how to fix it? Quote Link to comment https://forums.phpfreaks.com/topic/314626-object-of-class-mysqli_result-could-not-be-converted-to-string/ Share on other sites More sharing options...
ginerjm Posted March 25, 2022 Share Posted March 25, 2022 Are you sure about the line? I don't see any attempt to convert a var unless it is the two Values in your query statement. Try separating the query build from the query call and see what happens. $q =...... mysqli__query($connection, $q); Quote Link to comment https://forums.phpfreaks.com/topic/314626-object-of-class-mysqli_result-could-not-be-converted-to-string/#findComment-1594574 Share on other sites More sharing options...
yami Posted March 25, 2022 Author Share Posted March 25, 2022 I tried that too..I tried to separate them but it shows the same error! I have the same query in another php file but it does not show any error so I cannot tell what is wrong.. Quote Link to comment https://forums.phpfreaks.com/topic/314626-object-of-class-mysqli_result-could-not-be-converted-to-string/#findComment-1594575 Share on other sites More sharing options...
ginerjm Posted March 25, 2022 Share Posted March 25, 2022 Could we see the code you are now using as well as the exact error message? And be sure to point us to the line number Quote Link to comment https://forums.phpfreaks.com/topic/314626-object-of-class-mysqli_result-could-not-be-converted-to-string/#findComment-1594577 Share on other sites More sharing options...
yami Posted March 25, 2022 Author Share Posted March 25, 2022 So I have an if-else statement full of queries and it is the else part where there is an error and here it is: else{ $nameuser = $_SESSION['username']; $iduser = $connection->query("SELECT idCustomer FROM customer WHERE username='$nameuser'"); if(!$iduser){ die('Query failed!' . mysqli_error($connection)); } if (isset($_POST['idd']) && $_POST['idd']!=""){ $idd = $_POST['idd']; // echo $idd; // $query ="INSERT INTO cart(idItem,idUser) VALUES('$idd','$iduser')"; $resulti = mysqli_query($connection , "INSERT INTO cart(idItem,idUser) VALUES('$idd','$iduser')"); if(!$resulti){ die('Query failed!' . mysqli_error($connection)); } } } <input type='hidden' name='idd' id='idd' > the exact error message: Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\wamp64\www\index.php on line 134 it is the last query's line is it enough? Quote Link to comment https://forums.phpfreaks.com/topic/314626-object-of-class-mysqli_result-could-not-be-converted-to-string/#findComment-1594581 Share on other sites More sharing options...
Solution Barand Posted March 25, 2022 Solution Share Posted March 25, 2022 (edited) Your problem is with $iduser It is the result of the first query yet you are attemping to insert it as a value in the second query. You need to fetch the id from the resultset to use its value. Edited March 25, 2022 by Barand 1 Quote Link to comment https://forums.phpfreaks.com/topic/314626-object-of-class-mysqli_result-could-not-be-converted-to-string/#findComment-1594583 Share on other sites More sharing options...
yami Posted March 25, 2022 Author Share Posted March 25, 2022 ahh got it..it worked! Thank you @Barand Quote Link to comment https://forums.phpfreaks.com/topic/314626-object-of-class-mysqli_result-could-not-be-converted-to-string/#findComment-1594584 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.