Jump to content

MySQL custom SELECT function fix up


MasterACE14

Recommended Posts

hey,

 

I have a function which I basically was using to SELECT 1 column from a table. But I have been using it alot more lately, in more and more pages, and it becomes tedious, and reduces page loading times. Basically, it is used just to SELECT the 1 column, I want to SELECT all of them( * ) , but the catch is, I want to put each column( $row['column'] ) into a variable with the column name as the suffix of the variable name so it looks like this: $player_column1 = $row['column1']; and have it do this for every single column using $player_ as the prefix for the variable name. I would also like to put in a $playerid variable into the function variables, and if it isn't set, then just use $_SESSION['playerid']

 

here's my function:

<?php
// Select fields from the user table
function player_table($select){
    $where = $_SESSION['playerid'];
    $sql = "SELECT `" . $select . "` FROM `" . what_table($select) . "` WHERE `id`='" . $where . "' LIMIT 1";
    $rs = @mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());
    while($row = mysql_fetch_assoc($rs)){
        if(isset($row[$select])){return $row[$select];}
    }
};

?>

 

Any help is greatly appreciated  :)

 

Regards ACE

Link to comment
https://forums.phpfreaks.com/topic/96245-mysql-custom-select-function-fix-up/
Share on other sites

I soughta understand lol....

 

here's what I got so far:

<?php
// Select fields from the user table
function player_table($select,$id=null){
    if(!isset($id)) { $id = $_SESSION['playerid']; }
    $sql = "SHOW COLUMNS FROM `" . what_table($select) . "` WHERE `id`='" . $id . "' LIMIT 1";
$rs = @mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());
    for(1 < $rs) { $player_{$column_name} = $row[$column_name]; }
};

 

but I'm getting this error:

Parse error: syntax error, unexpected ')', expecting ';' in /home/ace/public_html/conflictingforces/functions.php on line 81

 

line 81 is the blank line after };

 

any ideas?

ok, thanks, that errors gone now. But I'm now getting this one...

Query:

SHOW COLUMNS FROM `rank` WHERE `id`='' LIMIT 1

 

Error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `id`='' LIMIT 1' at line 1

 

my current code:

<?php
// Select fields from the user table
function player_table($table=null,$id=null){
    if(!isset($id)) { $id = $_SESSION['playerid']; }
if(!isset($table)) { $table = "cf_users"; }
    $sql = "SHOW COLUMNS FROM `$table` WHERE `id`='" . $id . "' LIMIT 1";
$rs = @mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());
$i = 1;
while($row = mysql_fetch_assoc($rs)){
    for($i < $row; $i++; ) { $player_{$column_name} = $row[$column_name]; }
}
}

 

how do I set $column_name though?

couldn't the same thing be done with SELECT * FROM ??? or do I need to use SHOW COLUMNS?

 

latest code....

<?php
// Select fields from the user table
function player_table($table=null,$id=null){
    if(!isset($id)) { $id = $_SESSION['playerid']; }
if(!isset($table)) { $table = "cf_users"; }
    $sql = "SELECT * FROM `$table` WHERE `id`='" . $id . "' LIMIT 1";
$rs = @mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());
$i = 1;
while($row = mysql_fetch_assoc($rs)){
    for($i < $row; $i++; ) { $player_{$row['']} = $row['']; }
}
}

 

latest error...

Query:

SELECT * FROM `rank` WHERE `id`='' LIMIT 1

 

Error:

Table 'ace_cf.rank' doesn't exist

 

I don't know how its selecting from rank ???

 

<?php
// User Information
player_table("cf_users");
player_table("cf_users2");

I'm getting the same error again, and I have no idea why....

 

Query:

SELECT * FROM 'rank' WHERE `id`='1' LIMIT 1

 

Error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''rank' WHERE `id`='1' LIMIT 1' at line 1

 

<?php

// Select fields from the user table
function player_table($table=null,$id=null){
    if(!isset($id)) { $id = $_SESSION['playerid']; }
if(!isset($table)) { $table = "cf_users"; }
    $sql = "SELECT * FROM '$table' WHERE `id`='$id' LIMIT 1";
$rs = @mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());
$row = mysql_fetch_assoc($rs);

    foreach ($row as $colname=>$value) {
	$player_{$colname} = $value;
}

}

?>

 

calling it like this...

<?php

// User Information
player_table("cf_users");
player_table("cf_users2");

?>

 

Any ideas are greatly appreciated!

I'm trying all soughts of things now and am getting no where. I'm really begging for help now  :-[ , please whoever can help, I really would appreciate it.

 

current error:

Warning: Missing argument 2 for player_table() in /home/ace/public_html/conflictingforces/functions.php on line 53

Query:

SELECT * FROM `rank` WHERE `id`='' LIMIT 1

 

Error:

Table 'ace_cf.rank' doesn't exist

 

current code:

<?php
// Select fields from the user table
function player_table($table,$id){
    $sql = "SELECT * FROM `$table` WHERE `id`='$id' LIMIT 1";
$rs = @mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());
$row = mysql_fetch_array($rs);

    foreach ($row as $colname=>$value) {
	$player_{$colname} = $value;
	return $player_{$colname};
}

}

?>

 

please help if you can  :'(

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.