Snewzzer Posted June 18, 2016 Share Posted June 18, 2016 I just want some php code to retrieve a single value from my mysql database, sounds simple, I know the mysqli query but not the php command to use. Have tried everything I can see on the web but nothing works... help! Quote Link to comment https://forums.phpfreaks.com/topic/301361-get-single-value-from-table/ Share on other sites More sharing options...
Jacques1 Posted June 18, 2016 Share Posted June 18, 2016 There is no special mysqli method for fetching a single value. You fetch the entire row like with any other query. If you want more specific advice, show your current code. Quote Link to comment https://forums.phpfreaks.com/topic/301361-get-single-value-from-table/#findComment-1533798 Share on other sites More sharing options...
benanamen Posted June 18, 2016 Share Posted June 18, 2016 Start here: https://phpdelusions.net/pdo Quote Link to comment https://forums.phpfreaks.com/topic/301361-get-single-value-from-table/#findComment-1533799 Share on other sites More sharing options...
Snewzzer Posted June 18, 2016 Author Share Posted June 18, 2016 Thanks Jacques1, my code(which doesn't work !) for fetching a single value is:- $result= mysql_result(mysql_query("SELECT colname FROM table WHERE players = 'name'")); So if I have to fetch a row, how do I extract the single value that I need? Quote Link to comment https://forums.phpfreaks.com/topic/301361-get-single-value-from-table/#findComment-1533801 Share on other sites More sharing options...
Snewzzer Posted June 18, 2016 Author Share Posted June 18, 2016 Thanks Benanamen, looks like it's fetchColumn that I should be using, will give it a try. Quote Link to comment https://forums.phpfreaks.com/topic/301361-get-single-value-from-table/#findComment-1533802 Share on other sites More sharing options...
Snewzzer Posted June 18, 2016 Author Share Posted June 18, 2016 fetchColumn is giving me the error message - Undefined variable: pdo Quote Link to comment https://forums.phpfreaks.com/topic/301361-get-single-value-from-table/#findComment-1533803 Share on other sites More sharing options...
Jacques1 Posted June 18, 2016 Share Posted June 18, 2016 I suggest you start with the basics before jumping to any special features. There seems to be a lot of confusion here. You said you used mysqli, you've actually used the old ext/mysql extension, and now you've appearently switched to PDO. If you want to go with PDO, learn how to establish a connection first. Quote Link to comment https://forums.phpfreaks.com/topic/301361-get-single-value-from-table/#findComment-1533804 Share on other sites More sharing options...
Snewzzer Posted June 18, 2016 Author Share Posted June 18, 2016 Just gave the above code example of one of dozens that I have tried, this seemed the simplest to try and convey what I was trying to achieve. I'm only trying PDO on the suggestion of Benanamen, but give me a simpler way if there is one and I will use that. Newbie, 4 years since I last wrote any code. Quote Link to comment https://forums.phpfreaks.com/topic/301361-get-single-value-from-table/#findComment-1533805 Share on other sites More sharing options...
Snewzzer Posted June 18, 2016 Author Share Posted June 18, 2016 Just to clarify, maybe I wasn't clear enough. I want to retrieve a single value using the mysql statement above and assign this to a php variable. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/301361-get-single-value-from-table/#findComment-1533806 Share on other sites More sharing options...
Jacques1 Posted June 18, 2016 Share Posted June 18, 2016 (edited) What you don't seem to understand is that accessing a database requires a complete library. This isn't just one line of code which you can copy and paste. You have to establish a connection, execute a query (or in your case a prepared statement) and ultimately fetch data from the result set. So the first step is to choose a library. The mysql_* functions you were using previously are hopelessly outdated and have already been removed from the current PHP version. Nowadays were're using PDO or mysqli. PDO is a good choice. Now you need to learn PDO. We can't do that for you. You need to actually read and understand the tutorial referenced by benanamen. Edited June 18, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/301361-get-single-value-from-table/#findComment-1533807 Share on other sites More sharing options...
Snewzzer Posted June 18, 2016 Author Share Posted June 18, 2016 The following works:- $sql = "SELECT colname FROM tablename WHERE name = 'Joe'";$record = mysqli_query($con,$sql);$row = mysqli_fetch_row($record);echo $row[0]; But are you saying that I can't use this anymore? Quote Link to comment https://forums.phpfreaks.com/topic/301361-get-single-value-from-table/#findComment-1533808 Share on other sites More sharing options...
Jacques1 Posted June 18, 2016 Share Posted June 18, 2016 I said what I said. Now you need to read and understand it. I know you're in a hurry, and reading longer texts is difficult in the era of 140-character Tweets. But if you want to write any code whatsoever, you have to be able to absorb information. Programming isn't copy-and-paste. Quote Link to comment https://forums.phpfreaks.com/topic/301361-get-single-value-from-table/#findComment-1533809 Share on other sites More sharing options...
Snewzzer Posted June 18, 2016 Author Share Posted June 18, 2016 (edited) Jacques1, yes I will do, and thanks for everything. But no, I'm not in a hurry, this is a personal project of no great importance. It was such a simple query that I expected there might be a simple answer. I am returning to coding after a 4 year gap, I was a newbie then and still am now, so will endeavour to learn the current correct methods. Thanks again. Edited June 18, 2016 by Snewzzer Quote Link to comment https://forums.phpfreaks.com/topic/301361-get-single-value-from-table/#findComment-1533810 Share on other sites More sharing options...
Jacques1 Posted June 18, 2016 Share Posted June 18, 2016 This isn't about one query, it's about an entire database library. You cannot randomly insert a PDO or mysqli method call in the middle of your code. You choose one library, and then all database operations now and in the future have to be implemented with that library. You may even have to rewrite large parts of your current code. Forget about the single value stuff. This is about all of your queries. Quote Link to comment https://forums.phpfreaks.com/topic/301361-get-single-value-from-table/#findComment-1533812 Share on other sites More sharing options...
Snewzzer Posted June 18, 2016 Author Share Posted June 18, 2016 Jacques1, if I had to rewrite ALL of my code it wouldn't take too long! It consists of only two very short files which I only began a few nights ago. So I can throw everything out and start again... which I am doing now, starting with the php manual. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/301361-get-single-value-from-table/#findComment-1533813 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.