Using MYSQL to call a row from a table - php

I am trying to show specific information for my logged in users, i can call the user_name, but i have been able also to call info from the "website" entry which holds the html for the page, i need to be able to call "website" to each logged in user, i.e Susan (user_name) has the html showing when she logs in, BUT Barry (user_name) has the same html showing as susan, as it is being called from Susans row! Please help it is driving me mad!
Here is the code that is on the myaccount.php:
Welcome To <?php echo $_SESSION['user_name'];?>'s Account Page</strong>
<?php
if (isset($_GET['msg'])) {
echo "<div class=\"error\">$_GET[msg]</div>";
}
?>
<?php
$data = mysql_query("SELECT * FROM users") or die(mysql_error());
$info = mysql_fetch_array( $data );
Print "<b></b> ".$info['$website'] . " ";
?>

You have to add a WHERE clause to your query. If you have a field in your table called user_name it will be like this :
"SELECT * FROM users WHERE user_name = '" . $_SESSION['user_name'] . "'";
Note : You are using a deprecated (mysql_query) you should use rather (mysqli_query) which has different syntax
http://php.net/manual/en/mysqli.query.php

Have you tried
$data = mysql_query("SELECT * FROM users WHERE username='$_SESSION[user_name]'") or die(mysql_error());

Related

How do I display a logged in user's images from the database?

Say if a user uploaded multiple images to their profile page, how would I display that specific user's images? I tried I did something like this:
$db = mysqli_connect("localhost", "root", "", "testdb");
$sql = "SELECT * FROM users ORDER BY id DESC";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result)) {
echo "<a href='profiles/uploads/".$row['image']."> ";
echo "<img id='img_div' title='".$row['image']."' alt='".$row['image']."' src='profiles/uploads/".$row['image']."'/>";
//echo "<p id='img_div'>".$row['desc']."</p>";
echo "</a>";
But I feel like this is terribly wrong because it is showing everyone's images and not the user's images. I tried looking up answers, but can't seem to find one.
You need a where clause. where id = 5, of course, replace the number with whatever user you are looking for.
Right now the query SELECT * FROM users ORDER BY id DESC is saying:
Select all columns from all users, ordering them by id.
Instead, you want something like:
SELECT * FROM users WHERE id = {user id} which states:
Select all columns from all users, where the id equals the user id.
As an aside, I'm not sure how you are setting up your database, but
if a user uploaded multiple images to their profile page, how would I display that specific user's images
makes me think that you should really be having separate tables, if you want to allow multiple pictures.
Try this:
You need to set user in sql query like id = 10.
And also missing ' last of this line: echo "<a href='profiles/uploads/".$row['image'].">";
<?php
$user_id = 10;
$db = mysqli_connect("localhost", "root", "", "testdb");
$sql = "SELECT * FROM users WHERE id = ". $user_id ." LIMIT 1";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result)) {
echo "<a href='profiles/uploads/". $row['image'] ."'>";
echo "<img id='img_div' title='".$row['image']."' alt='".$row['image']."' src='profiles/uploads/".$row['quotes']."'/>";
//echo "<p id='img_div'>".$row['desc']."</p>";
echo "</a>";
}
?>

PHP didn't show text from table MySQL for specific user

i tried to make some changes for my page.
So,i want to show for users a specific coins what are in the table.(Table are created)
But,when i try to doing this didn't work(didn't show) if i want for the username.
Work only without verification for user id/name.
My code:
$strSQL = "SELECT * FROM users WHERE id=" . $_GET["id"];
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo "<dt>Coins:</dt><dd>" . $row["coins"] . "</dd>";
}
I tried to $_GET["id"] with ["username"].But also didn't work (Verification for session is like this: if(isset($_SESSION['username']))
Somebody know why didn't work?
$strSQL = "SELECT * FROM users WHERE id='".$_GET['id']."'";

How to grab an int from my MySQL server via PHP?

