mrbean Posted April 21, 2011 Share Posted April 21, 2011 in php you have the following mysql API fetch commands: ■mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both ■mysql_fetch_assoc — Fetch a result row as an associative array ■mysql_fetch_field — Get column information from a result and return as an object ■mysql_fetch_lengths — Get the length of each output in a result ■mysql_fetch_object — Fetch a result row as an object ■mysql_fetch_row — Get a result row as an enumerated array If u use one of them for example i use mysql_fetch_object. $run = mysql_query(SELECT `status` FROM `[settings]` WHERE `name`= *); $display = mysql_fetch_object($run); other example $run = mysql_query(SELECT `status` FROM `[settings]` WHERE `name`= 'abusing' AND `name`= 'hacking' AND `name`= 'cracking'); $display = mysql_fetch_object($run); When i do $display->something It wil assign the SELECT condition i want to assign the WHERE condition. the value of it. like i want to do this $display->WHERE->hacking how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/ Share on other sites More sharing options...
mrbean Posted April 21, 2011 Author Share Posted April 21, 2011 notify that $display->WHERE->hacking won't work just an example of how i want to do it. tell me how to do it:P and don't say $display->WHERE->hacking won't work. is this even possible with mysql api in php? Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/#findComment-1204618 Share on other sites More sharing options...
PFMaBiSmAd Posted April 21, 2011 Share Posted April 21, 2011 Queries return the SELECTed values. The WHERE condition is not something that you select. What are you really trying to achieve, because your question makes no sense to me? Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/#findComment-1204622 Share on other sites More sharing options...
mrbean Posted April 21, 2011 Author Share Posted April 21, 2011 the WHERE condition is the thing that u want to select. for example i have an table named:[settings] and it contains: status name on functions off hacking on cracking 2 minimalcharacters $object = SELECT `status` FROM `[settings]` WHERE name =* now i want to assign the WHERE condition. so it wil only select the thing thats the name * for example the name functions: $object->functions It wil output for example: on but that isn't the right syntax how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/#findComment-1204631 Share on other sites More sharing options...
PFMaBiSmAd Posted April 21, 2011 Share Posted April 21, 2011 If you want to select a specific setting, such as the functions setting, you would write the query as - SELECT `status` FROM `[settings]` WHERE name = 'functions' If you want to select all the settings, you would write the query as - SELECT `name`, `status` FROM `[settings]` ^^^ This last query returns a row for each setting and it would be your responsibility to retrieve them and store them in the way you want to reference the values. You would typically use an array, such as $settings, and use the names as the array keys and use the status as the array values. Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/#findComment-1204635 Share on other sites More sharing options...
mrbean Posted April 21, 2011 Author Share Posted April 21, 2011 can u give an example how to fetch the status of specifically only: name='hacking',name='cracking' and name='functions' with only using one mysql_query? Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/#findComment-1204641 Share on other sites More sharing options...
PFMaBiSmAd Posted April 21, 2011 Share Posted April 21, 2011 SELECT `name`,`status` FROM `[settings]` WHERE `name` IN('hacking','cracking','functions') Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/#findComment-1204653 Share on other sites More sharing options...
mrbean Posted April 21, 2011 Author Share Posted April 21, 2011 how to fetch in php? php script example? Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/#findComment-1204661 Share on other sites More sharing options...
PFMaBiSmAd Posted April 22, 2011 Share Posted April 22, 2011 Do you need help in actually fetching the data from the result set or in storing the information in a way that your script can access the name/status pairs? Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/#findComment-1204811 Share on other sites More sharing options...
mrbean Posted April 22, 2011 Author Share Posted April 22, 2011 actually both. But now whats important for me that I want to fetch it with only using one mysql_query() with the table name: [settings] and it contains the following things: status name on something off some on thing 20 things By referring the 'name' I want to fetch the status. I want to be able to do this: $some = $mysqlthing???(I dont know) echo "the status of some is:".$some; I wanted to ask how to assign the WHERE but if there is an other alternative to do this then tell me. Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/#findComment-1205024 Share on other sites More sharing options...
wildteen88 Posted April 23, 2011 Share Posted April 23, 2011 For getting status of the setting named some. Your query would be $query = "SELECT `status` FROM `settings` WHERE `name` = 'some'"; $result = mysql_query($query); $row = mysql_fetch_assoc($query); echo "the status of some is:" . $row['status']; Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/#findComment-1205196 Share on other sites More sharing options...
mrbean Posted April 23, 2011 Author Share Posted April 23, 2011 actually both. But now whats important for me that I want to fetch it with only using one mysql_query() with the table name: [settings] and it contains the following things: status name on something off some on thing 20 things By referring the 'name' I want to fetch the status. I want to be able to do this: $some = $mysqlthing???(I dont know) echo "the status of some is:".$some; I wanted to ask how to assign the WHERE but if there is an other alternative to do this then tell me. I mean: But now whats important for me that I want to fetch all the statusses with only using one mysql_query() thus NOT this: $query = "SELECT `status` FROM `settings` WHERE `name` = 'some'";$result = mysql_query($query);$row = mysql_fetch_assoc($query);echo "the status of some is:" . $row['status']; $query2 = "SELECT `status` FROM `settings` WHERE `name` = 'things'";$result2 = mysql_query($query);$row2 = mysql_fetch_assoc($query2);echo "the status of some is:" . $row2['status']; And so on I hope u understand me better now. Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/#findComment-1205296 Share on other sites More sharing options...
wildteen88 Posted April 23, 2011 Share Posted April 23, 2011 Then remove the where clause and use a while loop to display all the settings $query = "SELECT `name`, `status` FROM `settings`'"; $result = mysql_query($query); while($row = mysql_fetch_assoc($query)) { echo 'The setting <b>' . $row['name'] . '</b> is set to <b>' . $row['status'] . '</b><br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/#findComment-1205321 Share on other sites More sharing options...
mrbean Posted April 24, 2011 Author Share Posted April 24, 2011 I want to fetch specific one status with only using one mysql_query to fetch them all by refering the name. so if possible: i want to do this echo $name['something'], $name['things']; will echo 'on20'. I don't think this is easy to do with not much mysql api functions in php. and not this: <?php $query = "SELECT `name`, `status` FROM `settings`'"; $result = mysql_query($query); while($row = mysql_fetch_assoc($query)) { if($row['name'] == 'some') { echo 'status of some is:'.$row['status']; } else { echo 'The setting <b>' . $row['name'] . '</b> is set to <b>' . $row['status'] . '</b><br />'; } } ?> this will slow down my server until it has found: 'status' of 'some' so: 1 mysql_query will fetch all the statusses of all names and referring the specific name only will echo the status. thats why i asked if i could assign the where clause/function/condition (don't know what it is). Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/#findComment-1205619 Share on other sites More sharing options...
wildteen88 Posted April 24, 2011 Share Posted April 24, 2011 so if possible: i want to do this echo $name['something'], $name['things']; You will have to build the array of settings from the results of the query, Like so // get all settings from database $query = "SELECT `name`, `status` FROM `settings`'"; $result = mysql_query($query); // now add all settings to an array // we'll call it $settings while($row = mysql_fetch_assoc($query)) { $name = $row['name']; $status = $row['status']; $setting[$name] = $status; } // display the status of the setting named some echo $setting['something']; Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/#findComment-1205632 Share on other sites More sharing options...
mrbean Posted April 26, 2011 Author Share Posted April 26, 2011 awesome! Self I will never be able to discover that. It's a great interpretation of assigning the WHERE condition. Thank you! btw. if someone does know how to assign (directly from the query!) the where condition I would love to hear that. But I don't think thats possible. Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/#findComment-1206568 Share on other sites More sharing options...
wildteen88 Posted April 26, 2011 Share Posted April 26, 2011 btw. if someone does know how to assign (directly from the query!) the where condition I would love to hear that. But I don't think thats possible. By assign do you mean something like this? $data = $db->select->where('name', $name); Rather than having to type out $query = "SELECT name from table WHERE name = '$name'"; $result = mysql_query($query); $data = mysql_fetch_assoc($result); If thats what you mean, then you'll want to look for a third party active record class, like PHP Active Record. Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/#findComment-1206593 Share on other sites More sharing options...
mrbean Posted April 27, 2011 Author Share Posted April 27, 2011 How awesome are u? I need to learn how to make such a class like that. It's really useful for me. But thanks for the advice and I am going to explore the class and learn from it. Quote Link to comment https://forums.phpfreaks.com/topic/234384-mysql-api-in-php/#findComment-1206835 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.