/    Sign up×
Community /Pin to ProfileBookmark

Outputting Data More Efficiently

[code=php]<?php // let’s go!

// get the news!

$postID = $_GET[‘postID’];
$title = $_GET[‘title’];

// make sure that GET is set

if (!isset($_GET[‘postID’]) || !isset($_GET[‘title’]))
{

// if not, display an error

die(‘There is no data.’);

}

// require the connection and formatting settings

require_once(“path_to_connection”);
require_once(“path_to_code”);

// set up the access

$conn = dbAccess();

$query1 = “SELECT `a`.`post_id`, `a`.`user_id`, `a`.`author_id`, `a`.`author_name`, `a`.`date_posted`, `a`.`author_email`, `a`.`category_id`, `c`.`category_name`, `a`.`title`, `a`.`post_body`, `a`.`tags`, `a`.`info` FROM `posts` `a` INNER JOIN `categories` `c` ON `a`.`category_id` = `c`.`category_id` WHERE `a`.`post_id` = ? AND `a`.`title` = ? LIMIT 1”;

if ($stmt1 = $conn->prepare($query1))
{

// bind the parameters

if ($stmt1->bind_param(‘is’, $newID1, $newTitle1))
{

$newID1 = $postID;
$newTitle1 = $title;

if ($stmt1->execute())
{

// bind the result

if ($stmt1->bind_result($postIDNULL, $userIDNULL, $authorIDNULL, $authorNULL, $dateNULL, $emailNULL, $catIDNULL, $category, $newsTitle, $bodyNULL, $newsTags, $newsInfo))
{

if ($stmt1->fetch())
{

?>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>

<head>

<title><?php echo $newsTitle . ” — Ultimate Hogwarts: The Rebirth”; ?></title>

<link href=”/css/general2.css” rel=”stylesheet” type=”text/css”>
<link href=”/css/ulists.css” rel=”stylesheet” type=”text/css”>
<link href=”/css/test.css” rel=”stylesheet” type=”text/css”>

<meta name=”keywords” content=”<?php echo $newsTags; ?>”>
<meta name=”description” content=”<?php echo $newsInfo; } } } } $stmt1->close(); } ?>”>
<?php // include the remaining meta information
include_once(“../includes/meta_info.html”);
?>

</head>

<body>

<div id=”wrapper”>

<div id=”headers”>

<div id=”header1″></div>

<div id=”header2″></div>

</div>

<div id=”navigation”>

<!– Begin Navigation Menu –>

<?php // include the navigation menu

include_once(“../includes/navigation2.html”); ?>

<!– End Navigation Menu –>

</div>

<div id=”right”>

<p>Hello!</p>

</div>

<div align=”center” id=”center”>

<div id=”news”>

<?php

// write the query

// REMEMBER JEFFREY: WHEN USING INNER JOIN IN A SQL QUERY, BOTH THE MYSQL FIELD AND PREFIX NEED TO BE TICKED AND DOTTED, AS SO: `a`.`username`

$query2 = “SELECT `a`.`post_id`, `a`.`author_id`, `a`.`author_name`, DATE_FORMAT(`a`.`date_posted`, ‘%W, %M %d %Y %l:%i %p’), `a`.`author_email`, `a`.`category_id`, `c`.`category_name`, `a`.`title`, `a`.`post_body` FROM `posts` `a` INNER JOIN `categories` `c` ON `a`.`category_id` = `c`.`category_id` WHERE `a`.`post_id` = ? AND `a`.`title` = ? LIMIT 1”;

if ($stmt2 = $conn->prepare($query2))
{

if ($stmt2->bind_param(‘is’, $newID2, $newTitle2))
{

$newID2 = $postID;
$newTitle2 = $title;

if ($stmt2->execute())
{

// bind the results

if ($stmt2->bind_result($post_id, $author_id, $author, $date, $email, $catID, $category, $newNewsTitle, $body))
{

if ($stmt2->fetch())
{

$body = html_entity_decode($body, ENT_QUOTES, ‘UTF-8’);

echo “<div class=’news’><b>$newNewsTitle</b></div>”;
echo “<div class=’news’><b>Posted By:</b> $author on $date in $category</div>”;
echo “<div class=’news_post’>” . BBCODE(strip_tags($body, ‘<p><br><a><b><i>’)) . “</div>”;

}

}

}

}

$stmt2->close();

}

