Jump to content

[SOLVED] mysql query not working


sudsy1970

Recommended Posts

hi all,

 

am trying to create a file to fetch data from my database when someone forgets their password.  i keep getting loads of errors, each different everytime.  this time it's Parse error: syntax error, unexpected T_ECHO in ...on line 36.

 

I have very little hair left to pull any chance someone can tell me where i am going wrong ?

 

cheers

 

here's the code

<?	

$username=$_POST['myUsername'];


echo " you entered $username";

// Connect to mysql 
$dbServer=mysql_connect("localhost","0274148","8lgn62");

    if (!$dbServer) 
    {
     echo "Failed to connect to MySQL"; 
     exit;
    }
    	else 
    	{
     	  echo"connected<br>";
    	}
    	
    mysql_select_db("db0274148",$dbServer);
            $sql = "SELECT * FROM users WHERE username=\"".$_POST["myUsername"];
       	   $queryResult=mysql_query($sql);
  
       	    while ($dbRecord=mysql_fetch_array($queryResult))
       	    {
       	     echo "found: ".$dbRecord["username"].", ".$dbRecord["email"];
       	     trim($dbRecord["username"]);
       	     strtolower($dbRecord["username"]);
       	     
      // If username and email match the user verified. 
      // If not will redirect to the register page.
      
      if ($dbRecord["username"] == $_POST['myUsername)']
       {
         echo "You have been verified as a member";
         
       // send an email with the password in it.
       $msg ="Name:	".$dbRecord["firstname"]."\n";
       $msg.="E-Mail:	".$dbRecord["email"]."\n";
       $msg.="Message: You have been verified.\n Your password is".$dbRecord["password"]."\n";
       
       $recipient ="A.Suddes@wlv.ac.uk";
       $subject ="Password Recovery";
       $mailheaders ="From: My web site<Remember the 80's>\n";
       $mailheaders.="Reply to:".$dbRecord["email"];
       
       //Send the email
       
       mail($recipient,$subject,$msg,$mailheaders);
       }
      
         	else 
         	 {
         	  echo"Didn't work sudsy"; 
         	 }
   
?>

Link to comment
Share on other sites

Actually there is already a ')' but it is in the wrong place. (edit: and no I am not going to post the corrected line of code because it is a trivial matter to look at the line and make sure all the punctuation matches and is the correct place.)

 

Syntax errors are reported at the point that the language parser encounters something that it does not understand. This often occurs a line or two after the actual incorrect syntax because the parser thinks everything it encountered up to that point fits in with the syntax that results when you make a syntax error in your code. When you get a syntax error, look at both the line the error is reported on and work backward to see if you can find the problem yourself.

Link to comment
Share on other sites

That's another problem with posting "fixed" code. It often is done quickly, cannot be or is not tested, and is done by someone who after looking at thousands and thousands of bogus lines of code WILL miss seeing basic errors in it because they are expecting to be helping with real programming problems, not syntax errors.

Link to comment
Share on other sites

Cheers, have looked several times but can't see the wood for the trees, sometimes need to use a fresh set of eyes.

 

Found the missing ')' but now i have an error code i really don't understand:

 

Parse error: syntax error, unexpected $end in ...line 59

 

i don't have $end anywhere and my code finishes at line 58 ???

Link to comment
Share on other sites

ah cool,

 

wondered what it meant,  have now found it cheers.  got a supplied argument is not a valid MySQL result resource but hopefully can have a look at that and sort it.

 

 

cheers for the help guys

 

when your a newbie you sometimes just need a prod in the right direction.

Link to comment
Share on other sites

 

<?php
   $sql = "SELECT * FROM users WHERE username=\"".$_POST["myUsername"];
?>

 

This is more a matter of preference, but has saved me from simple mistakes.

 

Don't use array items in sql queries.

Set it to a variable and then use single quotes around it. Then you don't need to escape things in the query.

<?php
  $userName = $_POST['myUsername'];
$sql = "SELECT * FROM users WHERE username = '$userName'";
?>

 

For queries, use double quotes around the whole statement, and single quotes around the variables in the WHERE clauses.

 

Most any other time, I use single quotes and then concatenate the strings.

 

<?php
  echo 'This is a string with a '. $variable.' and is easier to find '. $mistakes;
?>

 

With code highlighting the variable sticks out and allows you to see syntax errors more easily.

 

Again, more of a personal preference. But this kind of "thinking" makes finding mistakes much easier.

 

Nate

 

Link to comment
Share on other sites

Ok,

 

have done some reading on the mysql subject and have looked at a tutorial but am still struggling a bit.

 

I get:

connecting to the database and searching for a record (thanks for the variable tip) and am fairly confident i could manage that.

 

Don't get:

Where this returned data goes to and how to access it properly. i,e

 

  $queryResult=mysql_query($sql); then it goes on later in the code

 

while ($dbRecord=mysql_fetch_array($queryResult))

 

this is what i dont get. can someone break it down into plain english for me.

 

<?
    $dbServer=mysql_connect("localhost","cp1079_student","");
    if (!$dbServer) {echo "Failed to connect to MySQL"; exit; }
    
    mysql_select_db("cp1079",$dbServer);
    
    $sql ="SELECT * FROM pet";
    $sql.=" WHERE name=\"".$_POST["searchName"]."\"";  // the space before the WHERE is critical
    
    $queryResult=mysql_query($sql);
    
    if (mysql_error())
    {
      echo "Problem with Query<BR>";
      echo "The following error message was returned from MySQL:<BR>";
      echo mysql_error();
      exit;
    }
    
    if (mysql_num_rows($queryResult)==0)
    {
      echo "No pets with that name.";
    }
    else
    {
      while ($dbRecord=mysql_fetch_array($queryResult))
      {
        echo "found: ".$dbRecord["name"].", ".$dbRecord["species"].", ".$dbRecord["owner"].", ".$dbRecord["age"]."<BR>";
      }
    }
?>

 

Link to comment
Share on other sites

ok then so if i modified that to read my database names and then i could use the $dbRecord references to input information into an email i.e

 

$msg echo "To:".$dbRecord["email"];

 

or something like that and it would populate the correct email address to send as long as i manage to point it to the correct entry in the database ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.