Jump to content

Getting specific data from a mysql table


waddledoo

Recommended Posts

I am trying to get specific data from a MySQL table but cannot seem to find a proper command.

 

                $link = mysqli_connect('#####', '########', '########');
	$user = $_SESSION['username'];
	$email = mysqli_query($link,"select email from members where username='$user'");
	echo $email;

 

Specifically, I am trying to get the email address stored for the user specified by the session, and display that address.

However, I do not get any output with the current code, and am not sure what to try next. I have tried mysqli_fetch_row() but it only returns an error.

Link to comment
Share on other sites

$email = mysqli_query($link,"SELECT email FROM members WHERE username= '$user' LIMIT 1");
echo $email['email'];

 

This should work. I don't know if your link values are correct or how your table is designed. I also don't know if $user is the username properly escaped as it should be since you're using it in a query.

Link to comment
Share on other sites

                <?php
	session_start();
	$link = mysqli_connect('#####', '#####', '#####');
	$user = $_SESSION['username'];
	$email = mysqli_query($link,"SELECT email FROM members WHERE username= '$user' LIMIT 1");
	echo $email['email'];
	if ($email=='')
	{echo "<no address set>";}
	else
	{echo '$email';}
	?>

 

I figured it would be best to show the complete code. I implemented the changes you suggested, but my output remains the same (nothing).

Link to comment
Share on other sites

I don't see where $user is getting assigned anything? Can you show me where $user is assigned a value? (If it doesn't have a value that's why you're not getting any results!)

 

Use this to show an error message:

 

if (!$email = mysqli_query($link,"SELECT email FROM members WHERE username= '$user' LIMIT 1"))
{
    die('Invalid user specified.');
}

Link to comment
Share on other sites

The $user is set here

$user = $_SESSION['username'];

 

I added the if statement with the die command, and it executes. It seems the user is not being set correctly

 

 

EDIT: I forgot to set the database earlier in the code. This has solved the error of not returning a result, but now I get the following error:

 

Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\(etc etc...) on line 75

 

I tried using

$display = (string)$email;
echo $display;

 

But it gives the same error.

Link to comment
Share on other sites

I looked up the fetch functions, and after alot of trial and error I came up with:

 

<?php
	session_start();
	$link = mysqli_connect('#####', '#####', '#####');
	$user = $_SESSION['username'];
	mysqli_select_db($link,"#####");
	$result = mysqli_query($link,"SELECT email FROM members WHERE username= '$user' LIMIT 1");
	$email = mysqli_fetch_row($result);
	if ($email[0]=='')
	{echo "<no address set>";}
	else
	{echo $email[0];}
	?>

 

It works perfectly. Thanks :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.