will35010 Posted June 2, 2009 Share Posted June 2, 2009 I want to use echo instead of return in my function. I keep getting error about the syntax being wrong. It's saying the echo line is wrong. function getDrivers1() { $sql = "SELECT fname, lname, dl FROM drivers"; $rs = mysql_query($conn, $sql); while($row = mysql_fetch_array($rs)) { echo "<option value=\"$row['dl']\">".$row['lname'].",".$row['fname']."</option>"; } Link to comment https://forums.phpfreaks.com/topic/160677-solved-syntax-problem/ Share on other sites More sharing options...
anupamsaha Posted June 2, 2009 Share Posted June 2, 2009 Please change to: echo "<option value=\"{$row['dl']}\">".$row['lname'].",".$row['fname']."</option>"; Or, echo "<option value=\"" . $row['dl'] . "\">".$row['lname'].",".$row['fname']."</option>"; Whichever way you like. Link to comment https://forums.phpfreaks.com/topic/160677-solved-syntax-problem/#findComment-847950 Share on other sites More sharing options...
Ken2k7 Posted June 2, 2009 Share Posted June 2, 2009 Read mysql_query. Put $conn after $sql in the parameters. Link to comment https://forums.phpfreaks.com/topic/160677-solved-syntax-problem/#findComment-847952 Share on other sites More sharing options...
will35010 Posted June 2, 2009 Author Share Posted June 2, 2009 I keep getting error connecting to mysql. I know my $conn works, because my other scripts use it and it works. Here are the files: functions.php <?php //db connection info require('opendb.php'); require('config.php'); function getDrivers1() { $sql = "SELECT fname, lname, dl FROM drivers"; $rs = mysql_query($sql, $conn); while($row = mysql_fetch_array($rs)) { echo "<option value=\"" . $row['dl'] . "\">".$row['lname'].",".$row['fname']."</option>"; } } config.php: <?php //Database Settings $dbhost = "localhost"; $dbuser = "root"; $dbpass = "test"; $dbname = "vehicles"; $prefix = ""; $user_prefix = ""; $dbtype = "MySQL" ?> opendb.php <?php // This is opendb.php $conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die ('Error connecting to mysql'); ?> I'm using <?php echo getDrivers1() ?> within the html to call the function. I also have a require function.php. Link to comment https://forums.phpfreaks.com/topic/160677-solved-syntax-problem/#findComment-847981 Share on other sites More sharing options...
will35010 Posted June 2, 2009 Author Share Posted June 2, 2009 Read mysql_query. Put $conn after $sql in the parameters. Thank you! Link to comment https://forums.phpfreaks.com/topic/160677-solved-syntax-problem/#findComment-847984 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.