$conn->commit();

$conn->close();

?>

</div>

</div>

</div>

</body>

</html>

[/code]

If there is a more efficient way of getting the first set of data out, I would like to know. All I need is to get the title, tags, and info out for SEO purposes, but it doesn’t seem to work unless I pull all of the data from the database. Would this code:

[code=php]$query = “SELECT `title`, `tags`, `info` FROM `posts` WHERE `post_id` = ? AND `title` = ? LIMIT 1”;[/code]

work? I just want to make sure I’m trying the right thing before I go into debugging.

to post a comment
PHP

17 Comments(s)

Copy linkTweet thisAlerts:
@chazzyDec 16.2008 — i asked you this before and you dodged the question. why are you issuing the same query twice?
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorDec 16.2008 — My apologies. Didn't mean to dodge the question, I thought you saw this in the last post:

[code=php]Warning: mysqli::prepare() [mysqli.prepare]: All data must be fetched before a new statement prepare takes place in /home/josephwitchard/uhrebirth.com/test/show_news.php on line 135 [/code]

I was getting that error, so my best guess was that it meant I needed to pull all of the data out, because I was getting that error when I only tried getting just those three fields. After I started pulling everything out, it went away.

The reason I'm issuing a query beforehand is because I want to get the title, tags, and info out for SEO purposes. After all of that information goes in the <head>, I continue to build the page and display the post.
Copy linkTweet thisAlerts:
@chazzyDec 16.2008 — I think you're assuming that the webpage gets sent back in chunks. PHP is compiled and executed first, before anything gets sent back to the browser. This holds true no matter how many queries you execute.

And yes I saw that in your last post, and no it is not a valid reason why you execute the same query twice.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorDec 16.2008 — I guess I thought it just made more sense to only pull out what you needed right then. Bad call?
Copy linkTweet thisAlerts:
@ayveghDec 16.2008 — Not a completely bad call, but keep in mind that you should never have two queries retrieving the same information; once you have it, use it, don't fetch it again unless you know that it has changed.
Copy linkTweet thisAlerts:
@chazzyDec 16.2008 — I guess I thought it just made more sense to only pull out what you needed right then. Bad call?[/QUOTE]

but, you're not. your query is essentially the same in both cases (I only see 2 columns different). you don't "reduce strain" on the server by reducing the # of columns returned, as the pointer returned is generally about the same size if it's 1 column or 10.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorDec 16.2008 — [code=php]<?php // let's go!

// get the news!

$postID = $_GET['postID'];
$title = $_GET['title'];

// make sure that GET is set

if (!isset($_GET['postID']) || !isset($_GET['title']))
{

// if not, display an error

die('There is no data.');

}

// require the connection and formatting settings

require_once("path_to_connection");
require_once("path_to_formatting");



// set up the access

$conn = access_function();

$query = "SELECT post_id, author_id, author_name, DATE_FORMAT(date_posted, '&#37;W, %M %d, %Y %l:%i %p'), author_email, a.category_id, c.category_name, title, post_body, tags, info FROM posts a INNER JOIN categories c ON a.category_id = c.category_id WHERE post_id = ? AND title = ? LIMIT 1";

