Jump to content


Photo

Why doesn't separating the dollar sign work


  • Please log in to reply
5 replies to this topic

#1 jasonkirk

jasonkirk

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 05 May 2013 - 04:03 PM

Setup a array that held all the query info for a database with the plan to put the second of code that updates the database into a function.  But hasn't been working.  In the end I widdled it down to the question, why don't these two bits of code work the same?
 
Array:
 
$database_info = array("db_table_name" => tbl_detentions, "db_field_names" => array("rel_student_uid", "detention_date"));

 

Database code:
 
$query = "INSERT INTO ". $database_info['db_table_name'];
$query .= " (" . $database_info['db_field_names'][0] . ", " . $database_info['db_field_names'][1] . ") VALUES ";
$query .= "($" . "rel_student_uid, '$detention_date')";
$result = mysql_query($query);
 
 
 
$query = "INSERT INTO ". $database_info['db_table_name'];
$query .= " (" . $database_info['db_field_names'][0] . ", " . $database_info['db_field_names'][1] . ") VALUES ";
$query .= "($rel_student_uid, '$detention_date')";
$result = mysql_query($query);


#2 blacknight

blacknight

    Advanced Member

  • Members
  • PipPipPip
  • 245 posts
  • Locationsaint john

Posted 05 May 2013 - 04:58 PM

$ in php marks the calling of a var weather it be array or what not if the $ is used improperly it will error out

 

$query .= "($" . "rel_student_uid, '$detention_date')";

 

reads the $ as a var call with no var name

 

$query .= "($rel_student_uid, '$detention_date')";

reads the $rel_student_uid as a var for php to call

 

any $ inside " " will be treated as a php var to avoid this if you are thing $ as just a character use mysql escape or enclose it in ' ' 

this making any $ inside a ' ' reas as text unless code escaped with ' ' . $var . ' ' or for doubles " " . $var . " " i personally always code escape my var and use mysql escape

 

i hope this helps


if at first your fail google will give you a pass

#3 jasonkirk

jasonkirk

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 06 May 2013 - 08:36 AM

Thanks it helps a bit though still haven't quite grasped. Tried making the original array variable have the $ instead of after the fact and using the mysql escape but still get the variable name $rel_student_uid instead of the actual uid.

 

 INSERT INTO tbl_detentions (rel_student_uid, detention_date) VALUES ('$rel_student_uid', '2013-3-3')

 

 

 

 

 

$database_info = array("db_table_name" => tbl_detentions, "db_field_names" => array("rel_student_uid", "detention_date"), "form_fields" => array('$rel_student_uid', "$detention_date"));
 

 

// Add detention to database BEGIN
$testt = mysql_real_escape_string($database_info['form_fields'][0]);

 

 

$query = "INSERT INTO ". $database_info['db_table_name'];
$query .= " (" . $database_info['db_field_names'][0] . ", " . $database_info['db_field_names'][1] . ") VALUES ";
$query .= "('" . mysql_real_escape_string($testt) . "', '$detention_date')";
$message .= $query;
$result = mysql_query($query);
if($result) {
$message .= "<center>Student given detention.</center>";
 
} else {
$message .= "Problem adding detention to student record.";
$message .= "<br>" . $query;
 
}
$detention_count++;
}
// Add detention to database END

 



#4 trq

trq

    Advanced Member

  • Administrators
  • 31,023 posts
  • LocationSydney, Australia.

Posted 06 May 2013 - 08:46 AM

Variables are not interpolated within single quotes.

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#5 jasonkirk

jasonkirk

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 06 May 2013 - 09:15 AM

Yea, I'm starting to see that treating ' and " the same has finally caught up and bit me.  I tended to do it to avoid escape characters so I could have quotes inside quotes without the confusion of \ all over the place.  Still haven't figured out the right combination yet.  I can break things worse but can't seem to go in the other direction.



#6 cyberRobot

cyberRobot

    Advanced Member

  • Gurus
  • 1,758 posts

Posted 06 May 2013 - 09:20 AM

In some cases, you may want to consider removing the quotes altogether.

 

$database_info = array("db_table_name" => tbl_detentions, "db_field_names" => array("rel_student_uid", "detention_date"), "form_fields" => array($rel_student_uid, $detention_date));

Blogging about PHP and other web development topics at ScorpBytes.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com