Page 1 of 1
small function problem [assistance would be great]
Posted: Sun Nov 23, 2008 11:18 pm
by UnkownHero
this is my function
Code: Select all
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($res, MYSQL_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 i have a page to show the users from the db. like this
Code: Select all
<?php
include "crud_functions.php";
connect();
selectDB();
//list users
$listUsers = readUsers();
echo $listUsers;
?>
but the page turns blank..
why ?
Thanks in advance!
Re: small function problem [assistance would be great]
Posted: Mon Nov 24, 2008 12:22 am
by Eran
Try var_dump($listUsers) instead, and see what the contents are.
Re: small function problem [assistance would be great]
Posted: Tue Nov 25, 2008 3:14 am
by UnkownHero
will try it..
Re: small function problem [assistance would be great]
Posted: Tue Nov 25, 2008 3:47 am
by Rovas
I think your query or the strings you use for the connection aren' t correct.
Try this:
Code: Select all
$sql = sprintf(" SELECT * FROM `crud_` WHERE `id` = '%s'",
mysql_real_escape_string($id) .";");
//if you put @ PHP will ignore and not display any error message that function outputs
$res = mysql_query($sql) or die(echo "Query failed because: " .mysql_error());
if ($res) {
//testing purposes
$output ="Data: <hr />'';
while ($row = mysql_fetch_assoc($res)) {
// don' t put variables that refer to array values in a string -recipe for problems
$output .= "First Name :{" .$row['fname'] ."} <br>" . "username :{" $row['uname'] ."} <br>" . "email : {" .$row['email'] ."} <br>" . "phone :{".$row['phone'] ."} <br>";
}
//same as before
$output .="<hr />";
return $output;
}
// if you return the output directly you don' t need to add else unless you have a different string for $output
Re: small function problem [assistance would be great]
Posted: Tue Nov 25, 2008 4:28 am
by UnkownHero
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
Re: small function problem [assistance would be great]
Posted: Tue Nov 25, 2008 4:47 am
by UnkownHero
Rovas wrote:I think your query or the strings you use for the connection aren' t correct.
Try this:
Code: Select all
$sql = sprintf(" SELECT * FROM `crud_` WHERE `id` = '%s'",
mysql_real_escape_string($id) .";");
//if you put @ PHP will ignore and not display any error message that function outputs
$res = mysql_query($sql) or die(echo "Query failed because: " .mysql_error());
if ($res) {
//testing purposes
$output ="Data: <hr />'';
while ($row = mysql_fetch_assoc($res)) {
// don' t put variables that refer to array values in a string -recipe for problems
$output .= "First Name :{" .$row['fname'] ."} <br>" . "username :{" $row['uname'] ."} <br>" . "email : {" .$row['email'] ."} <br>" . "phone :{".$row['phone'] ."} <br>";
}
//same as before
$output .="<hr />";
return $output;
}
// if you return the output directly you don' t need to add else unless you have a different string for $output
$listUsers = readUsers(1);
if($listUsers)
{
print $listUsers;
}
var_dump($listUsers);
if($listUsers == false)
{
print "no users or this is an error!";
}
works but only gives me one row from the db :S
maybe take out the where id part and just select all ? :S
Re: small function problem [assistance would be great]
Posted: Tue Nov 25, 2008 5:27 am
by Rovas
Yes you take out the id you want to see all of them.
Re: small function problem [assistance would be great]
Posted: Tue Nov 25, 2008 5:29 am
by UnkownHero
<solved><solved>