GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Jun-2008, 20:51
BlueFireCO. BlueFireCO. is offline
Junior Member
 
Join Date: Dec 2006
Posts: 41
BlueFireCO. is an unknown quantity at this point
Angry

Query different tables in single database with php


I have this problem with php and mysql where I query a table a database and it works fine, but then i go and query another table in the same database and it won't let me (or returns nothing). On top of that, the "or die (mysql_error())" function won't return anything but it is dying after that statement. In the past, i have moved around this problem by putting everything in it's own database. Since that is, overall, a terrible idea, I would like to know how to fix this problem weather it be a setting i need to change or a piece of code i need to include.

PHP Code:

$mysql_connection = mysql_connect("bluefirecode.com","********","********");
    mysql_select_db("bluefirecode");

    $sql1 = "SELECT * FROM user WHERE user = '$user'";
    $result1 = mysql_query($sql1);
    $user_info = mysql_fetch_row($result1);

    $sql2 = "SELECT * FROM post WHERE id = '$post_id'";
    $result2 = mysql_query($sql2);
    $post_info = mysql_fetch_row($result2); 



i get nothing in $post_info ( 'user' and 'post' are both tables in 'bluefirecode')
  #2  
Old 25-Jun-2008, 06:36
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Query different tables in single database with php


Yes, that seems very strange.

Make sure (through an echo or something) that the $post_id actually has a value set, and that an id with that value actually exists in the table.
Since the second query is not failing (at the mysql_query() call), that leads me to believe that the query executed, but there were no results (which is not an error) -- and with no results, then the 2nd 'fetch_row' call will fail because of an invalid $result2 value.

Maybe add this [before the line assigning $post_info] for the 2nd query attempt to see what might be happening:
PHP Code:

// Check the result
// Shows the actual query sent to MySQL, and the error. 
// Useful for debugging.
if (!$result2) {
    $message  = 'Invalid query: ' . mysql_error() . "\n"; // any error.
    $message .= 'Whole query: ' . $sql2;  // the query itself
    die($message);
} 


__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 25-Jun-2008, 08:31
TreyAU21's Avatar
TreyAU21 TreyAU21 is offline
Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 116
TreyAU21 has a spectacular aura aboutTreyAU21 has a spectacular aura about

Re: Query different tables in single database with php


Quote:
Originally Posted by BlueFireCO.
I have this problem with php and mysql where I query a table a database and it works fine, but then i go and query another table in the same database and it won't let me (or returns nothing).

Not letting you is alot different than returning nothing ... It's key to find out which is occurring.

Quote:
Originally Posted by BlueFireCO.
On top of that, the "or die (mysql_error())" function won't return anything but it is dying after that statement.

You don't have the or die in your code snippet, but I'm guessing you have tried it their before. If you aren't getting into the OR DIE block, then it would appear that you ARE passing the DB a properly formatted query.

Quote:
Originally Posted by BlueFireCO.
In the past, i have moved around this problem by putting everything in it's own database. Since that is, overall, a terrible idea, I would like to know how to fix this problem weather it be a setting i need to change or a piece of code i need to include.

This is your brain ... This is your brain on having a bunch of databases with only one table each . You certainly don't want to do that. What you need to do is echo some key information (as TurboPT suggested). Probably the biggie would be echoing the query. Make sure that the query is populated with the values you think should be there. If it looks like what you think it should be, simply run the query on the mysql command prompt and see what it returns. It's obviously possible that the query just doesn't return anything.
__________________
If practice makes perfect and nobody's perfect... why practice?

Homepage: http://www.treywhite.com
Blog: http://www.treywhite.com/blog.php
Web Design Company: http://www.ewebproductions.com
  #4  
Old 25-Jun-2008, 12:57
BlueFireCO. BlueFireCO. is offline
Junior Member
 
Join Date: Dec 2006
Posts: 41
BlueFireCO. is an unknown quantity at this point

Re: Query different tables in single database with php


After following all of what you said i finally fixed the problem. As it turned out i was trying to use a different database than the one that had all the info in it. (i'm in the process of merging all the separate db's into one) So on that part thanks for walking me through my stupidity . I really cant explain why it happened in the past though, it's in the past anyhow.
  #5  
Old 25-Jun-2008, 14:18
TreyAU21's Avatar
TreyAU21 TreyAU21 is offline
Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 116
TreyAU21 has a spectacular aura aboutTreyAU21 has a spectacular aura about

Re: Query different tables in single database with php


Quote:
Originally Posted by BlueFireCO.
(i'm in the process of merging all the separate db's into one)

Peanut Butter Jelly Time!!! Keep in mind that a database should only contain tables that go together. Don't just put every table you have in one database for the sole purpose of having only one database.
__________________
If practice makes perfect and nobody's perfect... why practice?

Homepage: http://www.treywhite.com
Blog: http://www.treywhite.com/blog.php
Web Design Company: http://www.ewebproductions.com
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
automatically assigning a number into the database from php markyp MySQL / PHP Forum 2 01-May-2007 10:12
combining 2 fields in a single mysql select query mrjameer MySQL / PHP Forum 1 28-Sep-2006 20:38
Add/Remove Database tables dynamically from workspace abhi MS Visual C++ / MFC Forum 0 22-Jun-2005 07:10
Parsing PHP code that's stored in a database. JdS MySQL / PHP Forum 3 13-May-2004 10:15
PHP MySQL query jgnasser MySQL / PHP Forum 3 08-Apr-2004 06:42

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:26.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.