Php connects to mysql to query a piece of data

Source: Internet
Author: User
Php connection to mysql Query a piece of data php mysql Query

A table in the database has a primary key pmcode and other attributes.
Now I want to use pmcode to determine whether a user exists. if the user exists, the user will retrieve the information. if the user does not exist, the user will return "nonexistent" and then send the information in xml format. the code is as follows:
// Obtain the membership card number $ arry = explode ("+", $ form_Content) from the split message; $ memberid = $ arry [1]; // connect to the database $ link = mysql_connect (SAE_MYSQL_HOST_M. ':'. SAE_MYSQL_PORT, SAE_MYSQL_USER, SAE_MYSQL_PASS); if ($ link) {mysql_select_db (SAE_MYSQL_DB, $ link ); // query data $ result1 = mysql_query ("select * from numbers where pmcode = '". $ memberid. "';"); $ num_result = mysql_num_rows ($ result1); if ($ num_result = 1) {// $ row = $ result1-> fetch_assoc (); $ row = mysql_fetch_row ($ result1); $ result_str = "your member -- information is as follows: \ n card number: $ row [pmcode]. \ n name :. $ row [names]. \ n level :. $ row [4]. \ n points: $ a [5] "; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ result_str); echo $ resultStr; exit;} else {$ msgType = "text"; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, "-- select * from numbers where pmcode = '". $ memberid. "'"); echo $ resultStr; exit ;}} else {echo "database connection failed ". mysql_error;} mysql_close ($ link );}

However, even if the correct pmcode is entered, the desired data cannot be obtained.


Reply to discussion (solution)

If ($ num_result = 0) {// $ row = $ result1-> fetch_assoc (); $ row = mysql_fetch_row ($ result1 ); $ result_str = "your member -- information is as follows: \ n card number: $ row [pmcode]. \ n name :. $ row [names]. \ n level :. $ row [names]. \ n points: $ a [names] "; $ resultStr = sprintf ($ textTpl, $ fromUsername, $ toUsername, $ time, $ msgType, $ result_str); echo $ resultStr; exit;

It cannot be changed to = 0.

Echo $ memberid; what value do you get? can you execute the command to copy the SQL statement to the doscommand line to get the data?

Echo $ memberid; what value do you get? can you execute the command to copy the SQL statement to the doscommand line to get the data?
The correct value is obtained.

$ Num_result = mysql_num_rows ($ result1 );
Echo $ num_result; // output something

$ Num_result = mysql_num_rows ($ result1 );
Echo $ num_result; // output something
1

That's right. the code of your primary Post is okay. you should be able to enter if.

That's right. the code of your primary Post is okay. you should be able to enter if.
After a piece of data is taken out, how can we obtain the attributes and print the pmcode, names, and so on?

$ Row = mysql_fetch_row ($ result1 );
Print_r ($ row );

$ Row = mysql_fetch_row ($ result1 );
Print_r ($ row );
What if I only use the names attribute?

$ Row ['names']

$ Row ['names']

$ Link = mysql_connect (SAE_MYSQL_HOST_M. ':'. SAE_MYSQL_PORT, SAE_MYSQL_USER, SAE_MYSQL_PASS); if ($ link) {mysql_select_db (SAE_MYSQL_DB, $ link); $ memberid = "j1_1_1_b005 "; // query data $ result1 = mysql_query ("select * from numbers where pmcode = '". $ memberid. "'"); $ row = mysql_fetch_row ($ result1); $ num_result = mysql_num_rows ($ result1); if ($ num_result = 1) {$ resultStr = $ row ['names']; echo "fff ". $ resultStr; exit;} else {echo "no data found" ;}} else {echo "failed to connect to database ". mysql_error;} mysql_close ($ link );

Why only fff is output?

Print_r ($ row); // Paste the result

Array ([0] => July 0000b005 [1] =>? WHY ??? [2] => F [3] => [4] => [5] => 20121012 [6] => 20130430 [7] => Z [8] => JE11 [9] => 0 [10] => 0 [11] => 0 [12] => [13] =>)

Print_r ($ row); // Paste the result
Array ([0] => July 0000b005 [1] =>? WHY ??? [2] => F [3] => [4] => [5] => 20121012 [6] => 20130430 [7] => Z [8] => JE11 [9] => 0 [10] => 0 [11] => 0 [12] => [13] =>)

Oh, I made a mistake.
Echo $ row [1];

If there are garbled characters, add mysql_query ("set names utf8") to the query.

Change mysql_fetch_row to mysql_fetch_assoc... Then print the array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.