SA-MP Forums Archive
SQL Select - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: SQL Select (/showthread.php?tid=596551)



SQL Select - TwinkiDaBoss - 19.12.2015

Alright so its quite simple, but I dont know how to do it.
What im trying to achieve is simple, read the SQL and print all the results, I want to read all rows that match the ID
PHP код:
new query[512];
    
mysql_format(mysqlquerysizeof(query),"SELECT `ID`,`Model` FROM `furniture` WHERE `MasterID`=%i",AccInfo[playerid][HouseOwner]);
     
mysql_tquery(mysqlquery);
     print(
query); 



Re: SQL Select - Kaperstone - 19.12.2015

Although I recommend phpMyAdmin to browse the MySQL database (while on development, having it hosted on production can be a security risk, once (& if) breached
PHP код:
forward OnHouseStripHouseData(playerid);
 
public 
OnPlayerConnect(playerid) {
    new 
query[512];
    
GetPlayerName(playeridpnamesizeof(pname));
  
mysql_format(mysqlquerysizeof(query),"SELECT `ID`,`Model` FROM `furniture` WHERE `MasterID`=%i",AccInfo[playerid][HouseOwner]); 
  
mysql_tquery(mysqlquery"OnHouseStripHouseData"); 
    return 
1;
}
 
public 
OnHouseStripHouseData() {
  new 
      
dest[128], // we will store the field data here 
      
rows=cache_num_rows(), // get number of rows in the result
      
fields=cache_get_field_count(); // get the number of fields in the result
  // Loop through all the existing rows in the result
  
for(new x=0;x<rows;x++) {
    
// Loop through all the fields on the specific row
    
for(new y=0;y<fields;y++) {
      
// Get data from row X and field number Y from left to right
      
cache_get_row(xydest);
      
// Print it !
      
printf("| %s"dest);
    } 
  }
  return 
1;

inb4, untested.
Don't have anything to test with, its improvised.


Re: SQL Select - TwinkiDaBoss - 19.12.2015

Quote:
Originally Posted by Kaperstone
Посмотреть сообщение
Although I recommend phpMyAdmin to browse the MySQL database (while on development, having it hosted on production can be a security risk, once (& if) breached
PHP код:
forward OnHouseStripHouseData(playerid);
 
public 
OnPlayerConnect(playerid) {
    new 
query[512];
    
GetPlayerName(playeridpnamesizeof(pname));
  
mysql_format(mysqlquerysizeof(query),"SELECT `ID`,`Model` FROM `furniture` WHERE `MasterID`=%i",AccInfo[playerid][HouseOwner]); 
  
mysql_tquery(mysqlquery"OnHouseStripHouseData"); 
    return 
1;
}
 
public 
OnHouseStripHouseData() {
  new 
      
dest[128], // we will store the field data here 
      
rows=cache_num_rows(), // get number of rows in the result
      
fields=cache_get_field_count(); // get the number of fields in the result
  // Loop through all the existing rows in the result
  
for(new x=0;x<rows;x++) {
    
// Loop through all the fields on the specific row
    
for(new y=0;y<fields;y++) {
      
// Get data from row X and field number Y from left to right
      
cache_get_row(xydest);
      
// Print it !
      
printf("| %s"dest);
    } 
  }
  return 
1;

inb4, untested.
Don't have anything to test with, its improvised.
Not results at all. Nothing printed
PHP код:
YCMD:test(playerid,params[],help) {
    new 
query[512];
     
mysql_format(mysqlquerysizeof(query),"SELECT `ID`,`Model` FROM `furniture` WHERE `MasterID`=%i",AccInfo[playerid][HouseOwner]);
      
mysql_tquery(mysqlquery"OnHouseStripHouseData");
    return 
true;
}
forward OnHouseStripHouseData(playerid);
public 
OnHouseStripHouseData(playerid) {
      new
      
dest[128], // we will store the field data here
      
rows=cache_num_rows(), // get number of rows in the result
      
fields=cache_get_field_count(); // get the number of fields in the result
      
    
for(new x=0;x<rows;x++) {
        for(new 
y=0;y<fields;y++) {
              
cache_get_row(xydest);
              
printf("| %s"dest);
        }
     }
      return 
1;

EDIT: Nevermind, the problem was in my format, works!! thanks!


Re: SQL Select - TwinkiDaBoss - 19.12.2015

There is 1 problem tho, sorry for double post, but it prints the stuff this way
PHP код:
[20:09:15] | 6
[20:09:15] | 2290
[20:09:15] | 7
[20:09:15] | 2290
[20:09:15] | 8
[20:09:15] | 2290
[20:09:15] | 9
[20:09:15] | 2290
[20:09:15] | 10
[20:09:15] | 2290
[20:09:15] | 11
[20:09:15] | 2290
[20:09:15] | 12
[20:09:15] | 2290 
I want to print 2 variables in the same line. Ie:

2290 || 7


Re: SQL Select - Kaperstone - 20.12.2015

use strcat to push each field to the array, after the field loop ends (which means - end of row) print the array.


Re: SQL Select - TwinkiDaBoss - 20.12.2015

Ah, mind just showing me how? I know what you mean but im not sure how to push them multiple into 1 line


Re: SQL Select - TwinkiDaBoss - 22.12.2015

Bump
Quote:

Ah, mind just showing me how? I know what you mean but im not sure how to push them multiple into 1 line




Re: SQL Select - Jefff - 22.12.2015

pawn Код:
forward OnHouseStripHouseData();
public OnHouseStripHouseData()
{
    new id,model,rows = cache_num_rows(); // get number of rows in the result

    for(new i=0; i < rows; i++)
    {
        id = cache_get_field_content_int(i, "ID");
        model = cache_get_field_content_int(i, "Model");
        printf("%d|%d", id, model);
    }
    return 1;
}



Re: SQL Select - TwinkiDaBoss - 23.12.2015

Quote:
Originally Posted by Jefff
Посмотреть сообщение
pawn Код:
forward OnHouseStripHouseData();
public OnHouseStripHouseData()
{
    new id,model,rows = cache_num_rows(); // get number of rows in the result

    for(new i=0; i < rows; i++)
    {
        id = cache_get_field_content_int(i, "ID");
        model = cache_get_field_content_int(i, "Model");
        printf("%d|%d", id, model);
    }
    return 1;
}
Thanks


Re: SQL Select - TwinkiDaBoss - 23.12.2015

There is 1 problem, it sorta creates an infinite loop. It keeps telling all players the furniture ID after they buy one afterwards.