Jump to content

[SOLVED] MySQL - Checking if a Column is in table 1 or table 2


MasterACE14

Recommended Posts

Evening PHP Freaks,

 

I am making a basic function which checks whether a column is in table 1, or table 2. All is going good except for the last part. I'm stuck at the finish line lol.

 

Here is what I got, and I would appreciate it if someone would be able to do the rest of it, I'm just lost now.  :-\

<?php
// Check if the column in the database is in 1 table or another
function wheres_column($column) {
// Your two queries...
$first = "SHOW COLUMNS * FROM `cf_users`";
$second = "SHOW COLUMNS * FROM `cf_users2`";
// Start query one
$result = mysql_query($first);
// Debug...
if(!$result){
    die('There was an error.');
}
// Start loop
while($row = mysql_fetch_assoc($result))
{
if(
}

};
?>

 

Regards ACE

here's a general purpose column name finder

<?php
$host = '****';
$usr = '****';
$pwd = '****';
$dbname = 'test';

mysql_connect ($host,$usr,$pwd);
mysql_select_db($dbname);

if (isset($_GET['column']))
{
    $ta = wherecolumn($_GET['column']);
    echo '<pre>', print_r($ta, true), '</pre>';
}

function wherecolumn($column)
{
    $sql = "SELECT C.TABLE_NAME 
            FROM information_schema.`COLUMNS` C
            WHERE C.TABLE_SCHEMA=DATABASE() 
            AND C.COLUMN_NAME='$column'";
    $tables = array();
    $res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>");
    while ($row = mysql_fetch_row($res))
    {
        $tables[] = $row[0];
    }
    return $tables;
}
?>
<form>
Column <input type="text" name="column">
<input type="submit" name="sub" value="Find"> 
</form>

ok, here's the latest script, and I got a parse error:

<?php
// Check if the column in the database is in 1 table or another
function what_table($column) {
// Your two queries...
$first = "SHOW COLUMNS FROM `cf_users`";
$second = "SHOW COLUMNS FROM `cf_users2`";
// Start query one
$result = mysql_query($first);
// Debug...
if(!$result){
    die('There was an error.');
}
// Start loop
while($row = mysql_fetch_assoc($result))
{
if($row[$column] == $column) { $table = "cf_users" } // this is line 40
else { $table = "cf_users2" }
}

return $table;

};
?>

 

Parse error: syntax error, unexpected '}' in /home/ace/public_html/conflictingforces/functions.php on

C'mon bro... you're good enough at PHP to fix a parse error... = /

 

if($row[$column] == $column) { $table = "cf_users"; }

 

And I tell you again, better formatting on your part will make finding rogue or missing delimiters much easier.

 

PhREEEk

how in hell did I miss that  :-\

 

Thanks guys.

 

EDIT: Well the function is working now, except it works for every page except for the one I intended it for :-\

        I'm back to the Error "cf_users.supplydrop column doesnt exist" again. Is their a way to make it so I can make a if statement, and if the person is on a certain page, It will change what is in $table name?

 

The only problem I can see with this is. My pages are all include_once() in my index.php page, so my url's are like:

index.php?page=ability

 

And I don't thinking the GLobal Variables can pick up the ? and beyond.

 

any ideas?

not quite...

I'm back to the Error "cf_users.supplydrop column doesnt exist" again. Is their a way to make it so I can make a if statement, and if the person is on a certain page, It will change what is in $table name?

 

The only problem I can see with this is. My pages are all include_once() in my index.php page, so my url's are like:

index.php?page=ability

 

And I don't thinking the GLobal Variables can pick up the ? and beyond.

 

any ideas?

The only problem I can see with this is. My pages are all include_once() in my index.php page, so my url's are like:

index.php?page=ability

And I don't thinking the GLobal Variables can pick up the ? and beyond.

any ideas?

I don't exactly understand you but code like this will pick the page name and all passed value after ? too.

<?php 
$QueryString="";
foreach ($_GET as $key => $value)
{ 
$value = urlencode(stripslashes($value));
if($QueryString!="")
$QueryString .="&";

$QueryString .= "$key=$value";
}

$pageName=basename($_SERVER['PHP_SELF'] );

$page =$pageName."?".$QueryString;
echo $page; // will show the full page 
echo $QueryString; // will show after ? 
?> 

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.