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
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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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  :'(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.