MySQL/PHP Script Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
neex1233
Forum Newbie
Posts: 10
Joined: Sun Jun 21, 2009 7:44 pm

MySQL/PHP Script Error

Post by neex1233 »

I made this script that checks if the user has more than one in a MySQL row, and if they do it displays their eMail. If they don't it tells the user that they don't want it to be shown. Here it is:

Code: Select all

 
<?
require '/home/username/public_html/folder/config.php';
$username = mysql_real_escape_string($_SESSION['username']); 
$sql="SELECT * FROM users WHERE username='$username'";
$result=mysql_query($sql) or die(mysql_error());
$rows=mysql_fetch_array($result);
$email =  $row['email'];
$do = 1;
 
function doSmily($msg) //Fix eMail
{
$msg = str_ireplace('@', ' [AT] ', $word);
$msg = str_ireplace('.', ' [DOT] ', $word);
return $word;
}
$s = "$email";
$ne = doSmily($s);
 
if($rows['e'] >= $do){  // Check if user has 2
echo "$ne"; 
}else{
echo "This user chose not to show their eMail.";
}
?>
 
If they have more than one in the 'e' column (in the MySQL DB), instead of displaying the user's email, it just says they dont want to show it. Could you help me fix this? Thanks.
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: MySQL/PHP Script Error

Post by BornForCode »

I assume that in the specified table you have only one record per user, the code is:

Code: Select all

 
<?php
require '/home/username/public_html/folder/config.php';
$username = mysql_real_escape_string($_SESSION['username']);
$sql="SELECT * FROM users WHERE username='".$username."'";
$result=mysql_query($sql) or die(mysql_error());
 
// One record is supposed to be returned
$row=mysql_fetch_assoc($result);
$do = 1;
 
function doSmily($msg) //Fix eMail
{
    $msg = str_ireplace('@', ' [AT] ', $word);
    $msg = str_ireplace('.', ' [DOT] ', $word);
    return $msg;
}
 
 
// If he has more then an address
$message = ($row['e'] >= $do)? doSmily($row['email']) : 'This user chose not to show their eMail';
echo $message;
 
Last edited by BornForCode on Tue Jun 30, 2009 10:42 am, edited 3 times in total.
neex1233
Forum Newbie
Posts: 10
Joined: Sun Jun 21, 2009 7:44 pm

Re: MySQL/PHP Script Error

Post by neex1233 »

That still doesn't work.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: MySQL/PHP Script Error

Post by Eric! »

Can you be more descriptive than does not work? I found several problems with the code. Also insert

Code: Select all

echo $rows['e'];
at line 19 for debugging and see what is there.

Line 8 should be:

Code: Select all

$email =  $rows['email']; // rows not row
Your function should be

Code: Select all

function doSmily($msg) //Fix eMail
 {
  $msg = str_ireplace('@', ' [AT] ', $msg);
  $msg = str_ireplace('.', ' [DOT] ', $msg);
  return $msg;
}
Without more information about your problem that is the best I can do. Most likely the problem is your function was returning $word which is an undefined value and you were trying to echo it as an email address.
neex1233
Forum Newbie
Posts: 10
Joined: Sun Jun 21, 2009 7:44 pm

Re: MySQL/PHP Script Error

Post by neex1233 »

By not working I mean that even if the user has more than one in the e column, it says that they chose not to show their email. Also, if I use that code, I get this error:

Parse error: syntax error, unexpected T_ELSE in /home/username/public_html/folder/folder/e.php on line 22

But this is the final code:

Code: Select all

 
<?php
require '/home/username/public_html/folder/config.php';
$username = mysql_real_escape_string($_SESSION['username']); 
$sql="SELECT * FROM users WHERE username='$username'";
$result=mysql_query($sql) or die(mysql_error());
$rows=mysql_fetch_array($result);
$do = 1;
echo "do = {$do}; e = {$rows['e']}<br />\n";
$ee = $rows['email'];
 
function doSmily($msg) //Fix eMail
 {
  $msg = str_ireplace('@', ' [AT] ', $msg);
  $msg = str_ireplace('.', ' [DOT] ', $msg);
  return $msg;
}
if($rows['e'] >= $do)
echo $rows['e']; 
{  // Check if user has 2
  echo doSmily($row['email']); 
}
else
{
  echo "This user chose not to show their eMail.";
}
 
?>
 
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: MySQL/PHP Script Error

Post by BornForCode »

I updated the code, check it out.
neex1233
Forum Newbie
Posts: 10
Joined: Sun Jun 21, 2009 7:44 pm

Re: MySQL/PHP Script Error

Post by neex1233 »

Which code?
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: MySQL/PHP Script Error

Post by BornForCode »

The code from my post :) :dubious:
neex1233
Forum Newbie
Posts: 10
Joined: Sun Jun 21, 2009 7:44 pm

Re: MySQL/PHP Script Error

Post by neex1233 »

Okay, well even though the user has more than one, I get this:

This user chose not to show their eMail
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: MySQL/PHP Script Error

Post by BornForCode »

Are you sure that the value of $row['e'] is greater than 0?
Can you check please by putting an echo to it somewhere at the end of the code.
neex1233
Forum Newbie
Posts: 10
Joined: Sun Jun 21, 2009 7:44 pm

Re: MySQL/PHP Script Error

Post by neex1233 »

I am.
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: MySQL/PHP Script Error

Post by BornForCode »

Does the guys from devenet charging the posts based on the number of words used? :twisted:
neex1233
Forum Newbie
Posts: 10
Joined: Sun Jun 21, 2009 7:44 pm

Re: MySQL/PHP Script Error

Post by neex1233 »

What???
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: MySQL/PHP Script Error

Post by Eric! »

You put the echo debug statement in the wrong place. And BornForCode's code still has the $word bug in it.

Try this:

Code: Select all

<?php
require '/home/username/public_html/folder/config.php';
$username = mysql_real_escape_string($_SESSION['username']);
$sql="SELECT * FROM users WHERE username='$username'";
$result=mysql_query($sql) or die(mysql_error());
$rows=mysql_fetch_array($result);
$do = 1;
echo "do = {$do}; e = {$rows['e']}<br />\n";
$ee = $rows['email'];
 
function doSmily($msg) //Fix eMail
 {
  $msg = str_ireplace('@', ' [AT] ', $msg);
  $msg = str_ireplace('.', ' [DOT] ', $msg);
  return $msg;
}
 
echo "This is rows e: ".$rows['e']; // for debugging only
if($rows['e'] >= $do)
{  // Check if user has 2
  echo doSmily($row['email']);
}
else
{
  echo "This user chose not to show their eMail.";
}
 
?>
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: MySQL/PHP Script Error

Post by BornForCode »

Nooo i've fixed that :mrgreen:
Post Reply