Jump to content

mixing html and php variable names in an echo statement


Aaron4osu

Recommended Posts

I'm trying to input the results from a query into html, but I'm just getting the variable names rather than their values.  I used single quotes for the echo statement, but I think I must need to switch to double to do that.  But then I have problems with the double quotes from the class names (ex. class="SideBoxTitle"). 

 

<?php
// get user's videos from database
$conn = mysqli_connect($dbhost, $dbuser, $dbpass) 
			or die("MySQL Error: " . mysql_error());

$query = "SELECT * FROM haas12_test.videos";

$result = mysqli_query($conn, $query)
			or die ("Couldn't execute query.");

while($row = mysqli_fetch_assoc($result))
{
extract($row);

echo ' 
	<div id="topBox"  class="mainVideoSideBoxes" >
	    	
	      <div class="SideBoxTitle" ><h3> $vidtitle </h3> </div><!-- close SideBoxTitle -->
	        <div class="sideBoxVidContainer"> 
	            <div class="SideBoxScreenCast" > $vidurl </div><!-- close SideBoxScreenCast -->
	            <div class="SideBoxDesc" ><p> $viddesc </p> </div><!-- close SideBoxDesc -->
	        </div><!-- close sideBoxVidContainer -->
	    
	</div><!-- close mainVideoSideBoxes -->

</div><!-- close mainVideoSideBoxes -->
'; // end echo staement
}				

?>

Link to comment
Share on other sites

Variables are only parsed in a string that is defined with double quotes or the heredoc method of defining strings.

 

You can use the double quotes to define your string and single quotes around the html tag parameter values or you can escape the double quotes around the tag parameters.

//Use single quotes for html parameters
echo "< img src='{$row['image_source']}' />";

//Escape double quotes for html parameters
echo "< img src=\"{$row['image_source']}\" />";

 

Of if you don't want to use single-quotes and you don't want to escape look into the heredoc method: http://www.php.net/manual/en/language.types.string.php

Link to comment
Share on other sites

Thanks!  That fixed that part, but I tried to do the same think here and it isn't working.

$query = "SELECT * FROM haas12_test.videos where username = \"$username\"";

 

It works if I substitute $username for an actual username from the database. I read the link you posted on the heredoc method but it didn't understand it. 

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.