0

I have a problem

I want to echo the value of "points" column.

What I tried, but did not work:

$stmt = $mysqli->prepare("SELECT points FROM member_profile WHERE user_id = '$firstName'"); $stmt->execute(); $array = []; foreach ($stmt->get_result() as $row) { $array[] = $row['points']; } print_r($array); 

THis is my current code:

<?php header('Content-Type: text/html; charset=Windows-1250'); session_start(); $firstName = $_SESSION['firstname']; $servername = "db.xxxx.gsp-europe.net"; $username = "xxxxxxxxxxxxx"; $password = "xxxxxxxxxxxxxx"; $dbname = "xxxxxxxx"; /// Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // check if the user exist $check = "SELECT * FROM `member_profile` WHERE user_id = '$firstName'"; $result = mysqli_query($conn,$check) or die(mysqli_error($conn)); $rows = mysqli_num_rows($result); //if exist increse points with 1 if($rows>=1){ $sql = "UPDATE `member_profile` SET points = points + 1 WHERE user_id = '$firstName'"; if ($conn->query($sql) === TRUE) { echo "Thingz created successfully"; } else { echo "Error doing sum thingz: " . $conn->error; } } // if don't exist create user with points 0 if ($rows == 0) { $query = "INSERT into `member_profile` (user_id, points) VALUES ( '$firstName' ,'0')"; $result = mysqli_query($conn,$query)or die(mysqli_error($conn)); $conn->close(); } ?> 

What I need in the nutshell: In the end of file, will be a "echo" that will show the current value of "points" column with identificator "user_id". Thats all

Thanks for your time, I appreciate it !

1
  • user_id equal to $firstName don't think that is really what u want Commented May 21, 2019 at 15:13

1 Answer 1

2

You are getting the result, but aren't fetching the datas.

$stmt->get_result() returns a result set -> mysqli_result and to handle this, you need to call the method fetch_array() from that result.

change your code to :

$results = $stmt->get_result(); while ($row = $result->fetch_array(MYSQLI_ASSOC)) { $array[] = $row['points']; } 

If you only want 1 result without using arrays, don't use arrays (yes, yes).

$results = $stmt->get_result(); while ($row = $result->fetch_array(MYSQLI_ASSOC)) { $points = $row['points']; } 
Sign up to request clarification or add additional context in comments.

3 Comments

Okey, its working .. But it showing:: Array ( [0] => 167 ) ... I need only "167".. How to fix it ? Thanks
Check edited answer, but this you could have guessed by yourself :)
Oh my god... Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.