1

If I know the query only returns one row, and more specefically, in this case one value,

$full_name = database::query("SELECT concat (fname, ' ', lname) from cr WHERE email='$email'");

how do I get that value into a single variable. Database::query returns the value form mysql_query. The query above insert "Resource ID 4" in to mysql table.

Credentials(cr)

Tweets(tw)

3 Answers3

4

I don't know about database::query(), but mysql_query() does indeed return a resource.

Once you have that resource, you must fetch your data from it, using, for example, mysql_fetch_array() :

$data = mysql_fetch_array($full_name);


Note that there are several mysql_fetch_* function, depending on what kind of result your want (array, object, ...) :

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • An array, an object, and an associative array, what about a single value, i.e. the simplest case? I keep seeing the array example and I know I could use it but I keep thinking there is one just for a single value? –  Jul 25 '11 at 21:35
  • @Chris In any case, you must fetch the row *(and a row generally contains several values -- one being the exception)* ; then, you can access each data *(one, in your case)* that's in that row ;;; if you want to see what's in `$data`, you can use `var_dump($data);` to get a dump of that variable's content. – Pascal MARTIN Jul 25 '11 at 21:37
  • So all "grabs" from the table are row based? –  Jul 25 '11 at 21:44
  • Yep :-) *(actually, even if your select only returns one line of one column, that's still sent as... well, a row)* – Pascal MARTIN Jul 25 '11 at 21:48
2
$query = mysql_query("SELECT concat (fname, ' ', lname) from cr WHERE email='$email'");
$row = mysql_fetch_row($query);
echo $row[0]; 
Nicola Cossu
  • 54,599
  • 15
  • 92
  • 98
1

I'm not sure what else your database class has to offer, but using built in php mysql function you could use:

$full_name = database::query("SELECT concat (fname, ' ', lname)
     from cr WHERE email='$email'");
$full_name = mysql_fetch_row($full_name);
$full_name = $full_name[0];
Paul
  • 139,544
  • 27
  • 275
  • 264