Db help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
invision-killa
Forum Newbie
Posts: 6
Joined: Mon Apr 09, 2007 2:42 pm

Db help

Post by invision-killa »

Hello Fellow Forum People, I'm kinda new to php/mysql world and well i made my own little site for my clan that i have on 5 different games but i dont want to duplicate the sites 5 different times is there any way i can make it where i could have a drop down menu that will select a database file like for something like


----Drop down menu----
Rainbow Six 3
Rainbow Six Lockdown
Rainbow Six Vegas 360
Rainbow Six Vegas PS3
Black Hawk Dwon Team Saber



Which will kinda select which database will be used see in my index.php

i have it where it selects db.php so how can i change that so it will come in effect the right way as well?

I hope you understand :? im not good at asking for help :?

Thank you
invision-killa
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Store your database names in an array, then pass that by way of a dropdown to your code:

Code: Select all

<?php
$servers = array(
    array('name' => 'dbname1', 'display' => 'Rainbow Six 3'),
    array('name' => 'dbname2', 'display' => 'Rainbow Six Lockdown'),
    array('name' => 'dbname3', 'display' => 'Rainbow Six Vegas 360'),
    array('name' => 'dbname4', 'display' => 'Rainbow Six Vegas PS3'),
    array('name' => 'dbname5', 'display' => 'Black Hawk Dwon Team Saber')
);

$selected_server = 0; // Or whatever default you want from the array indeces above
if (isset($_POST['server_choice']))
{
    $server_choice = intval($_POST['server_choice']);
    if (array_key_exists($server_choice, $servers))
    {
        $selected_server = $servers[$server_choice]['name'];
    }
}
else
{
    echo '<form id="server_chooser" method="post" action="' . basename(__FILE__) . '">';
    echo '<select name="server_choice">';
    for ($i = 0; $i < count($servers);   $i)
    {
        echo '<option value="' . $i . '">' . $servers[$i]['display'] . '</option>';
    }
    echo '</select>';
    echo '</form>';
}
?>
When you have a selected_server you can then pass it to your connection class.
invision-killa
Forum Newbie
Posts: 6
Joined: Mon Apr 09, 2007 2:42 pm

Post by invision-killa »

what do you mean connection class? and see im going to have like index.php which where they will be asked what game they play then when they select what game they play it will take them to index2.php and thats where all the scripts are basicly set at and thats where the database is connected at or what ever
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I am not completely understanding your question. What I posted was a way in which a user can select something that is fed by a database. When they select something from that list, it posts back to the page it is on and can be used to do whatever you want it to do.
invision-killa
Forum Newbie
Posts: 6
Joined: Mon Apr 09, 2007 2:42 pm

Post by invision-killa »

ok i couldnt really figure out your script so i made alittle of my own now here is what i got


index.php

Code: Select all

<html>
<form name="Members" method="post" action="index2.php">
          <select name="db" class="input-box">
          <option <?php echo $db=='db.php' ? 'selected' : ''?>>Lockdown</option>
          <option <?php echo $db=='db2.php' ? 'selected' : ''?>>Team Sabre</option>
          <option <?php echo $db=='db3.php' ? 'selected' : ''?>>Vegas PS3</option>
          <option <?php echo $db=='db4.php' ? 'selected' : ''?>>Vegas 360</option>
          <option <?php echo $db=='db5.php' ? 'selected' : ''?>>Lockdown Allied</option>
        </select>
		<input type="submit" name="Submit" value="Submit" class="submit-button">
</form>
</html>
This is whats in index2.php (now there is more in index2.php but it doesnt have anything to do with database)

Code: Select all

$db = $row['db'];

echo $row['db'];

This is what i get..

Code: Select all

Warning: mysql_query(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /home/www/diesquad.freehostia.com/header.php on line 35

Warning: mysql_query(): A link to the server could not be established in /home/www/diesquad.freehostia.com/header.php on line 35

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/www/diesquad.freehostia.com/header.php on line 38

Only thing thats getting me is the site comes up good and it shows a record of a news that i made which is in the database showing up so how can this be if its not connecting to the database?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You aren't showing us how the connection is made. Let us see that code so we can see the potential error in the code. Lines 30 to 40 would be particularly helpful.
invision-killa
Forum Newbie
Posts: 6
Joined: Mon Apr 09, 2007 2:42 pm

Post by invision-killa »

Code: Select all

<table width="100%" border="1" bordercolor="#666666" cellspacing="1" cellpadding="0">
                <tr> 
                  <td width="73%"> 
                    <?php

$sql = mysql_query("SELECT new FROM pm WHERE user_name = '".$_SESSION['user_name']."'");
	
$new = false; 
while ($row = mysql_fetch_array($sql)) { 
  
if (!$new && $row['new'] == '1') {
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

we're looking for where mysql_connect() is called.
invision-killa
Forum Newbie
Posts: 6
Joined: Mon Apr 09, 2007 2:42 pm

Post by invision-killa »

Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


is this what your talking about??

Code: Select all

<? 
/*  Database Information - Required!!  */
/* -- Configure the Variables Below --*/
$dbhost = '';
$dbuser = '';
$dbpasswd = '';
$database = '';

/* Database Stuff, do not modify below this line */

$connection = mysql_pconnect("$dbhost","$dbuser","$dbpasswd") 
	or die ("Couldn't connect to server.");
	
$db = mysql_select_db("$database", $connection)
	or die("Couldn't select database.");
?>

see it works just fine if i have the require 'db.php';


Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You are trying to pass a database name to that connection function, are you not?
invision-killa
Forum Newbie
Posts: 6
Joined: Mon Apr 09, 2007 2:42 pm

Post by invision-killa »

i think so :S lol
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If that is what you are trying to do then you need to set the $dbhost name (and related credentials) to the selected database before passing it to the connection function.
Post Reply