Jump to content

Not working


Glenskie

Recommended Posts

Well, you aren't outputtting anything in that code. But, assuming you are echoing $name somewhere else, either your query is failing or there were no results. You need to add code to handle those situations. Also, if you are using a LIMIT 1 on your query, there is no reason to use a while() loop.

 

$query = "SELECT firstname, lastname
          FROM myMembers
          WHERE id='$logOptions_id'
          LIMIT 1";
$result = mysql_query($query);

if(!$result)
{
    echo "Query: {$query}<br>Error: " . mysql_error();
}
elseif(!mysql_num_results($results))
{
    echo "There were no results.";
}
else
{
    $row = mysql_fetch_assoc($result);
    $name = "{$row['firstname']} {$row['lastname']}";
    echo $name;
}

Link to comment
Share on other sites

Well, you aren't outputtting anything in that code. But, assuming you are echoing $name somewhere else, either your query is failing or there were no results. You need to add code to handle those situations. Also, if you are using a LIMIT 1 on your query, there is no reason to use a while() loop.

 

$query = "SELECT firstname, lastname
          FROM myMembers
          WHERE id='$logOptions_id'
          LIMIT 1";
$result = mysql_query($query);

if(!$result)
{
    echo "Query: {$query}<br>Error: " . mysql_error();
}
elseif(!mysql_num_results($results))
{
    echo "There were no results.";
}
else
{
    $row = mysql_fetch_assoc($result);
    $name = "{$row['firstname']} {$row['lastname']}";
    echo $name;
}

 

minor quibble, substitue mysql_num_results() with mysql_num_rows()

Link to comment
Share on other sites

The code I provided [with the one fix for mysql_num_rows()] would do one of three things:

 

1) If the query is failing, then you will see an output of the query and the error

 

2) If the query succeeded, but with no results, there would be output to that effect

 

3) If the query executed successfully and there was a record returned you would see the output from the two fields in the SELECT query.

 

So, if you get no output, the only thing I can assume is that the matching record has empty values (or only white-space characters) for those two fields or that code is not getting executed. I.e. you have some sort of control structure that is preventing those lines from running such as an if() condition.

Link to comment
Share on other sites

ok , the query succeded but no results , i will post the code im using it for ...

here is where i set up where i get there name

function name()
{

$lookup = "SELECT firstname, lastname
          FROM myMembers
          WHERE id='$id'
          LIMIT 1";
$result = mysql_query($lookup);

if(!$result)
{
    echo "Query: {$lookup}<br>Error: " . mysql_error();
}
elseif(!mysql_num_rows($results))
{
    echo "There were no results.";
}
else
{
    $row = mysql_fetch_assoc($result);
    $name = "{$row['firstname']} {$row['lastname']}";
    
}
}

and here is where i use it


<a class="post-name" href="profile.php?id=<?php print $from_id?>"><?php name()?></a> | <a href="#/posts&id=<?php print $id?>" class="post-date" title="<?php print date('l, jS \of F, Y \a\t g:ia ', $row['date']+60*60);?>"><?php print $date?></a> | <a class="post-comment" id="comment-toggle<?php print $id?>" onClick="comment_toggle('<?php print $id?>')">Comment</a> |<?php/*<a class="post-like" id="PostLike<?php print $id?>" onClick="<?php print $like_unlike?>('<?php print $id?>')"><?php print ucwords($like_unlike)?></a> <div class="num-likes" id="NumLikes<?php print $id?>"><?php print $num_likes?>*/?></div>

</div>

</li>

Link to comment
Share on other sites

ok , the query succeded but no results , i will post the code im using it for ...

here is where i set up where i get there name

function name()
{

$lookup = "SELECT firstname, lastname
          FROM myMembers
          WHERE id='$id'
          LIMIT 1";
$result = mysql_query($lookup);

if(!$result)
{
    echo "Query: {$lookup}<br>Error: " . mysql_error();
}
elseif(!mysql_num_rows($results))
{
    echo "There were no results.";
}
else
{
    $row = mysql_fetch_assoc($result);
    $name = "{$row['firstname']} {$row['lastname']}";
    
}
}

and here is where i use it


<a class="post-name" href="profile.php?id=<?php print $from_id?>"><?php name()?></a> | <a href="#/posts&id=<?php print $id?>" class="post-date" title="<?php print date('l, jS \of F, Y \a\t g:ia ', $row['date']+60*60);?>"><?php print $date?></a> | <a class="post-comment" id="comment-toggle<?php print $id?>" onClick="comment_toggle('<?php print $id?>')">Comment</a> |<?php/*<a class="post-like" id="PostLike<?php print $id?>" onClick="<?php print $like_unlike?>('<?php print $id?>')"><?php print ucwords($like_unlike)?></a> <div class="num-likes" id="NumLikes<?php print $id?>"><?php print $num_likes?>*/?></div>

</div>

</li>

 

You aren't getting any results because you are not returning/outputting anything from the function.

Link to comment
Share on other sites

how should i do it then?

 

function name($id)
{

$lookup = "SELECT firstname, lastname
          FROM myMembers
          WHERE id='$id'
          LIMIT 1";
$result = mysql_query($lookup);

if(!$result)
{
    echo "Query: {$lookup}<br />Error: " . mysql_error();
}
elseif(!mysql_num_rows($results))
{
    echo "There were no results.";
}
else
{
    $row = mysql_fetch_assoc($result);
    $name = "{$row['firstname']} {$row['lastname']}";
    echo $name; //function needs to output this
}
}

Link to comment
Share on other sites

There are a couple reasons why you aren't getting results.  Starting with $id is out of scope, as you have not passed it to the function. After that, you need to return the values from the function in order to use it the way you are trying to.

 

Functions in PHP

How to return values from functions in PHP

Variables scope in PHP (GLOBAL = Bad)!

 

Note: AyKay47's last post will not work because of the scope issue.

Link to comment
Share on other sites

There are a couple reasons why you aren't getting results.  Starting with $id is out of scope, as you have not passed it to the function. After that, you need to return the values from the function in order to use it the way you are trying to.

 

Functions in PHP

How to return values from functions in PHP

Variables scope in PHP (GLOBAL = Bad)!

 

Note: AyKay47's last post will not work because of the scope issue.

 

good catch, my code is updated, OP you will need to pass this variable when calling the function, as noted.

Link to comment
Share on other sites

You need to have php's error_reporting set to E_ALL (or even better a -1) and display_errors set to ON in your master php.ini on your development system so that php will report and display all the errors it detects. You will save a TON of time. There's a variable name mistyped in your code that was pointed out in reply #9 in this thread and the initial nonexistent $id variable inside the function, that would both have been producing undefined variable error messages that would have alerted you to the problems.

 

Now that you finally showed that you were using the offending code inside a function, in reply #8, and that someone finally posted code passing the $id value into the function as a parameter, you will need to alter the code that is calling that function to supply the $id variable as a call time parameter.

 

Also, functions should return values instead of directly echoing/printing them. This is so that the function will be general purpose and can be used anywhere in your code. If you return the resulting name, you can use your function to build a string in a variable or an in an echo statement, to build an array, as a value to another function call, as a value in a conditional comparison, inside a class,...

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.