I am a novice when it comes to PHP but I don't understand if my syntax is wrong in this statement, or how would I grab an int from my MySQL server.
I know that my server credentials are working fine. How would I fix this statement to give me a returned integer of the number of reviews in the userinfo table?
$numberofpreviousreviews = mysql_query("SELECT `number_of_reviews` FROM `userinfo`") or die(mysql_error()); //Check to see how many reviews user has previously created
$amountofreviews = $numberofpreviousreviews + 1;
$query2 = mysql_query("ALTER TABLE userinfo ADD `amountofreviews` VARCHAR(10000)") or die(mysql_error()); //Make another column in database for the new review
You need to fetch your results after you run your query. There are several ways to do this but using mysql_fetch_assoc() will work for you.
$numberofpreviousreviews = mysql_query("SELECT `number_of_reviews` FROM `userinfo`") or die(mysql_error()); //Check to see how many reviews user has previously created
$row = mysql_fetch_assoc($numberofpreviousreviews);
$amountofreviews = $row['number_of_reviews'] + 1;
FYI, you shouldn't be using mysql_* functions anymore. They are deprecated and going away. You should use mysqli or PDO.
Assume you have a table userinfo which has the following structure and data :
Scenario #1 :
If you want to retrieve the all number_of_reviews, then do like this,
$query = "SELECT `number_of_reviews` FROM `userinfo`";
$result = mysqli_query($db,$query);
while ($row = mysqli_fetch_assoc($result)) {
echo "Number of reviews : " . $row['number_of_reviews'] . "<br/>";
}
It will give you,
Number of reviews : 20
Number of reviews : 40
Since, the result has many rows, it will display like above.
Scenario #2:
If you want to retrieve only the specific number_of_reviews for some user id (which is unique). I take id as 1 as a example here. Then do like,
$query2 = "SELECT `number_of_reviews` FROM `userinfo` WHERE `id` = 1";
$result2 = mysqli_query($db,$query2);
while ($row2 = mysqli_fetch_assoc($result2)) {
echo $row2['number_of_reviews'] . "<br/>";
}
This will print,
20.
Because, number_of_reviews is 20 for id 1.

transfer signup_data from registrationtbl to userstbl

Good day does anyone can help me about my problem?
i have a signup_form all the data inputted will insert to my registrationtbl, my problem is the code below just delete the data from registrationtbl. how can i move the data to the userstbl and remove it from the registrationtbl? thank you . hope someone may help me..
the code is also came from one of the topics here..
<?php
$con=mysqli_connect("localhost","root","","scouts");
if ( isset($_GET['id']) ) {`enter code here`
$id = $_GET['id'];
$result = mysqli_query($con,"SELECT * FROM registration WHERE id='$id'");
while($row = mysqli_fetch_array($result))
{
echo $row['idno'] . " " . $row['surname'];
echo "<br>";
}
$row = mysqli_query($con,"INSERT INTO users (surname, firstname, middlename, gender, idno, password, signup_date) 'SELECT * from registration WHERE id =$id");
$sql = mysqli_query($con,"DELETE FROM registration WHERE id='$id'");
echo "its deleted";
}
?>
why are you using 2 tables:
simply use users table only, and add a status field to the table, when user registered the status may be 0, when registration approved change status to 1... so there is no need for 2 tables...
or if u have to use both tables, my suggession is the use of a database trigger to insert value to the second table from 1st.

Displaying the users information from the database

I am trying to created a way to call information from the database for a user to view. Such as they log in and it has their registered information viewed. I have this
session_start();
if($_SESSION['id'])
$result = mysql_query("SELECT * FROM User WHERE `id` = $_SESSION[id]")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo '<b>First Name:</b>' .$row['fname'];
echo '<br>';
echo '<b>Last Name:</b>' .$row['lname'];
}
but nothing shows up. My database name is megan, table is user, fields i want displayed are first name (fname) and last name (lname).
Can someone point me in the right direction. Thank you in advance!
Bad array indexing.
$result = mysql_query("SELECT * FROM User WHERE `id` = " . $_SESSION['id'])
You should turn on PHP error displaying.
First change this,
if(isset($_SESSION['id']))
because you have to check if the session isset correctly then do the query,
then the sql change to this,
$result = mysql_query("SELECT * FROM User WHERE `id` = ".$_SESSION['id'])

Categories