Jump to content

[SOLVED] Echos out Row Name Not Value.


WeirdMystery

Recommended Posts

Hello I'm working on my own site with user pages, right now I'm working on descriptions for each user.

$te = "SELECT 'desc' FROM `front_user` "

."WHERE `id`='".$_GET["id"]."'

";

 

 

$le = mysql_query($te);

while ($row = mysql_fetch_assoc($le)) {

    echo $row["desc"];

 

That only prints out "desc", I want it to print out the value of "desc". Such a user id as 22 and his description is "hello" I want it to print hello. I don't know what I'm doing wrong...

Link to comment
Share on other sites

I am not COMPLETELY sure.. but I'd guess that because you're selecting 'desc' instead of desc or `desc` you're selecting a string rather than a field.. either that or that user's description is 'desc' because I read somewhere mysql supports single quotes but that again is not really solid information from me :) try removing the ' ' around desc in the select clause

Link to comment
Share on other sites

Or since you should in general avoid using reserved keywords as column names -

 

desc is a reserved keyword and should not be used as a column name. If you do use it as a column name it requires special handling everywhere you use it. In mysql, you can use the back-tick ` to force reserved keywords and other things that are not normally permitted as column names to be used. Since the back-tick is mysql specific, it should not be used if you intend your code to be usable on a different sql server.

Link to comment
Share on other sites

Changed the name to des. Still prints out nothing...

Here is the whole code.

 

<?php

include "dbconfig.php";

if ($_GET["id"])

{

 

$q = "SELECT * FROM `front_user` "

."WHERE `id`='".$_GET["id"]."' ";

$r = mysql_query($q);

 

if ( $obj = @mysql_fetch_object($r) )

  {

$te = "SELECT `des` FROM `front_user` "

."WHERE `id`='".$_GET["id"]."'

";

 

$le = mysql_query($te);

    echo $row[des];

    }

else

  {

        echo "Error: ID doesn't exist.";

  }

  }

 

?>

 

Link to comment
Share on other sites

Well, you removed the line of code that is fetching a row from the result set -

 

while ($row = mysql_fetch_assoc($le)) {

 

Are you learning php, developing php code, and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your php.ini to get php to help you? There would have been an error about $row being undefined that would have helped you possibly troubleshoot your code yourself.

 

Also, why are executing one query to retrieve all the columns (the *) in any matching rows, then if that query finds a row, execute a query again to retrieve only the des column, when the first query, when it succeeds has already retrieved the des column along with all the other columns?

 

Remove the @ from in front of mysql_fetch_object($r). That just hides errors that would help you find out why your code is not working. And to find if a SELECT query worked or not you use an if() test to see if it returned a FALSE value or a result resource. And to find how many rows a SELECT query returned, you use mysql_num_rows() (after you have determined that the query worked.)

Link to comment
Share on other sites

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.