0

Can some one give me a solution for this error. This the error that im getting

Notice: Array to string conversion in C:\xampp\htdocs\TomisStore\functions\add_to_cart.php on line 47 Array

$newCart = new Cart();
$statement3 = $dbCon->prepare("SELECT MAX(cart_NO) FROM cart");
$statement3->execute();
 if ($statement3->rowCount() > 0) {
     $result2 = $statement3->fetch();
}     echo $result2;

2 Answers 2

2

you can use var_dump($result2) or print_r($result2) and see what is in the fetch.

also you need to change your query to

"SELECT MAX(cart_NO) as cart_no FROM cart"

then you print the result to know how to access cart_no

Sign up to request clarification or add additional context in comments.

2 Comments

array(1) { [0]=> array(2) { ["cart_no"]=> string(1) "3" [0]=> string(1) "3" } } this is the result i get when i use var_dump($result2). How can i access the value inside it. Please help me im bit new to PHP
then you coult simply acces $result[0]['cart_no'] by the way be careful because the variable $result2 could not exist if $statement3->rowCount() > 0 statement is false
2

$result2 is an Array, not a string, echo expects a string

be Aware that $result2 is not within the same block it is defined

try var_dump($result2)

i assume, that you could echo $result2["MAX(cart_NO)"]

you could query SELECT MAX(cart_NO) as max_cart_no FROM cart

then you could echo $result2['max_cart_no'];

2 Comments

array(1) { [0]=> array(2) { ["cart_no"]=> string(1) "3" [0]=> string(1) "3" } } this is the result i get when i use var_dump($result2). How can i access the value inside it. Please help me im bit new to PHP
see my answer: $result2['INDEX_VALUE'] where INDEX_VALUE is the Name of the selected tablefield. you can alter the Name of the table field as shown within the SQL via "as your_desired_Name". the var_dumps Shows the Indexes, for example $result2[0]["cart_no"] (2-dimensional Array)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.