Jump to content

[SOLVED] How to easily output mysql in php?


Anzeo

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

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.