Jump to content

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
}				

?>

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

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. 

Assuming you aren't getting any errors, if the query works when you hard code a value in, but not when you use a variable, then the variable probably doesn't contain the value you think it does. Echo the query string and see.

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.