Μysql – Ηow to get first value from array after fetch in php

Hello i want to get the first value (uid) from the object that i fetch.

Here is the syntax

 $sql2 = "SELECT **uid,**notification_created,username,name,profile_pic,tour,email, api_signature,first_name,last_name, artisticBehaviour,location,bio,last_login,first_login,updates_count,friend_count,profile_views,group_count,password
        FROM users WHERE username=:username ";
        $stmt2 = $db->prepare($sql2);
        $stmt2->bindParam("username", $username, PDO::PARAM_STR);
        $stmt2->execute();
        $userData = $stmt2->fetch(PDO::FETCH_OBJ);
        $uid = ????????

I want to retrieve the first value (uid) so i may use it as a variable

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

First bind the result and then fetch them

you should test if the username is empty before using it

$sql2 = "SELECT uid,notification_created,username,name,profile_pic,tour,email, api_signature,first_name,last_name, artisticBehaviour,location,bio,last_login,first_login,updates_count,friend_count,profile_views,group_count,password
        FROM users WHERE username=:username ";
        $stmt2 = $db->prepare($sql2);
        $stmt2->bindParam("username", $username, PDO::PARAM_STR);
        $stmt2->execute();

$result = $stmt2->get_result();
if ($result->num_rows>0) {
    while ($row = $result->fetch_assoc()) {
        echo $row['uid'];
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x