ifis Posted December 16, 2007 Share Posted December 16, 2007 I am trying to create a form were the result is a field in a MySQl database, but inside that field I have an number of variables I want to replace with variables I have previously set. I tried str_replace, but it does not seem to be working for 1 variable, and I am going to have a couple. Any suggestions? Is this not the easiest way to do it? Here is the code: <?php // Connect to server and select databse. mysql_connect("*******", "*****", "******")or die("cannot connect"); mysql_select_db("*****")or die("cannot select DB"); //get data $make=$_POST['make']; $lic = $_POST['lic']; $student=$_POST['student']; $sql = "SELECT * FROM Endorsements WHERE lic='$lic'"; $result=mysql_query($sql); //get the first entry from the result $row = mysql_fetch_array($result); echo str_replace("$student", $student,"$row[end]"); ?> The field in the MySQL database is: I certify that $student has satisfactorily completed the test for the $make. Link to comment https://forums.phpfreaks.com/topic/81866-solved-apply-php-variable-to-mysql-result-with-stored-variables/ Share on other sites More sharing options...
phpSensei Posted December 16, 2007 Share Posted December 16, 2007 You could use an array. http://ca.php.net/str_replace try <?php // Connect to server and select databse. mysql_connect("*******", "*****", "******")or die("cannot connect"); mysql_select_db("*****")or die("cannot select DB"); //get data $make=$_POST['make']; $lic = $_POST['lic']; $student=$_POST['student']; $sql = "SELECT * FROM Endorsements WHERE lic='$lic'"; $result=mysql_query($sql) or die(mysql_error()); //get the first entry from the result $row = mysql_fetch_array($result); $replace = array("$student","$make"); $with = array($studen,$make); echo str_replace($replace, $with, $row['end']); ?> Link to comment https://forums.phpfreaks.com/topic/81866-solved-apply-php-variable-to-mysql-result-with-stored-variables/#findComment-415928 Share on other sites More sharing options...
ifis Posted December 16, 2007 Author Share Posted December 16, 2007 I updated the code to: //get the first entry from the result $row = mysql_fetch_array($result); $find = array("$student","$make"); $replace = array($student,$make); echo str_replace($find,$replace,$row['end']); but it still does not replace the variables. The result is still: I certify that $student has satisfactorily completed the test for the $make. any more suggestions? Link to comment https://forums.phpfreaks.com/topic/81866-solved-apply-php-variable-to-mysql-result-with-stored-variables/#findComment-415945 Share on other sites More sharing options...
phpSensei Posted December 16, 2007 Share Posted December 16, 2007 I updated the code to: //get the first entry from the result $row = mysql_fetch_array($result); $find = array("$student","$make"); $replace = array($student,$make); echo str_replace($find,$replace,$row['end']); but it still does not replace the variables. The result is still: I certify that $student has satisfactorily completed the test for the $make. any more suggestions? I forgot to add slashes try <?php // Connect to server and select databse. mysql_connect("*******", "*****", "******")or die("cannot connect"); mysql_select_db("*****")or die("cannot select DB"); //get data $make=$_POST['make']; $lic = $_POST['lic']; $student=$_POST['student']; $sql = "SELECT * FROM Endorsements WHERE lic='$lic'"; $result=mysql_query($sql) or die(mysql_error()); //get the first entry from the result $row = mysql_fetch_array($result); $replace = array("\$student","\$make"); $with = array($student,$make); echo str_replace($replace, $with, $row['end']); ?> Link to comment https://forums.phpfreaks.com/topic/81866-solved-apply-php-variable-to-mysql-result-with-stored-variables/#findComment-415949 Share on other sites More sharing options...
papaface Posted December 16, 2007 Share Posted December 16, 2007 I suggest you change $student in your database to *student* to make things easier in the future. Then I think your code should be: <?php // Connect to server and select databse. mysql_connect("*******", "*****", "******")or die("cannot connect"); mysql_select_db("*****")or die("cannot select DB"); //get data $make=$_POST['make']; $lic = $_POST['lic']; $student=$_POST['student']; $sql = "SELECT * FROM Endorsements WHERE lic='$lic'"; $result=mysql_query($sql); //get the first entry from the result while ($row = mysql_fetch_assoc($result)) { echo str_replace("*student*", $student,$row["end"]); } ?> Link to comment https://forums.phpfreaks.com/topic/81866-solved-apply-php-variable-to-mysql-result-with-stored-variables/#findComment-415961 Share on other sites More sharing options...
ifis Posted December 16, 2007 Author Share Posted December 16, 2007 the second response from phpsensei worked. I have not tried the other code yet, but will. Thanks again, as usual! Link to comment https://forums.phpfreaks.com/topic/81866-solved-apply-php-variable-to-mysql-result-with-stored-variables/#findComment-415966 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.