Glenskie Posted January 31, 2012 Share Posted January 31, 2012 this wont display there name $sql = mysql_query("SELECT firstname,lastname FROM myMembers WHERE id='$logOptions_id' LIMIT 1"); while($row = mysql_fetch_array($sql)){ $fn = $row["firstname"]; $ln = $row["lastname"]; $name=("$fn,$ln"); } Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/ Share on other sites More sharing options...
Psycho Posted January 31, 2012 Share Posted January 31, 2012 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; } Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/#findComment-1312803 Share on other sites More sharing options...
AyKay47 Posted January 31, 2012 Share Posted January 31, 2012 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() Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/#findComment-1312806 Share on other sites More sharing options...
Glenskie Posted January 31, 2012 Author Share Posted January 31, 2012 still no Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/#findComment-1312833 Share on other sites More sharing options...
Pikachu2000 Posted January 31, 2012 Share Posted January 31, 2012 Things like "doesn't work" and "still no" are meaningless. Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/#findComment-1312840 Share on other sites More sharing options...
Glenskie Posted January 31, 2012 Author Share Posted January 31, 2012 i know , im trying to think on how to say it , one sec Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/#findComment-1312841 Share on other sites More sharing options...
scootstah Posted January 31, 2012 Share Posted January 31, 2012 It's simple: What is happening vs what is supposed to happen. Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/#findComment-1312844 Share on other sites More sharing options...
Psycho Posted January 31, 2012 Share Posted January 31, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/#findComment-1312854 Share on other sites More sharing options...
Glenskie Posted February 1, 2012 Author Share Posted February 1, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/#findComment-1313181 Share on other sites More sharing options...
Drummin Posted February 1, 2012 Share Posted February 1, 2012 elseif(!mysql_num_rows($results)) Should be elseif(!mysql_num_rows($result)) Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/#findComment-1313190 Share on other sites More sharing options...
Drummin Posted February 1, 2012 Share Posted February 1, 2012 Don't you need to echo the name? $row = mysql_fetch_assoc($result); $name = "{$row['firstname']} {$row['lastname']}"; echo "$name"; Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/#findComment-1313192 Share on other sites More sharing options...
AyKay47 Posted February 1, 2012 Share Posted February 1, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/#findComment-1313277 Share on other sites More sharing options...
Glenskie Posted February 2, 2012 Author Share Posted February 2, 2012 how should i do it then? Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/#findComment-1313543 Share on other sites More sharing options...
AyKay47 Posted February 2, 2012 Share Posted February 2, 2012 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 } } Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/#findComment-1313546 Share on other sites More sharing options...
jcbones Posted February 2, 2012 Share Posted February 2, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/#findComment-1313549 Share on other sites More sharing options...
AyKay47 Posted February 2, 2012 Share Posted February 2, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/#findComment-1313550 Share on other sites More sharing options...
Glenskie Posted February 2, 2012 Author Share Posted February 2, 2012 this is so weird , its still not posting the name of the user , should i post extra code? this are two different functions that im using so mabe thats why? Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/#findComment-1313594 Share on other sites More sharing options...
PFMaBiSmAd Posted February 2, 2012 Share Posted February 2, 2012 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,... Quote Link to comment https://forums.phpfreaks.com/topic/256083-not-working/#findComment-1313648 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.