Jump to content

Help with pulling data from database


imimin

Recommended Posts

I have the following code that I am trying to use to dynamically pull data from a database to fill my metatags.  I have confirmed I have correctly connected to the DB.  I am getting the following error:

 

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /homepages/27/d120150310/htdocs/poj/index.php on line 23

 

Which is line 8 below.

 

The code I am using is:

 

		<?php
    $cat = $_GET['page'];
    $get_items = "SELECT * FROM head_data WHERE page='index.php'";
    $get_items = mysql_query($get_items);

	while($item_row = mysql_fetch_array($get_items)){
		$item_title = $item_row['title'];
		$item_desc = $item_row['desc'];
		$item_keywords = $item_row['keywords'];

		echo "<TITLE>$item_title</TITLE>
			<meta name="description" content="$item_desc">
			<meta name="keywords" content="$item_keywords">";
}

?>

 

I am not very good at this stuff yet, but am trying to learn.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/164046-help-with-pulling-data-from-database/
Share on other sites

Change this:

echo "<TITLE>$item_title</TITLE>
            <meta name="description" content="$item_desc">
            <meta name="keywords" content="$item_keywords">";

 

to this:

 

echo '<TITLE>$item_title</TITLE>
            <meta name="description" content="$item_desc">
            <meta name="keywords" content="$item_keywords">';

 

You were using double quotes to enclose the string, and using double quotes inside the string, which exits the double quotes. You need to use single quotes to enclose the string if you want to use double quotes inside it.

@haku:

Variables won't execute in single quotes.

 

echo '<TITLE>',$item_title,'</TITLE>
            <meta name="description" content="',$item_desc,'">
            <meta name="keywords" content="',$item_keywords,'">';

 

or:

echo "<TITLE>$item_title</TITLE>
            <meta name=\"description\" content=\"$item_desc\">
            <meta name=\"keywords\" content=\"$item_keywords\">";

Thanks, that did the trick.  I am now longer getting the error and my web page is loading but my DB data is not being populated in my variable.  When I look at the source of my web page I get this for the metatags:

 

		<TITLE>$item_title</TITLE>
			<meta name="description" content="$item_desc">
			<meta name="keywords" content="$item_keywords">

 

Any additional help would be appreciated!

Variables won't execute in single quotes.

 

echo '<TITLE>',$item_title,'</TITLE>
            <meta name="description" content="',$item_desc,'">
            <meta name="keywords" content="',$item_keywords,'">';

 

or:

echo "<TITLE>$item_title</TITLE>
            <meta name=\"description\" content=\"$item_desc\">
            <meta name=\"keywords\" content=\"$item_keywords\">";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.