TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-24-2008, 03:06 AM   #1 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Bug 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

Last edited by codefreek : 11-24-2008 at 06:03 AM.
codefreek is offline  
Reply With Quote
Old 11-24-2008, 06:39 AM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

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.
tony is offline  
Reply With Quote
Old 11-24-2008, 11:41 AM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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.
__________________
Tanax is offline  
Reply With Quote
Old 11-25-2008, 01:29 AM   #4 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

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?
Enfernikus is offline  
Reply With Quote
Old 11-25-2008, 07:56 AM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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 
__________________
Tanax is offline  
Reply With Quote
Old 11-25-2008, 09:21 AM   #6 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

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;
    }

codefreek is offline  
Reply With Quote
Old 11-25-2008, 09:41 AM   #7 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

And.. how do you use the function?
__________________
Tanax is offline  
Reply With Quote
Old 11-25-2008, 10:04 AM   #8 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

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

Last edited by codefreek : 11-25-2008 at 10:28 AM.
codefreek is offline  
Reply With Quote
Old 11-25-2008, 11:00 AM   #9 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

Is the `id` field unique to the user? If it is, then you will only get one row.
Enfernikus is offline  
Reply With Quote
Old 11-25-2008, 11:29 AM   #10 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

<solved><solved>
codefreek is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:17 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design