This question already has answers here:
Single result from database using mysqli
(6 answers)
Closed 4 years ago.
I need to get only the id of member who's username is X from the mysql database.
Can this only be done with a while loop or is there any other way around this?
What I'm thinking of is something like:
$id = mysqli_query($con,'SELECT id FROM membrs WHERE username = '$username' LIMIT 1)
Thanks,
You can use:
mysqli_fetch_array();
// For Instance
$id_get = mysqli_query($con, "SELECT id FROM membrs WHERE username='$username' LIMIT 1");
$id = mysqli_fetch_array($id_get);
echo $id['id']; // This will echo the ID of that user
// What I use is the following for my site:
$user_get = mysqli_query($con, "SELECT * FROM members WHERE username='$username'");
$user = mysqli_fetch_array($user);
echo $user['username']; // This will echo the Username
echo $user['fname']; // This will echo their first name (I used this for a dashboard)
without while loop we can do it by the following code, if you are selecting more than 1 record you need to loop it
$row = mysqli_fetch_array($id);
echo $row['id'];
Related
This question already has answers here:
PHP and MySQL: Order by most recent date and limit 10
(6 answers)
Closed 5 years ago.
In my website, All I pretty much do is show the fullnames of the users that submitted a form from the database and when a admin clicks a name, it just gives the clicked users email. My question is, how can I arrange the list of names to where it gives me the newest names on top and older on bottom? I have a timestamp cell in there that documents the timestamp when the user submits a form. Here is my code
<?php
require_once('db_login.php');
$stmt = $handler->prepare("SELECT * FROM formdata");
$stmt->execute();
while($result = $stmt->fetch()){
$userid = $result['user_id'];
echo "<a href='speaker_apps.php?infoid={$userid}'>". $result['fullname']."</a></br>";
}
if(isset($_GET['infoid'])){
$stmt = $handler->prepare("SELECT * FROM formdata where user_id='".$_GET['infoid']."'");
$stmt->execute();
$row = $stmt->fetch();
echo "<p>id =".$row['email']."</p></br>";
}
?>
ORDER your results:
$stmt = $handler->prepare("SELECT * FROM formdata ORDER BY your_time_field DESC");
This question already has answers here:
MySQL skipping first row
(2 answers)
Closed 5 years ago.
Even though it works correctly when I query it in phpMyAdmin SQL tab. What I am trying to accomplish is to display all images uploaded by the user ( which are currently 4 ) but I only get 3 ( the one missing is always the first one ). Here's my code:
<?php
$currentUser = $_SESSION['id'];
$sql = "SELECT username FROM user WHERE id='$currentUser'";
$result = mysqli_query($conn, $sql);
$getResult = mysqli_fetch_assoc($result);
$author = $getResult['username'];
$sql2 = "SELECT * FROM image WHERE author='$author' ORDER BY id DESC";
$result2 = mysqli_query($conn, $sql2);
$getResult2 = mysqli_fetch_assoc($result2);
while ($row = $result2->fetch_assoc()){
echo '<img class="profilePageImages" src="uploads/'.$row['path'].'" alt="Random image" />';
}
?>
This line, before the loop is consuming the first row. Just delete it.
$getResult2 = mysqli_fetch_assoc($result2);
This question already has answers here:
MySQL returns only one row
(5 answers)
Closed 6 years ago.
I want to list all username from database if the id exists. That code works fine in database select * from users where id IN (1,2,3,4,5).
But in php it doesn't work. My php code is
$sql = "select * from users where id IN (1,2,3,4,5)";
$result = mysql_query($sql);
if ( mysql_num_rows($result) > 0 ) {
$row = mysql_fetch_assoc( $result );
echo $row["username"];
}
This php code only list one user name. I want to list all usernames. Sorry for my bad English. Thanks.
<?php
$sql = "select * from users where id IN (1,2,3,4,5)";
$result = mysql_query($sql);
if ( mysql_num_rows($result) > 0 ) {
while($row = mysql_fetch_assoc( $result )) {
echo $row["username"]."\n";
}
}
if you only fetch once, you will only get one row - obviously. so if you want all rows, you have to fetch as long as there are rows to fetch.
This question already has answers here:
PHP check in and out system
(3 answers)
Closed 7 years ago.
I am trying to update a value into my table with the last ID, but I can not get mysql_insert_id() working. Can someone help me (I am a beginner with PHP). Thanks in advance!
This is a piece of my code, I know that mysql is outdated but I am doing this as a school project:
$tijd = date("H:i:s");
$userid = mysql_insert_id();
$query = "UPDATE tijden SET tijduit = '$tijd' WHERE id = '$userid'";
$resultaat = mysql_query($query);
unset($_SESSION['inchecken']);
$tijd = date("H:i:s");
$userid = mysql_insert_id();
You cannot get a mysql_insert_id() without doing an INSERT query. If you are trying to retrieve the last record do the following query:
SELECT id FROM tijden ORDER BY id DESC LIMIT 1
From there get the last ID and use this variable to update the record below.
$query = "UPDATE tijden SET tijduit = '$tijd' WHERE id = '$userid'";
$resultaat = mysql_query($query);
You do not necessarily need to separate the $query variable from the function. Simplify by doing:
$resultaat = mysql_query("UPDATE tijden SET tijduit='$tijd' WHERE id='$userid'");
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I update if exists, insert if not (aka upsert or merge) in MySQL?
I know this is pretty basic.. but for some reason this is not working for me. I have a form that stores a Facebook user's ID to check if they already submitted the form. I have the form that submits the User ID into the database working perfectly. Its just this part of checking if the User ID value exists in the database that is tripping me up.
Here's my code....
$user_id = 1234567890;
$checkUserID = mysql_query("SELECT fbUserID from submissions WHERE fbUserID = '$user_id'");
if ($checkUserID) {
echo "GTFO BRO";
}
Whenever I do an "echo" on the $checkUserID variable I get this returned.. "Resource id #9"
mysql_query returns a resource containing the result of the query, you need to use something like this:
$user_id = 1234567890;
$checkUserID = mysql_query("SELECT fbUserID from submissions WHERE fbUserID = '$user_id'");
if (!$checkUserID) {
die('Query failed to execute for some reason');
}
if (mysql_num_rows($checkUserId) > 0) {
echo "User id exists already.";
$user = mysql_fetch_array($checkUserId);
print_r($user); // the data returned from the query
}
I think you query string is wrong. If you're using double quotes, you'd have to change it to
.... WHERE fbUserId = '{$user_id}'"
or you have to concatenate it
..... WHERE fbUserId = '" . $user_id . "'"
try the following piece of code:
$checkUserID = mysql_query("SELECT fbUserID from submissions WHERE fbUserID = '$user_id'");
while($test = mysql_fetch_array($checkUserID))
if ($test ) {
echo "GTFO BRO";
}
i hope this will work properly for you..