Jump to content

Recommended Posts

Hya,

 

 

 

I was wandering:

 

whenever I want data from a query I use this code to eventually get the value in a php var

 

$qry = mysql_query("SELECT * FROM MEMBER WHERE ID='$ID'");
$profile = mysql_fetch_array($qry);
$Nick = $profile["Nick"];

 

The problem is, I use the same way of coding to select only one specific field of my table. So for example when I only need the Nickname, I use this code, but I also use it for generating my profile.

 

My question is: is their an easier way to select only one field and put it in a variable?

 

 

 

TIA

Link to comment
https://forums.phpfreaks.com/topic/46270-solved-how-to-easily-output-mysql-in-php/
Share on other sites

My question is: is their an easier way to select only one field and put it in a variable?

 

Not really. In fact there should be more to your code to prevent errors.

 

<?php

  $sql = "SELECT Nick FROM MEMBER WHERE ID='$ID'";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_array($result);
      $Nick = $row["Nick"];
    } else {
      echo "No results found";
    }
  } else {
    echo "Query failed<br />$sql<br />" . mysql_error();
  }

?>

 

Notice however that I only selected Nick in the query instead of the wildcard *.

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.