473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Using mysql in php

My table is set up like this

Field Type
name varchar(50)
setting varchar(50)
userid int(11)
value text

Now im trying to get the userid from the database where the name = name and setting = month the value = 6 and the setting = day and the value = 7

in the field "setting" there are various data name 2 of them are called day and month and basically im trying to get the userid number from the database where day = today and month = this month ive tried this and a few other ways but they dont work :(

Expand|Select|Wrap|Line Numbers
  1. $day = date("j");
  2. $month = date("n");
  3. $sql = "(SELECT userid FROM " .tablename. " WHERE name = 'addon' AND setting = 'day' AND value = $day) UNION (SELECT userid FROM " .tablename. " WHERE name = 'addon' AND setting = 'month' AND value = $month)";
  4. $result = db_query($sql);
  5. $row = db_fetch_assoc($result);
  6.  
Any help please?
Jul 6 '10 #1
17 2841
TheServant
1,168 Expert 1GB
Welcome to Bytes.
Do you get any errors?

tablename should be $tablename, I imagine. Fix that and see how it works.
Jul 6 '10 #2
@TheServant
Well thats just it the above code sort of works but it only picks one when there are more than one that meet the the search.
Jul 7 '10 #3
TheServant
1,168 Expert 1GB
@acoolguy
So that was just a typo with tablename instead of $tablename?

From what you have given, I can't see any other errors, so can you post how you're processing your result to display it?

Also, can you try and run two queries:
Expand|Select|Wrap|Line Numbers
  1. $sql_1 = "SELECT userid FROM " .$tablename. " WHERE name = 'addon' AND setting = 'day' AND value = $day";
  2. $sql_2 = "SELECT userid FROM " .$tablename. " WHERE name = 'addon' AND setting = 'month' AND value = $month";
  3.  
  4. $result_1 = db_query($sql_1);
  5. $result_2 = db_query($sql_2);
  6. $row_1 = db_fetch_assoc($result_1);
  7. $row_2 = db_fetch_assoc($result_2);
  8.  
If they both work as expected, then I don't know. If they fail, or get the same error as when you use the UNION, then it's cleary your MySQL statement or database structure.

I usually use mysql_fetch_array(), so I am not sure of particular differences between that and db_fetch_assoc(), however, if you are generating more than one row of results, you will need to retrieve them with a while loop:
Expand|Select|Wrap|Line Numbers
  1. while($row = mysql_fetch_array($result)){
  2.     echo $row['userid'];
  3. }
Jul 7 '10 #4
@TheServant
Thanks for the reply however using the 2 sql statements it only shows 1 result yet i know there are more than 1 as i checked the database myself :(
I use db_num_rows($row) to get the number of rows thats returned from the query and it shows only 1 result yet there are more than 1.
Jul 7 '10 #5
@acoolguy
Update:

After going to the phpmyadmin and running the follow code in the sql section i get the result of 4 however using the same code in php i get 1 result ?

Expand|Select|Wrap|Line Numbers
  1. SELECT userid
  2. FROM `tablename`
  3. WHERE name = 'addon'
  4. AND setting = 'day'
  5. AND value = '7'
  6. UNION SELECT userid
  7. FROM `tablename`
  8. WHERE name = 'addon'
  9. AND setting = 'month'
  10. AND value = '7'
Jul 7 '10 #6
TheServant
1,168 Expert 1GB
So if you do:
Expand|Select|Wrap|Line Numbers
  1. $sql_1 = "SELECT userid FROM " .$tablename. " WHERE name = 'addon' AND setting = 'day' AND value = $day"; 
  2. $sql_2 = "SELECT userid FROM " .$tablename. " WHERE name = 'addon' AND setting = 'month' AND value = $month";   
  3. $result_1 = db_query($sql_1); 
  4. $result_2 = db_query($sql_2); 
  5. echo db_num_rows($result_1);
  6. echo db_num_rows($result_2);
  7.  
The first one returns the correct number and the second always returns 1, and there should be more than 1 for the second?

The other thing to test is remove your variables and try run EXACTLY the same string using PHP like so:
Expand|Select|Wrap|Line Numbers
  1. $result_2 = db_query("SELECT userid FROM `tablename` WHERE name = 'addon' AND setting = 'day' AND value = '7' UNION SELECT userid FROM `tablename` WHERE name = 'addon' AND setting = 'month' AND value = '7' "); 
  2. echo db_num_rows($result_1);
Jul 8 '10 #7
@TheServant
I have tried them both ways in both the phpmyadmin sql section and in the php file and both ways is the same result which is
phpmyadim sql section result 4
php file result 1
its a real mystery to me i have tried a load of different ways and they all produce the same results in phpmyadmin sql section the result is 4 (correct)
php file the result =1 (wrong)
Jul 8 '10 #8
TheServant
1,168 Expert 1GB
It has to be how you are calling or counting it in PHP. Can you post your PHP script including your row counting line please?
Jul 8 '10 #9
@TheServant
Expand|Select|Wrap|Line Numbers
  1. $table = db_prefix("names");
  2.         if (get_module_setting("allowindex","birthdayscroll") == '1'){ //check if allowed to show on index page
  3.         $day = date("j");
  4.         $month = date("m");
  5.         $sql = "SELECT userid FROM " .$table. " WHERE modulename = 'addon' AND setting = 'day' AND value = '$day'";
  6.         $sql1 = "SELECT userid FROM " .$table. " WHERE modulename = 'addon' AND setting = 'month' AND value = '$month'";
  7.         $sql3 = $sql." UNION ".$sql1;
  8.         $result3 = db_query($sql3);
  9.         $row3 = db_fetch_assoc($result3);
  10.         $num = db_num_rows($row3);
  11.         $id = $row['userid'];
  12.         output("`nNum = %s`n`n",$num);
  13.  
  14. }
  15.  
Jul 8 '10 #10
TheServant
1,168 Expert 1GB
OK, basically once you call db_fetch_assoc() you destroy the original structure. If you move db_num_rows() above db_fetch_assoc() and echo that, it should be correct. You really need to use a while loop as I put above if you have more than one row, which in most cases you will.
Jul 8 '10 #11
Ok after a little rewrite i have this

Expand|Select|Wrap|Line Numbers
  1. $day = date("j");
  2. $month = date("n");
  3. $sql = "SELECT userid FROM " .$table. " WHERE name =  'addon' AND setting = 'day' AND value = '$day' UNION SELECT userid FROM " .$table. " WHERE name =  'addon' AND setting = 'month' AND value = '$month'";
  4. $result = db_query($sql);
  5. $row = db_fetch_assoc($result);
  6. foreach($row as $playerid){
  7. $sql = "SELECT name FROM " .db_prefix("accounts"). " WHERE acctid = '$playerid'";
  8. $result = db_query($sql);
  9. $row = db_fetch_assoc($result);
  10. $name = $row['name'];
  11. output("UserId = %s`nName = %s`n",$playerid ,$name);
  12. }
  13.  
Yet it still picks only one when there are more :(
Jul 8 '10 #12
TheServant
1,168 Expert 1GB
The reason is on line 5. As soon as you call db_fetch_assoc() you lose the structure, unless it's in a while loop. Try:
Expand|Select|Wrap|Line Numbers
  1. $day = date("j");
  2. $month = date("n");
  3. $sql = "SELECT userid FROM " .$table. " WHERE name =  'addon' AND setting = 'day' AND value = '$day' UNION SELECT userid FROM " .$table. " WHERE name =  'addon' AND setting = 'month' AND value = '$month'";
  4. $result = db_query($sql);
  5. while($row = db_fetch_assoc($result)){
  6.     echo $row['userid'];
  7. }
Jul 8 '10 #13
@TheServant
Thanks for all the help so far however after trying the code like this ...
Expand|Select|Wrap|Line Numbers
  1. $day = date("j");
  2. $month = date("n");
  3. $sql = "SELECT userid FROM " .$table. " WHERE name =  'addon' AND setting = 'day' AND value = '$day' UNION SELECT userid FROM " .$table. " WHERE name =  'addon' AND setting = 'month' AND value = '$month'";
  4. $result = db_query($sql);
  5. while($row = db_fetch_assoc($result)){
  6. output("UserId = %s`n",$row['userid']);
  7. }
  8.  
It now shows everyone regardless of the conditions in the sql statement ?
Jul 8 '10 #14
TheServant
1,168 Expert 1GB
I don't think you need UNION:
Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT userid FROM " .$table. " WHERE name = 'addon' AND ( (setting = 'day' AND value = '$day') OR (setting = 'month' AND value = '$month') );
I haven't done this for a while, but I think that should work.
Jul 8 '10 #15
@TheServant
Thanks a million i thought it might have been the database structure and that i would have to reorganize it in a better way however
Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT userid FROM " .$table. " WHERE name = 'addon' AND ( (setting = 'day' AND value = '$day') OR (setting = 'month' AND value = '$month') );
  2.  
didn't work but
Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT userid FROM " .$table. " WHERE name = 'addon' AND setting = 'day' AND value = '$day' AND setting = 'month' AND value = '$month'";
  2.  
did work once again thanks a million :)
Jul 8 '10 #16
TheServant
1,168 Expert 1GB
Haha, you know I was looking at your logic for a while and thought that that was the mistake, but then I just presumed you were doing something tricky. Glad it works.
Jul 9 '10 #17
Thanks again :)
Jul 9 '10 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Campbell's List | last post by:
Hi, I'm completely new to MySql and need help importing tables from Access or Dbase. I am using Dreamweaver MX to create a data-driven web site. With the VPS hosting plan we're on, our remote...
7
by: phillip.s.powell | last post by:
We're looking at a GUI interface for our MySQL DB and I am interested in MySQL Administrator, however, one of our requirements is to be able to import/export databases. Is this possible or do I...
1
by: encohen | last post by:
There have been several posts on having MS Access using MySQL as the backend db. I know that you have to set up an ODBC connection and use the MySQL odbc driver. What I want to know is how to set up...
3
by: sp3d2orbit | last post by:
I've read the MySQL licensing material and what I've found online, but I'm still unclear about when I have to pay MySQL for a license. Scenario: I've created an application that stores some data...
2
by: Shun | last post by:
Hello, I am new to .net Platform. I want to build a sample application using VB.net and backend mysql. now my problem is how to connect to the mysql database. which is the better provider(...
0
by: xkp | last post by:
Hi all, i just had to install the 5.0 version of mysql. previously i used an old 3."something" (my server crashed so i dont remember the precise version). I used to access mysql database using...
14
by: Ben | last post by:
I don't know whether anyone can help, but I have an odd problem. I have a PSP (Spyce) script that makes many calls to populate a database. They all work without any problem except for one...
1
by: WebNewbie | last post by:
Hi, I am new to using mysql and there isn't any tutorials online on that shows how to create mysql stored procedure for paging purposes. Thus, I read tutorials on creating stored proc that were...
0
by: John Kirkpatrick | last post by:
Hi all, I am having difficulty displaying records on a frontend MS Access 2000 form using a MySQL backend. The following code works well with the Jet database engine but doesn't work properly...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.