Welcome Guest ( Log In | Register )



3 Pages V  < 1 2 3 >  
Reply to this topicStart new topic
> MySQL, Multiple Tables
nightfox
post Aug 4 2006, 03:23 AM
Post #11


NiGHTFoX - Hiding in the dark
Group Icon

Group: Members
Posts: 680
Joined: 3-April 05
Member No.: 3,584



QUOTE(Chesso @ Aug 3 2006, 10:00 PM) *

$sql = "SELECT * FROM `users`, `content` WHERE `access_name`='$active_user' AND `users.id`= 'content.id'";

How does users.id and content.id work anyway. You are most likely getting the error on trying to fetch the array because the syntax is wrong or something you try to grab does not exist (meaning it could still mean wrong syntax).

Didn't work....

I don't even know how it works... I'm going off the pointless examples that don't make any sense... I've been googling & looking through my PHP & MySQL books... so based on these confusing examples, I revised my code to this:
CODE

$sql = "SELECT * " .
       "FROM users u " .
       "JOIN content c " .
       "WHERE access_name='$active_user' AND id = 'id'";
$row = mysql_fetch_array(mysql_query($sql));

and for some reason, it's complaining about this line:
$row = mysql_fetch_array(mysql_query($sql));

It's always that line whenever I work with MySQL... that's why I hate working with it so much...

[N]F
Go to the top of the page
 
+Quote Post
Chesso
post Aug 4 2006, 03:34 AM
Post #12


Teh Coder
Group Icon

Group: Members
Posts: 1,053
Joined: 18-April 06
From: Australia
Member No.: 12,833



Yeah because of the mysql_fetch_array, it errors because the syntax is incorrect for the SQL or your'e not asking for information exists.
Go to the top of the page
 
+Quote Post
nightfox
post Aug 4 2006, 12:33 PM
Post #13


NiGHTFoX - Hiding in the dark
Group Icon

Group: Members
Posts: 680
Joined: 3-April 05
Member No.: 3,584



ok,ok... I took an example from my other php book and now, I replaced
$row = mysql_fetch_array(mysql_query($sql));
to
$result = mysql_query($sql)
or die(mysql_error());

and now, I got a different error message:
Column 'id' in where clause is ambiguous

At least it is better than working with Microsoft errors which tell you nothing... I can partly understand what that means...

Edit: Ok, I re-did the code and it's now this:
CODE

$sql = "SELECT * " .
       "FROM users u " .
       "JOIN content c " .
       "WHERE u.access_name='$active_user' AND u.id = 'c.cid'";
$row = mysql_fetch_array(mysql_query($sql));

not producing any errors, but now I see my "Invalid ID or General Error" message even when I try to execute the script with a VALID access name (test.php?id=access_name)

[N]F

This post has been edited by nightfox: Aug 4 2006, 12:46 PM
Go to the top of the page
 
+Quote Post
vhortex
post Aug 4 2006, 05:45 PM
Post #14


Guilty Until Proven Innocent
Group Icon

Group: Members
Posts: 372
Joined: 13-April 05
Member No.: 3,937



QUOTE(nightfox @ Aug 4 2006, 08:33 PM) *

Edit: Ok, I re-did the code and it's now this:
CODE

$sql = "SELECT * " .
       "FROM users u " .
       "JOIN content c " .
       "WHERE u.access_name='$active_user' AND u.id = 'c.cid'";
$row = mysql_fetch_array(mysql_query($sql));

not producing any errors, but now I see my "Invalid ID or General Error" message even when I try to execute the script with a VALID access name (test.php?id=access_name)

[N]F


