How to convert the SQL collection to the desired string of 100 points!

Source: Internet
Author: User
Tags rtrim
How to convert the SQL collection to the desired string of 100 points! Query ('select infotags, id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE "% I Love You % "')

I want to make the id from the preceding query into a string.
For example, five id values are output: 1, 2, 3, 4, and 5.
Now I want to convert these five values into such strings.
(1, 2, 3, 4, 5)
How can this problem be solved?

I want to try the following method, but I cannot.
$ Result = query ('select infotags, id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE "% I Love You % "');
$ FoodsPic = array ();
While ($ row = mysql_fetch_array ($ result )){
$ FoodsPic [] = $ row;
}

Print_r ($ foodsPic );
The above method not only gets all the IDs, but also other useless ones. I only want the set of IDs, and through processing, construct these IDs into strings (1, 2, 3, 4, 5)
I don't know how to improve it,


Reply to discussion (solution)

$ SQ = "SELECT infotags, id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE '% I Love You %'"; $ result = mysqli_query ($ link, $ SQ); $ idStr = ''; if (mysqli_num_rows ($ result) {$ data = mysqli_fetch_all ($ result, MYSQL_ASSOC); foreach ($ data as $ value) {if (isset ($ value ['id']) $ idStr. = $ value ['id'];} $ idStr = rtrim ($ idStr, ','); echo $ idStr ;}

$ SQ = "SELECT infotags, id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE '% I Love You %'"; $ result = mysqli_query ($ link, $ SQ); $ idStr = ''; if (mysqli_num_rows ($ result) {$ data = mysqli_fetch_all ($ result, MYSQL_ASSOC); foreach ($ data as $ value) {if (isset ($ value ['id']) $ idStr. = $ value ['id'];} $ idStr = rtrim ($ idStr, ','); echo $ idStr ;}


I still cannot write it, but I feel that you have the correct idea, but I will not benefit one of my database operation classes.
For example, if you use mysqli_fetch_all
I checked the information and it seems that this function has been banned.
In addition, my system has a database function operation class.
 SQL = mysql_query ($ query, return_dblink ($ query) or die ($ ecms_config ['DB'] ['showerror'] = 1? Mysql_error ().'
'. Str_replace ($ GLOBALS ['dbtbpre'], '*** _', $ query): 'dberror '); return $ this-> SQL;} // execute mysql_query () statement 2 function query1 ($ query) {$ this-> SQL = mysql_query ($ query, return_dblink ($ query); return $ this-> SQL ;} // execute the mysql_query () statement (select database USE) function usequery ($ query) {global $ ecms_config; $ this-> SQL = mysql_query ($ query, $ GLOBALS ['link']) or die ($ ecms_config ['DB'] ['showerror'] = 1? Mysql_error ().'
'. Str_replace ($ GLOBALS ['dbtbpre'], '*** _', $ query): 'dberror '); if ($ GLOBALS ['linkrd']) {mysql_query ($ query, $ GLOBALS ['linkrd ']);} return $ this-> SQL;} // execute the mysql_query () statement (operate the database) function updatesql ($ query) {global $ ecms_config; $ this-> SQL = mysql_query ($ query, return_dblink ($ query )) or die ($ ecms_config ['DB'] ['showerror'] = 1? Mysql_error ().'
'. Str_replace ($ GLOBALS ['dbtbpre'], '*** _', $ query): 'dberror '); return $ this-> SQL;} // execute mysql_fetch_array () function fetch ($ SQL) // the parameter of this method is $ SQL, which is the SQL statement execution result {$ this-> r = mysql_fetch_array ($ SQL ); return $ this-> r;} // execute fetchone (mysql_fetch_array () // The difference between this method and fetch () is: 1. the parameter of this method is $ query, which is an SQL statement // 2. this method is used for while (). The for () database pointer does not automatically move down, while fetch () can automatically move down. Function fetch ($ query) {$ this-> SQL = $ this-> query ($ query); $ this-> r = mysql_fetch_array ($ this-> SQL ); return $ this-> r;} // execute mysql_num_rows () function num ($ query) // The parameter $ query is an SQL statement {$ this-> SQL = $ this-> query ($ query ); $ this-> num = mysql_num_rows ($ this-> SQL); return $ this-> num;} // execute numone (mysql_num_rows () // this method corresponds to num () the difference is: 1. the parameter of this method is $ SQL, which is the execution result of the SQL statement. Function num1 ($ SQL) {$ this-> num = mysql_num_rows ($ SQL); return $ this-> num;} // execute numone (mysql_num_rows ()) // function gettotal ($ query) {$ this-> r = $ this-> fetch1 ($ query ); return $ this-> r ['total'];} // execute free (mysql_result_free () // the parameter of this method is $ SQL, which is the execution result of the SQL statement. Use function free ($ SQL) {mysql_free_result ($ SQL) ;}// execute seek (mysql_data_seek () only when mysql_fetch_array is used ()) // the parameter of this method is $ SQL, which is the execution result of the SQL statement. $ pit is the offset number of the execution pointer function seek ($ SQL, $ pit) {mysql_data_seek ($ SQL, $ pit);} // execution id (mysql_insert_id () function lastid () // Obtain the last mysql database id {$ this-> id = mysql_insert_id ($ GLOBALS ['link']); if ($ this-> id <0) {$ this-> id = $ this-> gettotal ('select last_insert_id () as total');} return $ this-> id;} // return the Shadow Response count (mysql_affected_rows () function affectnum () // number of affected records after obtaining the operation data table {return mysql_affected_rows ($ GLOBALS ['link']) ;}}?>

Can I use the method here? for example, if
Instantiate this class first
$ Empire = news mysqlquery ();
Then, how can I rewrite the above with the $ empire-> method,

$ SQ = "SELECT infotags, id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE '% I Love You %'"; $ result = mysqli_query ($ link, $ SQ); $ idStr = ''; if (mysqli_num_rows ($ result) {$ data = mysqli_fetch_all ($ result, MYSQL_ASSOC); foreach ($ data as $ value) {if (isset ($ value ['id']) $ idStr. = $ value ['id'];} $ idStr = rtrim ($ idStr, ','); echo $ idStr ;}


 Query ('select infotags, id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE "% spot promotions %" '); // query the infotags field in the sub-table $ idStr = ''; if ($ empire-> num ($ sqld) {$ data = $ empire-> fetch ($ sqld); foreach ($ data as $ key => $ value) {if (isset ($ value ['id']) $ idStr. = $ value ['id'] ;}$ idStr = rtrim ($ idStr, ','); echo $ idStr ;}?>

I changed it, but it failed. I don't know what's wrong.

$ SQ = "SELECT infotags, id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE '% I Love You %'"; $ result = mysqli_query ($ link, $ SQ); $ idStr = ''; if (mysqli_num_rows ($ result) {$ data = mysqli_fetch_all ($ result, MYSQL_ASSOC); foreach ($ data as $ value) {if (isset ($ value ['id']) $ idStr. = $ value ['id'];} $ idStr = rtrim ($ idStr, ','); echo $ idStr ;}


I wrote the result, but it was a mess of numbers.
$ Sqld = $ empire-> query ('select infotags, id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE "% I Love You %" '); // query the infotags field in the subtable
$ Id1_= array ();
While ($ bqrd = $ empire-> fetch ($ sqld )){
If ($ bqrd [id])
$ IdStr. = $ bqrd [id];
// $ IdStr = rtrim ($ idStr ,',');
Echo $ idStr;

// The Code misled you to modify $ idStr. = $ bqrd [id]. ',';} $ idStr = rtrim ($ idStr ,',')

In addition, mysqli_fetch_all is the mysql database driver of PHP and has not been eliminated, but the server must enable this support. some server environments may not support such as sea... generally, servers compiled by yourself can easily get this support.

In addition, mysqli_fetch_all is the mysql database driver of PHP and has not been eliminated, but the server must enable this support. some server environments may not support such as sea... generally, servers compiled by yourself can easily get this support.



Can this code be changed to the while format without foreach? how can this code be changed?
$ SQ = "SELECT infotags, id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE '% I Love You % '";
$ Result = mysqli_query ($ link, $ SQ );
$ IdStr = '';
If (mysqli_num_rows ($ result )){
$ Data = mysqli_fetch_all ($ result, MYSQL_ASSOC );
Foreach ($ data as $ value ){
If (isset ($ value ['id'])
$ IdStr. = $ value ['id'];
}

$ IdStr = rtrim ($ idStr ,',');
Echo $ idStr;
}

$ SQ = "SELECT infotags, id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE '% I Love You %'"; $ result = mysqli_query ($ link, $ SQ); $ idStr = ''; if (mysqli_num_rows ($ result) {$ data = mysqli_fetch_all ($ result, MYSQL_ASSOC); foreach ($ data as $ value) {if (isset ($ value ['id']) $ idStr. = $ value ['id'];} $ idStr = rtrim ($ idStr, ','); echo $ idStr ;}



Can you change this to the while format? after the change, you can add points.

// Hava a try SELECT GROUP_CONCAT (infotags SEPARATOR ",") as infotags, id FROM hd_ecms_shop_data_1 WHERE hd_ecms_shop_data_1.infotags LIKE '% I Love You %' GROUP BY infotags

The id array can be queried and then processed using implode. why do I have to use SQL queries?

The id array can be queried and then processed using implode. why do I have to use SQL queries?


How to write, no code, no truth

// Sao Nian, this is a basic skill problem. while ($ row = mysql_fetch_array ($ result) {$ id [] = $ row ['id'];} echo join (",", $ id );

// Sao Nian, this is a basic skill problem. while ($ row = mysql_fetch_array ($ result) {$ id [] = $ row ['id'];} echo join (",", $ id );


Shangxian, please consult
Why
Echo $ row [id]; // no output or error
// Echo join (",", $ id); // you can print the value.

// Sao Nian, this is a basic skill problem. while ($ row = mysql_fetch_array ($ result) {$ id [] = $ row ['id'];} echo join (",", $ id );


I made a mistake, and my mind was totally confused,
Array ([0] => 9 [1] => 38 [2] => 39 [3] => 40 [4] => 36 [5] => 37)

// Sao Nian, this is a basic skill problem. while ($ row = mysql_fetch_array ($ result) {$ id [] = $ row ['id'];} echo join (",", $ id );


It's a good way to go to Xianxian. it's a plus. I hope you will have a calendar in the future. don't let us down on your expectations! Click here!

// Sao Nian, this is a basic skill problem. while ($ row = mysql_fetch_array ($ result) {$ id [] = $ row ['id'];} echo join (",", $ id );


Well, it's a good way to go to Xianxian. it's a plus!

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.