Jump to content

How to select a peace of information from mysql?


Jragon

Recommended Posts

Ok so i have done that this is my function:

 

<?php
function getsalt($user){
    include("connect.php");
    $query = 'SELECT salt FROM user WHERE username = "$user"';
    $salt = mysql_query($query);
    return $salt;
}
echo getsalt('rory');
?>

 

But all I'm getting is:

Resource id #4

 

Thanks

 

Jragon

You still need to make data from that.

<?php
function getsalt($user){
    include("connect.php");
    $query = 'SELECT `salt` FROM user WHERE username = "$user"';
    $saltres = mysql_query($query);
    $saltdata = mysql_fetch_object($saltres);
    return $saltdata->salt;
}
echo getsalt('rory');
?>

Looks like it should work...

 

What output does this give:

<?php
function getsalt($user){
    include("connect.php");
    $query = 'SELECT `salt` FROM user WHERE username = "$user"';
    $saltres = mysql_query($query);
    $saltdata = mysql_fetch_object($saltres);
    print_r($saltdata);
    return $saltdata->salt;
}
echo getsalt('rory');
?>

This is my dumb for the table

 

-- phpMyAdmin SQL Dump

-- version 3.2.4

-- http://www.phpmyadmin.net

--

-- Host: localhost

-- Generation Time: Jul 22, 2010 at 10:19 AM

-- Server version: 5.1.41

-- PHP Version: 5.3.1

 

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

 

--

-- Database: `visualupload`

--

 

-- --------------------------------------------------------

 

--

-- Table structure for table `user`

--

 

CREATE TABLE IF NOT EXISTS `user` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `firstname` varchar(25) NOT NULL,

  `lastname` varchar(25) NOT NULL,

  `username` varchar(25) NOT NULL,

  `password` varchar(32) NOT NULL,

  `dob` date NOT NULL,

  `gender` varchar(1) NOT NULL,

  `salt` varchar(12) NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

 

--

-- Dumping data for table `user`

--

 

INSERT INTO `user` (`id`, `firstname`, `lastname`, `username`, `password`, `dob`, `gender`, `salt`) VALUES

(1, 'Rory', 'Anderson', 'rory', 'hello', '2010-07-07', 'm', 'akldflasjkdf');

 

 

 

 

 

 

 

 

 

Nothing still

 

$query = "SELECT `salt` FROM `user` WHERE `username` = '$user'";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
$data = mysql_fetch_object($result);
print_r($data);
$salt = $data->salt';
print $salt;

 

Well, try this

its half working now

what i get

 

stdClass Object ( [salt] => akldflasjkdf ) akldflasjkdf

This is not half working, this is already working.

You first print_r the object and then print salt. They were both printed, if you leave out the print statements and insert return $salt it should work, I see point in using a loop here, as you only want one result. (if you are going to use a loop, make sure you include LIMIT 1 in your query)

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.