Please recode that to this one..
you have problems with the quotes.. use ` when you want to say table name or column name.

avoid using ' since mySQL dont understand that well.. " is use for table names..
CODE

$sql = "SELECT * " .
       "FROM users u " .
       "JOIN content c " .
       "WHERE `u`.`access_name` = `"  . $active_user. "` AND `u`.`id` = `c`.`cid`";
$row = mysql_fetch_array(mysql_query($sql));


I hope this solves the problem.


***********

about the $active_user
where do you get its value? it seems that you are using this variable but it was never been given a value.

please use this for visual test after the declaration of $sql
CODE

echo $sql;


if the "name =" part have nothing in its rigth side then, you gave fail to assign $active_user a value.



This post has been edited by vhortex: Aug 4 2006, 05:49 PM
Go to the top of the page
 
+Quote Post
nightfox
post Aug 4 2006, 07:21 PM
Post #15


NiGHTFoX - Hiding in the dark
Group Icon

Group: Members
Posts: 680
Joined: 3-April 05
Member No.: 3,584



QUOTE(vhortex @ Aug 4 2006, 01:45 PM) *

CODE

$sql = "SELECT * " .
       "FROM users u " .
       "JOIN content c " .
       "WHERE `u`.`access_name` = `"  . $active_user. "` AND `u`.`id` = `c`.`cid`";
$row = mysql_fetch_array(mysql_query($sql));


I hope this solves the problem.
***********

Nope, didn't work... it's still complaining about $row ...

Visual reference of the $sql:
SELECT * FROM users u JOIN content c WHERE `u`.`access_name` = `silly_george` AND `u`.`id` = `c`.`cid`
QUOTE

about the $active_user
where do you get its value? it seems that you are using this variable but it was never been given a value.

It has a value... well, it's from the URL...

my original code:
CODE

// #############################################
$active_user = $_GET['id'];
// #############################################

so basically, if there's a valid access name that comes after the ?id= in the URL, then fetch that user's content for their profile out of the content table.

[N]F
Go to the top of the page
 
+Quote Post
vhortex
post Aug 4 2006, 09:03 PM
Post #16


Guilty Until Proven Innocent
Group Icon

Group: Members
Posts: 372
Joined: 13-April 05
Member No.: 3,937



please try this one instead..

CODE

SELECT *
FROM users u
JOIN content c
WHERE `u`.`access_name` = \""  . $active_user. "\"
AND `u`.`id` = `c`.`id`


the changes are in the quotes..

\"" . $active_user. "\"

plus this typo

`c`.`id`

i hope this will work now
Go to the top of the page
 
+Quote Post
nightfox
post Aug 4 2006, 11:08 PM
Post #17


NiGHTFoX - Hiding in the dark
Group Icon

Group: Members
Posts: 680
Joined: 3-April 05
Member No.: 3,584



grrr...

now it is giving me Parse error: parse error, unexpected T_VARIABLE for line 18... guess which line that is tongue.gif

as for the code, I'm assuming it is supposed to be like this:
CODE

$sql = "SELECT *
       FROM users u
       JOIN content c
       WHERE `u`.`access_name` = \""  . $active_user. "\"
       AND `u`.`id` = `c`.`id`"


I think that section just needs to be tweaked and then it should work... this script is really starting to get annoying and I'm tempted to put it all into one table...

UPDATE: I GOT IT TO WORK!!!

Well, I cheated... I took the semi-long-and-not-nearly-effective way to do it... here's what I did:
CODE

$sql = "SELECT * FROM users WHERE `access_name` = \""  .$active_user. "\"";
$row = mysql_fetch_array(mysql_query($sql));
$user_id = $row['id'];
$sql2 = "SELECT * FROM content WHERE `cid` = \""  .$user_id. "\"";
$row2 = mysql_fetch_array(mysql_query($sql2));

As you can see, since I called up one query which has the User ID number in it and then turned that number into a variable to link the two databases... tongue.gif now I can get on with my life. But still, I'd like to take the old code and get that to work.

[N]F

This post has been edited by nightfox: Aug 4 2006, 11:55 PM
Go to the top of the page
 
+Quote Post
Chesso
post Aug 5 2006, 01:40 AM
Post #18


Teh Coder
Group Icon

Group: Members
Posts: 1,053
Joined: 18-April 06
From: Australia
Member No.: 12,833



Also have you considered using $_REQUEST? Instead of $_POST or $_GET, I'm pretty sure it handles both and I have been using it for some time without fail smile.gif
Go to the top of the page
 
+Quote Post
Quatrux
post Aug 5 2006, 03:27 AM
Post #19


the Q
Group Icon

Group: [HOSTED]
Posts: 1,017
Joined: 13-July 05
From: Lithuania, Vilnius
Member No.: 7,059



QUOTE(Chesso @ Aug 5 2006, 04:40 AM) *

Also have you considered using $_REQUEST? Instead of $_POST or $_GET, I'm pretty sure it handles both and I have been using it for some time without fail smile.gif


I personally avoid to use $_REQUEST; superglobal, but I know quite a lot of people who like to use it, so I guess it is alright, but it can make problems in your script, because I never trust user coming information into my script, so knowing through which method it came is quite alright, especially if you use both methods, but for some time now I really like to use QUERY_STRING with which you get the string index.php?string and even better PATH_INFO, with which you can get the string which comes from /index.php/string, but eventually I don't have anything against $_REQUEST; I just avoid it biggrin.gif
Go to the top of the page
 
+Quote Post
vhortex
post Aug 5 2006, 04:47 AM
Post #20


Guilty Until Proven Innocent
Group Icon

Group: Members
Posts: 372
Joined: 13-April 05
Member No.: 3,937



QUOTE
as for the code, I'm assuming it is supposed to be like this:
CODE

$sql = "SELECT *
       FROM users u
       JOIN content c
       WHERE `u`.`access_name` = \""  . $active_user. "\"
       AND `u`.`id` = `c`.`id`"


yeah, you are rigth.. i just posted the query.. biggrin.gif
i was so sleepy when i do that and lazy tooo...

CODE
$sql = "SELECT *
       FROM users u
       JOIN content c
       WHERE `u`.`access_name` = \""  . $active_user. "\"
       AND `u`.`id` = `c`.`id`";


there is a semicolon at the end.. and the parse error is that it is suppose to inside a quote string variable..

--------

They told me this when I started..
Good programmers -- modular / Object oriented approach of programming..
Good programmers -- reuses well crafted codes as libraries..
Good programmers -- have less work to do..
Good programmers -- turns into lazy people..

I guess i put much more focus on the lazt stuff. maybe if i was lazy enough i can be a good programmer..

Go to the top of the page
 
+Quote Post

3 Pages V  < 1 2 3 >
Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. MySQL - Trouble With Bulk Insert Statements(3)
  2. MySQL Realtime Replication(4)
  3. Recover Tables From A MySQL .frm File(8)
  4. Mirror My MySQL Database To Another Mysql Server(4)
  5. How To Connect MySQL With Flash?(8)
  6. MySQL Output Database Question(18)
  7. Linking Two Tables(12)
  8. Navcat For MySQL(8)
  9. Permission Problem With Mysql Database Creation(8)
  10. Mysql And Php(15)
  11. Problems With Php Saving Data Into Mysql(6)
  12. Login System Using A Mysql Db(5)
  13. Oracle Vs. Mysql Vs. Postgresql(9)
  14. Subqueries In Mysql(1)
  15. Apache Php With Mysql On Windows [solved](9)
  1. Not Understanding Mysql(4)
  2. Mysql Script Help(3)
  3. Mysql - So Hard(14)
  4. Mysql Problem(1)
  5. Sun Bought Mysql(6)
  6. Mysql Backup With Another Address?(4)
  7. I Have An Error With My Mysql Connection(7)
  8. Mysql And User File_priv(0)
  9. Mysql Database Management(1)
  10. Mysql Database Entry By Excel Sheets(2)
  11. Mysql On Computer(9)
  12. Any Website Provide Free Host Mysql Host?(4)
  13. Mysql Multiple Tables(1)


 



- Lo-Fi Version Time is now: 30th August 2008 - 03:59 PM