8 tips developed by PHP and MySQL

zhaozj2021-02-17  58

Source: www.freeelamp.com

Lamp architecture website, I used to pay more attention to installation / configuration, and the relative development is relatively small, because I have fewed development. The original text of this article is from:

Published on the o'reilly network (http://www.oreillynet.com/)

http://www.oreillynet.com/pub/a/onlamp/2002/04/04/webdb.html

After reading, it is quite inspired. Some problems encountered in the previous development, the blades are solved. So translate out and share it.

1. Use of PHP

When operating the database, use the associative array (Associative-indexed arrays) is very helpful. Here we look at an array traversal of a basic digital format:

$ TEMP [0] = "richmond";

$ TEMP [1] = "TiGERS";

$ TEMP [2] = "premiers";

For ($ x = 0; $ x

{

Echo $ TEMP [$ x];

echo ""

}

?>

However, another way to save the code is:

$ TEMP = Array ("Richmond", "TIGERS", "premiers");

Foreach ($ TEMP AS $ ELEMENT)

Echo "$ Element";

?>

Foreach can also output text subscripts:

$ TEMP = Array ("Club" => "richmond",

"nickname" => "tigers",

"AIM" => "premier");

Foreach ($ TEMP AS $ Key => $ VALUE)

Echo "$ key: $ value";

?>

Approximately 50 functions for processing arrays are described in the PHP manual.

2. Add variables in the PHP string

This is very simple:

$ TEMP = "Hello"

Echo "$ TEMP World";

?>

However, it should be noted that although there is no error below:

$ TEMP = Array ("One" => 1, "Two" => 2);

// Output :: The first Element IS 1

Echo "The First Element IS $ TEMP [One]."

?>

However, if the Echo statement is not caused by double quotes, it is necessary to report an error. Therefore, it is recommended to use the curne brackets:

$ TEMP = Array ("One" => 1, "Two" => 2);

Echo "The first element is {$ TEMP [" one "]}."

?>

3. Adopt associative array access query results

Look at the example below:

$ connection = mysql_connect ("localhost", "albert", "shhh"); mysql_select_db ("WINESTORE", $ Connection);

$ result = mysql_query ("Select Cust_ID, Surname,

Firstname from Customer, $ Connection;

While ($ row = mysql_fetch_array ($ result))

{

Echo "ID: / t {$ row [" Cust_ID "]} / n";

Echo "Surname / T {$ ROW [" Surname "]} / n";

Echo "First Name: / t {$ ROW [" firstname "]} / n / n";

}

?>

Function mysql_fetch_array () puts a line of query results into an array, can be referenced in two ways, such as CUST_ID, can be used in both ways: $ ROW ["Cust_ID"] or $ ROW [0]. Obviously, the former readability is much better than the latter.

In the multi-table chart, if the two column names are the same, it is best to separate them:

Select Winery.name as WNAME,

Region.name as RNAME,

From Winery, Region

WHERE WINERY.REGON_ID = region.region_id;

The column name is: $ ROW ["WNAME"] and $ ROW ["RNAME"].

In the case of specifying the table name and column name, only the column name:

Select Winery.Region_ID

From Winery

The column name is: $ ROW ["Region_ID"].

The reference to the aggregation function is the reference name:

SELECT Count (*)

From customer;

The column name is: $ ROW ["count (*)"].

4. Pay attention to common PHP bugs

Common PHP error correction is:

No page rendered by the web browser when more is expected

A pop-up dialog stating what the "Document Contains No Data"

A Partial Page When More Is Exected

Most of these situations are not the logic of scripts, but the bugs of bugs present in HTML, the HTML BUG generated by the script. For example, a closed tag similar to , , is missing, and the page cannot be refreshed. The way to solve this problem is to view the source code of HTML.

For complex, you cannot find out the page, you can analyze it through the W3C page checkpoint http://validator.w3.org/.

If there is no variable, or the variable definition error will make the program be quirky. For example, the following dead cycle:

For ($ counter = 0; $ counter <10; $ counter )

Myfunction ();

?>

Variable $ Counter is increasing, and $ counter is less than 10. Such errors are generally found by setting higher error reporting levels:

Error_Reporting (E_ALL);

For ($ counter = 0; $ counter <10; $ counter )

Myfunction ();

?>

5. Use the header () function to handle single part query

In many web database applications, some features often let users click on a connection, continue to stay in the current page, such a job I call it "single part query".

Below is a script called calling.php:

"- // W3C // DTD HTML 4.0 Transitional // En"

"http://www.w3.org/tr/html4/loose.dtd">

Calling page example </ title></p> <p></ hEAD></p> <p><body></p> <p><a href="action.php"> click here! </A></p> <p></ body></p> <p></ html></p> <p>When the user clicks on the above connection, you will call an action.php. Below is the source code of Action.php:</p> <p><? PHP</p> <p>// Database function</p> <p>/ / Redirection</p> <p>Header ("Location: $ http_referer");</p> <p>EXIT;</p> <p>?></p> <p>There are two common errors here to remind:</p> <p>When you call the header () function, you want to include an exit statement to stop the script, otherwise the subsequent script may output before the header is sent.</p> <p>Header () Function is a common error:</p> <p>Warning: Cannot Add Header Information - Headers Already SENT ...</p> <p>The header () function can only be called before the HTML output, so you need to check the blank, space, and the like that may exist in front of PHP.</p> <p>6. Reload problem and its resolution</p> <p>When I used the PHP program, I often touched the page refresh, and the database was more handled more.</p> <p>Let's look at AddCust.php:</p> <p><? PHP</p> <p>$ query = "Insert Into Customer</p> <p>SET SURNAME = $ Surname,</p> <p>Firstname = $ firstname ";</p> <p>$ connect = mysql_connect ("localhost", "fred", "shhh");</p> <p>MySQL_SELECT_DB ("WINESTORE", $ Connection;</p> <p>$ result = mysql_query ($ query, $ connection);</p> <p>?></p> <p><! Doctype HTML PUBLIC</p> <p>"- // W3C // DTD HTML 4.0 Transitional // En"</p> <p>"http://www.w3.org/tr/html4/loose.dtd"></p> <p><html></p> <p><HEAD></p> <p><title> Customer INSERT </ TITLE></p> <p></ hEAD></p> <p><body></p> <p>I'VE INSERTED The Customer for you.</p> <p></ body></p> <p></ html>?></p> <p>Suppose we use this program with the following connection:</p> <p>http://www.freeelamp.com/addcust.php?srainame=smith&firstname=fred</p> <p>If this request is only submitted once, OK, there will be no problem, but if you are refreshed multiple times, you will have multiple records.</p> <p>This problem can be resolved through the header () function: Below is the new version of AddCust.php:</p> <p><? PHP</p> <p>$ query = "Insert Into Customer</p> <p>SET SURNAME = $ Surname,</p> <p>Firstname = $ firstname ";</p> <p>$ connect = mysql_connect ("localhost", "fred", "shhh");</p> <p>MySQL_SELECT_DB ("WINESTORE", $ Connection;</p> <p>$ result = mysql_query ($ query, $ connection);</p> <p>Header ("Location: Cust_Receipt.php");</p> <p>?></p> <p>This script redirects the browser to a new page: CUST_RECEIPT.PHP:</p> <p><! Doctype HTML PUBLIC</p> <p>"- // W3C // DTD HTML 4.0 Transitional // En"</p> <p>"http://www.w3.org/tr/html4/loose.dtd"></p> <p><html></p> <p><HEAD></p> <p><title> Customer INSERT </ TITLE></p> <p></ hEAD></p> <p><body></p> <p>I'VE INSERTED The Customer for you.</p> <p></ body></p> <p></ html></p> <p>In this way, there is no side effects that the original page continues to refresh.</p> <p>7. Use lock mechanism to improve application performance</p> <p>If we have to run a report urgently, we can write a lock to the table, prevent others reading and writing, to improve the processing speed of this table.</p> <p>8. Develop fast scripts with mysql_unbuffered_query ()</p> <p>This function can be used to replace the mysql_query () function, the main difference is that mysql_unbuffered_query () is returned immediately after the query, does not need to wait or lock the database.</p> <p>However, the number of rows returned cannot be checked with a mysql_num_rows () function, because the result set size is unknown.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-29741.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="29741" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.077</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'U1D5tPnHi3hEcdZp5f38kPpb8AK5lZpE9O19x1Ik3KSs4Vf_2BQN1T84VoO53z23c1XoeC0_2F6XGdnCTm28aD6opg_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>