jasonkirk Posted May 5, 2013 Share Posted May 5, 2013 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); Link to comment https://forums.phpfreaks.com/topic/277677-why-doesnt-separating-the-dollar-sign-work/ Share on other sites More sharing options...
blacknight Posted May 5, 2013 Share Posted May 5, 2013 $ 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 Link to comment https://forums.phpfreaks.com/topic/277677-why-doesnt-separating-the-dollar-sign-work/#findComment-1428477 Share on other sites More sharing options...
jasonkirk Posted May 6, 2013 Author Share Posted May 6, 2013 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 Link to comment https://forums.phpfreaks.com/topic/277677-why-doesnt-separating-the-dollar-sign-work/#findComment-1428599 Share on other sites More sharing options...
trq Posted May 6, 2013 Share Posted May 6, 2013 Variables are not interpolated within single quotes. Link to comment https://forums.phpfreaks.com/topic/277677-why-doesnt-separating-the-dollar-sign-work/#findComment-1428602 Share on other sites More sharing options...
jasonkirk Posted May 6, 2013 Author Share Posted May 6, 2013 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. Link to comment https://forums.phpfreaks.com/topic/277677-why-doesnt-separating-the-dollar-sign-work/#findComment-1428615 Share on other sites More sharing options...
cyberRobot Posted May 6, 2013 Share Posted May 6, 2013 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)); Link to comment https://forums.phpfreaks.com/topic/277677-why-doesnt-separating-the-dollar-sign-work/#findComment-1428617 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.