Answer the question
In order to leave comments, you need to log in
How would you speed up this php code?
Hello
, I'm just starting my journey, so don't judge strictly, I'm making different sites for learning, here's what I sketched today:
<?
header("Content-Type: text/html; charset=UTF-8");
$db = mysql_connect("localhost","db79","password");
mysql_select_db("d_lx",$db);
mysql_query("SET NAMES utf8");
$result = mysql_query("SELECT * FROM videoByMe", $db);
$num_rows = mysql_num_rows($result);
$idVideo1 = rand (3,$num_rows);
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<html lang="ro">
<title>All Video</title>
<link rel="stylesheet" href="http://site.com/style/bats.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://www.site.ru/player6/jwplayer.js"></script>
</head>
<body>
<div class="header">
<div class="inner-header">
<a href="#"><img src="http://site.com/image/logo.png" style="width: 70px; margin-bottom: 5px; float: left;">
<a href="#" class="menu">
Home
</a>
<div class="vertical-line"></div>
<a href="#" class="menu">
Reg
</a>
<div class="vertical-line"></div>
<a href="#" class="menu">
Add video
</a>
</div>
</div>
<?
for ($i = 3; $i <= $num_rows; $i++) {
$resultat = mysql_query("SELECT * FROM `videoByMe` WHERE `id` = $i",$db);
$data = mysql_fetch_array($resultat);
$nameVideo = $data['DenumireaVideo'];
$IdvideoMIX = $data['VideoMixID'];
$linkToImage = $data['linkPoza'];
echo '<a href="index.php?id='.$i.'" style="color: #333; text-decoration: none;">
<div class="videos">
<img src="'.$linkToImage.'" style="width: 285px; height: 150px;">
<div style="font-size: 16px; margin-top: 5px; font-weight: bolder;">'.$nameVideo.'</div>
</div>
</a>';
}
mysql_close($db);
?>
</div>
</div>
</div>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
Read something on the topic of separation of responsibility
. Also, if you think that "the page is loading slowly" - you need to profile it.
1. At the browser level - just look in the debugger what exactly is loading slowly. Perhaps the pictures themselves are slowly downloading.
2. At the page level - perhaps the server is far away. Or maybe the page is slow to generate.
3. At the PHP level - there are separate profilers, you can start stupidly by logging the execution time of individual pieces of code. We end with specialized tools like blackfire.io.
Don't waste your youth on mysql_ functions. Not only are they terrible, but they are also outdated.
As long as you don't need complex database work, you can live with, for example, SafeMySQL . This, of course, is not a panacea and in the long term is generally a dead end, but compared to the spaghetti of the functions - the joy and well-being of the air.
Do not mix code and layout. Sooner or later you will still reach their separation, better sooner. All PHP first: prepare all the data that will be output. Then the page with minimal inserts of already prepared data went. The maximum allowed logic is the output of data in a loop.
If you have heavy external links, do lazy loading. At least elementary - display the page with the entire layout, but without specifics, and at the end write javascript that assigns specific src to specific pictures. It's easy, really. However, if for this you get acquainted with at least the well-known JQuery, everything will become even easier ...
this code is so fast. but such a mess is very difficult to maintain on projects. so use SOLID principles and apply design patterns wisely
It’s definitely not worth downloading all the videos from the table at once, it’s necessary page by page or with endless “Show more”, and most importantly, why make a SELECT query for each video, just one is enough, and then just iterate (loop) over all rows.
This is more of a problem in the database, debug.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question