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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

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.