未加星标

PHP and MySQL Basics II - Case Sense

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏
Last time

we set up a connection from aphp program to a mysql server. This time we will progress a little further in that direction.

Query

Data is asked for from the MySQL server by using a query written in a language named Structured Query Language (SQL). Now that we have a connection open to the server, we can pass out request to the server.

Manual Labor The PHP Manual is wonderful 99% of time. If you take a peek at the page for mysqli::query

there is a great example of a simple query. Many of learned to program by copying/pasting from books/manuals and this is a great us of the examples in the PHP manual. Except it may not work for you.

MySQL is usually case SeNsATiVe, so 'A' may not be the same thing as 'a'. But this is dependent to some extent on your operating system where 'A' = 'a'. I was using the example from the manual and ... it did not work.

What Happened Here is an excerpt of the code, somewhat cut down: <?php $mysqli = new mysqli("localhost", "user", "secret", "world_x"); /* check connection */ if ($mysqli->connect_errno) { printf("Connect failed: %s\n", $mysqli->connect_error); exit(); } $mysqli->close(); ?>

Run the program and ... nothing.

So What Happened? What happened is a subtle problem that novices will smack into very hard. Take a look at this section of the example. /* Select queries return a resultset */ if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) { printf("Select returned %d rows.\n", $result->num_rows); /* free result set */ $result->close(); }

If you try the query SELECT Name FROM City LIMIT 10; with the MySQL command line client program you will get the answer. And the answer is:

mysql> SELECT Name FROM City LIMIT 10; ERROR 1146 (42S02): Table 'world_x.City' doesn't exist mysql>

I am using the new world _x example database where the city is NOT capitalized instead of the old world database where it is! This lesson can be summed as check you schema/table/column names for case sensitivity. Except that there is another problem here.

In the real world occasional the database/table/column that you carefully double checked was spelled correctly and with the proper case sensitivity will go away. It may have been renamed, deleted, munged, or what have you. What is needed is a way to check to see if there was an error if the query can not run.

Lets change the code slightly:

/* Select queries return a resultset */ if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) { printf("Select returned %d rows.\n", $result->num_rows); /* free result set */ $result->close(); } else { printf("Query failed: %s\n", $mysqli->error); } Always Check for Return Codes

By simply adding about 40 characters, the reliability of the program shoots up immensely AND we get an exact answer if what went wrong.

Query failed: Table 'world_x.City' doesn't exist Same error as when we tried by query by hand. But now our code can handle this issue. We could even try to catch the error, send a note via a message queue to the operations staff about the nature of the problem, and possible limp along until things are resolved. Or we could just call exit()

When you are offered a return code be sure to check it. Yes, it may be over kill in simple examples. But the payoff comes when things go bad and you are scrambling to find out eleven months from now why your program is suddenly not working.

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: SQLMySQLPHP
分页:12
转载请注明
本文标题:PHP and MySQL Basics II - Case Sense
本站链接:http://www.codesec.net/view/524212.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(32)