TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Help with my function. (http://www.talkphp.com/general/3643-help-my-function.html)

codefreek 11-24-2008 03:06 AM

Help with my function.
 
hey, well here is my script

PHP Code:

function readUsers() {
    
$sql sprintf(" SELECT * FROM `crud_` WHERE `id` = '%s'"
    
mysql_real_escape_string($id));
    
$res = @mysql_query($sql) or die(mysql_error());
    if (
$res) {
      
$output '';
        while (
$row mysql_fetch_array($resMYSQL_ASSOC)) {
            
$output .= "First Name :{$row['fname']} <br>" "username :{$row['uname']} <br>" "email : :{$row['email']} <br>" "phone :{$row['phone']} <br>";
        }
      return 
$output;
    }else{
      return 
false;
    }


and then on a page called showusers i have like this

PHP Code:

<?php
include "crud_functions.php";
connect();


//list users
$listUsers readUsers();
echo 
$listUsers;


?>

but the showusers page turns blank..
why ?

Thank you in advance.
.Codefreek

tony 11-24-2008 06:39 AM

I can test the code since I don't have a database I can test it with now, but some i think you can find out more if you omit the '@' from this line: $res = @mysql_query($sql) or die(mysql_error());

It omits the server from sending an error, if there is any, in the query to the database.

Tanax 11-24-2008 11:41 AM

Don't really think this should be in the advanced section.

Anyhow.
Why are you returning false if you don't run a check on it?
PHP Code:

if($listUsers)
{
// Function returned true


HOWEVER. Your function will return true, even if there are no results.
So you would probably have to check if the function returned any results, aswell as checking if it returned true or false.

Enfernikus 11-25-2008 01:29 AM

This is assuming `id` is numerical

PHP Code:

sprintf(" SELECT * FROM `crud_` WHERE `id` = '%s'"
    
mysql_real_escape_string($id)); 

Could probably be changed to

PHP Code:

sprintf("SELECT * FROM `crud_` WHERE `id` = %d"
    (int)
$id); 


As to the actual function, have you checked if it's returning false?

Tanax 11-25-2008 07:56 AM

And to add to Enfernikus' message, you don't even SET the $id anywhere, so it's probably NULL.

PHP Code:

function ReadUsers($id

PHP Code:

$readUsers readUsers(4); // For example 


codefreek 11-25-2008 09:21 AM

code changes..

PHP Code:


function readUsers($id) {
    
$sql sprintf("SELECT * FROM `crud_` WHERE `id` = %d"
    (int)
$id);
    
$res = @mysql_query($sql) or die(mysql_error());
    if (
$res) {
      
$output '';
        while (
$row mysql_fetch_array($resMYSQL_ASSOC)) {
            
$output .= "First Name :{$row['fname']} <br>" "username :{$row['uname']} <br>" "email : :{$row['email']} <br>" "phone :{$row['phone']} <br>";
        }
      return 
$output;
    }else{
      return 
false;
    }



Tanax 11-25-2008 09:41 AM

And.. how do you use the function? :-P

codefreek 11-25-2008 10:04 AM

when i do $listUsers = readUsers(1); instead of $id
i get back a row, but as suspected only one. :S

on var dump, i get string(0); when run on $id

Enfernikus 11-25-2008 11:00 AM

Is the `id` field unique to the user? If it is, then you will only get one row.

codefreek 11-25-2008 11:29 AM

<solved><solved>


All times are GMT. The time now is 08:03 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0