if ($stmt = $conn->prepare($query))
{

// bind the parameters

if ($stmt->bind_param('is', $newID, $newTitle))
{

$newID = $postID;
$newTitle = $title;

if ($stmt->execute())
{

// bind the result

if ($stmt->bind_result($post_id, $author_id, $author, $datePosted, $author_email, $aCatID, $cCatName, $newsTitle, $body, $tags, $info))
{

if ($stmt->fetch())
{

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>

<title><?php echo $newsTitle . " -- Ultimate Hogwarts: The Rebirth"; ?></title>

<link href="/css/general2.css" rel="stylesheet" type="text/css">
<link href="/css/ulists.css" rel="stylesheet" type="text/css">
<link href="/css/test.css" rel="stylesheet" type="text/css">

<meta name="keywords" content="<?php echo $tags; ?>">
<meta name="description" content="<?php echo $info; ?>">
<?php // include the remaining meta information
include_once("../includes/meta_info.html");
?>

</head>

<body>


<div id="wrapper">

<div id="headers">

<div id="header1"></div>

<div id="header2"></div>

</div>

<div id="navigation">

<!-- Begin Navigation Menu -->

<?php // include the navigation menu

include_once("../includes/navigation2.html"); ?>

<!-- End Navigation Menu -->

</div>

<div id="right">

<p>Hello!</p>

</div>

<div align="center" id="center">

<div id="news">

<p>

<?php

// write out the post

echo "<div class='news'><a href='/test/show_news.php/{$postID}/{$title}'>$newsTitle</a></div>";
echo "<div class='news'><b>Posted By:</b> <a href='mailto:$author_email'>$author</a> on $datePosted in $cCatName</div>";
echo "<div class='news_post'>" . BBCODE(strip_tags($body, '<p><br><a><b><i>')) . "</div>";

}



}

}

}

$stmt->close();

}



$conn->commit();

$conn->close();


?>

</p>


</div>



</div>

</div>

</body>

</html>[/code]


How's that?
Copy linkTweet thisAlerts:
@chazzyDec 17.2008 — since you're going for stuff w/ more sp33dh0l3s, you could rewrite statements like this:

[code=php]
echo "<div class='news'><a href='/test/show_news.php/{$postID}/{$title}'>$newsTitle</a></div>";
[/code]

As
[code=php]
printf("<span class='news'><a href='/test/show_news.php/%s/%s'>%s</a></span>",$postID,$title,$newsTitle);
[/code]


In general, people believe that echo is faster than printf. This is true if the input does not contain any variables (for echo). for echo to handle variables it has to do some string manipulation to pull out the variable and resolve it before processing.

printf on the other hand does string replacement, looking for the tokens specifically in the input and replacing them with the values popped in via the associated array. printf also has the advantage of being purely low level (meaning it just invokes the low level matching function) vs. echo which has to locate stdout (which ironically is not the real stdout).
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorDec 17.2008 — Would I really need to do:

[code=html]<span class='news'>[/code]

instead of:

[code=html]<div class='news'>[/code]

if I used printf?
Copy linkTweet thisAlerts:
@chazzyDec 17.2008 — Would I really need to do:[/QUOTE]

No, I simply made that change because syntactically it appeared to be closer to what you're trying to do.

div's are used to enclose a body of text (similar to what people use <p> for)

span's are used to enclose smaller areas of text.
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorDec 18.2008 — So you're saying it would be better coding practice to use a span rather than a div, right?
Copy linkTweet thisAlerts:
@chazzyDec 18.2008 — So you're saying it would be better coding practice to use a span rather than a div, right?[/QUOTE]

Sure, [B]in my opinion,[/B] it would be a better approach.
Copy linkTweet thisAlerts:
@ayveghDec 18.2008 — As a rule, I use div's mostly, the occasional p, and when I want to apply a style to an inner portion of either of those, I use a span, as I'm pretty sure it was intended to be used. ?
Copy linkTweet thisAlerts:
@Joseph_WitchardauthorDec 18.2008 — Well, the reason I'm asking is because I want my layout to work well with browsers, but more specifically, mobile browsers (like on a PDA or phone). I had heard that good design with divs helped the page to be better in mobile browsers, so I just want to make sure I'm not messing myself up if I switch to using span.
Copy linkTweet thisAlerts:
@ayveghDec 18.2008 — Either is fine.

The key to successful design is keeping the design parameters away from the content, i.e. CSS files, and don't keep style information in the actual content; id and class everything instead.

But that's neither here nor there. ?

I don't mind providing design assistance- PM me. ?
Copy linkTweet thisAlerts:
@chazzyDec 19.2008 — I don't mind providing design assistance- PM me. ?[/QUOTE]

Or you can keep it on the forums...
Copy linkTweet thisAlerts:
@ayveghDec 19.2008 — Or you can keep it on the forums...[/QUOTE]
hehe I don't mean to offend anyone by taking it private, but I would've probably embarrassed myself if I publicly posted the response to Joseph's PM. ?
×

Success!

Help @Joseph_Witchard spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...