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!
I need to select the database, based on a variable. The variable is composed for two variables. One is chars ($front_db_name) and the other a integer ($topic_id).
I can't figure this out. I have tried several different methods (using mysql_select_db). Either the page doesn't show up or I get the message "No database selected" in my output. Later, I tried to use the the db name, not the variable, and it still wouldn't connect.
//----------------selects db based on topic id---------------------
$front_db_name = "username_x";
$use_db = "$front_db_name$topic_id";
$connect = mysql_connect("XXXXXX", "XXXXXX", "XXXXXX") or
die ("Check your server connection.");
//--------------make sure we're using the right database------------
$query = mysql_query("USE $use_db");
//-----------find max number of links in database--------------
$query = mysql_query("SELECT * FROM $linksinfo");
$num_of_links == 1;
while($row = mysql_fetch_array($query)) {
$searching_row = $rowї"entered"];
$num_of_links++;
}
I dont like the way I select the db and query it. I see a lot of other people's code and they do it differently. I'm rather new to this and learned this style from a book. Someone please show me a different way.
$front_db_name = "hostusername";
$use_db = "$front_db_name$topic_id";
$connect = mysql_connect("XXXXXX", "XXXXXX", "XXXXXX") or
die ("Cant connect " . mysql_error());
//----------------make sure we're using the right database---------------
$db_select = mysql_select_db($use_db, $connect);
if (!$db_select) {
die ("Cant use db " . mysql_error());
}
//---------------find max number of links in database----------------------
$query = mysql_query("SELECT * FROM $linksinfo");
$num_of_links == 1;
while($row = mysql_fetch_array($query)) {
$searching_row = $rowї"entered"];
$num_of_links++;
}
It just hangs and does nothing.
If I skip mysql_select_db and the next 4 lines with "//", it ofcourse works, but says "No Database Selected". Why won't it let me use it?
:: also, I had this problem before while teaching myself php/mysql. The book I read also used mysql_select_db and it didnt work then too. I practicing on a test site that I ran from my machine. I had to use mysql_query("USE db_name") to get it to work. But now I'm on a webhost and need db_name to be a variable. It wouldn't work as mysql_query("USE $db_name").
Last edited by bmac11 on Thu Feb 17, 2005 5:22 pm, edited 1 time in total.
Sounds to me like $linksinfo isn't set or is empty.
Do an:
echo '**'.$linksinfo.'**';
to check (the **'s are just so you can see the output if it's empty).
hmmm, probably because its not supposed to be a variable.
Must of added it when moving code and debugging.
I removed the $.. and eventually got it to work. But now my php code isnt working right. I dont understand. The same exact code is working on my test site on my computer. Now its on a webhost and it is access a "if" statement that it shouldnt.
it never ends....
I'm gonna go over this some more.. be back if I get stuck again.
When testing it's a good idea to have error_reporting turned up, eg at the top of your code put:
error_reporting(E_ALL);
It will then catch undefined variables etc..etc. ( you may also need to turn display_errors On too either in your php.ini, httpd.conf, a .htaccess or using ini_set() )
I had a problem before too about an error at line one. Eventually I found out that I had an error in an SQL query somewhere down the page. I had something like this:
SELECT * FROM table WHERE table = 'John's Stuff'";
noties the single ' inside of the two ' 's in my where clause. Check for something like that.
You could most likely figure out if this is the problem by printing all of your SQL queries to the browser.