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.

$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.

                <?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).

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.');
}

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.

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.