princeofpersia Posted December 1, 2010 Share Posted December 1, 2010 Hi guys, I have two tables where users can update their informations tables 1 is user which includes (users) id username password branch1 branch2 and next table is (branch) id branchname branchpostcode so far i have two branches with postcodes and I have been able to add the branches via drop down list from mysql in user table as below: id username password branch1 branch2 1 me dfdsfds 1 2 the branches store the branch id in the field, what i need to do is to join the tables to echo relevant postcode information for entered for users, so if i have branch1 value set to 1 then I need to connect this to my branch table and retrieve the postcode information, I came up with the code below but it gives me error $findpostcode1=mysql_query("SELECT users.branch1, branch.id, FROM users, branch WHERE users.branch1=branch.id"); while($row = mysql_fetch_array($findpostcode1)) { $findpostcode1 = $row['postcode']; echo "your postcode: $findpostcode1"; } } but i recieve this error "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource" can you help me with this please? I have googled every possible thing but as im a newbie maybe there are some techniques which i dont know. thank you in advance Quote Link to comment https://forums.phpfreaks.com/topic/220297-mysql-error-in-php/ Share on other sites More sharing options...
JakeTheSnake3.0 Posted December 1, 2010 Share Posted December 1, 2010 Try running that SQL statement directly in phpmyadmin and see what message you get. Quote Link to comment https://forums.phpfreaks.com/topic/220297-mysql-error-in-php/#findComment-1141604 Share on other sites More sharing options...
princeofpersia Posted December 1, 2010 Author Share Posted December 1, 2010 it gives me this #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM users, branch WHERE users.branch1=branch.id LIMIT 0, 30' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/220297-mysql-error-in-php/#findComment-1141613 Share on other sites More sharing options...
JakeTheSnake3.0 Posted December 1, 2010 Share Posted December 1, 2010 Look up Joins http://www.w3schools.com/Sql/sql_join.asp Quote Link to comment https://forums.phpfreaks.com/topic/220297-mysql-error-in-php/#findComment-1141616 Share on other sites More sharing options...
PFMaBiSmAd Posted December 1, 2010 Share Posted December 1, 2010 You have an extra comma in your query, at the point right before where the error message is calling your attention to something that was invalid due to that extra comma being there - the right syntax to use near 'FROM ... Quote Link to comment https://forums.phpfreaks.com/topic/220297-mysql-error-in-php/#findComment-1141626 Share on other sites More sharing options...
robert_gsfame Posted December 1, 2010 Share Posted December 1, 2010 yup..i notice this $findpostcode1=mysql_query("SELECT users.branch1, branch.id, FROM users, branch WHERE users.branch1=branch.id"); if you wish to join two tables then this is the format $query=mysql_query("SELECT table1.* FROM table1 JOIN table2 ON table1.user_id=table2.user_id WHERE table1.column='blablabla'"); here is the explanation "SELECT table1.*" - If you wish to get the whole records from table1 "table1 JOIN table2" - You join both tables table1 and table2 "ON table1.user_id=table2.user_id" - if both tables have user_id and you wish to connect both tables based on user_id "WHERE table1.column='blablabla" is the condition you wish to have. Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/220297-mysql-error-in-php/#findComment-1141631 Share on other sites More sharing options...
jcbones Posted December 1, 2010 Share Posted December 1, 2010 There are many formats to join 2 tables together. There is NATURAL, STRAIGHT, INNER, OUTER, LEFT, RIGHT joins. Although many work similar, they all have their own uses, and syntax. Your query should read as. $sql = "SELECT branch.branchpostcode FROM users, branch WHERE users.branch1=branch.id"; //<- Select only the postcode from the branch table. Edit: Left out CROSS join, which is equivalent to INNER and the same as JOIN. You don't use an ON clause with CROSS JOIN but you do with INNER JOIN. Quote Link to comment https://forums.phpfreaks.com/topic/220297-mysql-error-in-php/#findComment-1141635 Share on other sites More sharing options...
princeofpersia Posted December 1, 2010 Author Share Posted December 1, 2010 hi guys, thanks but none of these worked still getting the same error, also I have checked and there is no coma or anything to interupt the query, the code below is where I have my drop down list from branch table but when I update the table, it updates the id in branch1(users) how can i say to add postcode of that id in my users branch1 table? In drop down it shows the branch name, but when i do insert query for Branch1 it adds the branch id in my branch1 column in user field, instead Id like to insert the postcode of that branch, by doing this then i dont need to join tables. </select> Nearest Branch<select name='Branch1'><p /> <?php $branchdropdown=mysql_query("SELECT id ,branchname FROM branch"); while($row = mysql_fetch_array($$branchdropdown)) { echo "<option value=\"".$row['id']."\">".$row['branchname']."\n "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/220297-mysql-error-in-php/#findComment-1141799 Share on other sites More sharing options...
PFMaBiSmAd Posted December 1, 2010 Share Posted December 1, 2010 That's an entirely different section of code, so of course the replies don't apply to it. The replies were specific to the query and the code you posted at the start of this thread. If you look at the line of code where the error is being reported at, you will see that your code has two $$ in front of the variable name. There should only be one $ on a variable. Quote Link to comment https://forums.phpfreaks.com/topic/220297-mysql-error-in-php/#findComment-1141803 Share on other sites More sharing options...
princeofpersia Posted December 1, 2010 Author Share Posted December 1, 2010 Hi sorry for second section, I have posted it here as its anther solution of doing this code, i have removed it and added $postcodeone=$row['postcode']; in while but then when i add an entry it doesnt add anything to dtabase Quote Link to comment https://forums.phpfreaks.com/topic/220297-mysql-error-in-php/#findComment-1141808 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.