Nodral Posted November 10, 2011 Share Posted November 10, 2011 Hi All I've a question regarding php and mysql database interrogation. Is there a way of applying a scalar database call from php, rather than having to return values in an array? For example if I know there is only 1 value in a DB table which matches a select statement, do I have to return it with the following or is there another function which will save me using arrays? $sql="SELECT name FROM users WHERE id = 10"; $sql=mysql_query($sql); $row=mysql_fetch_array($sql); This would then give me $row['name'] to work with. Is there a way of doing this? Quote Link to comment https://forums.phpfreaks.com/topic/250853-mysql-scalar/ Share on other sites More sharing options...
cypher86 Posted November 10, 2011 Share Posted November 10, 2011 try $count=mysql_num_rows($sql) Quote Link to comment https://forums.phpfreaks.com/topic/250853-mysql-scalar/#findComment-1286983 Share on other sites More sharing options...
trq Posted November 10, 2011 Share Posted November 10, 2011 The simple answer is no. The interfaces are designed to return arrays because it is far more common to retrieve more than one field form a database. If you want to abstract this further yourself, php allows you to create your own functions. Quote Link to comment https://forums.phpfreaks.com/topic/250853-mysql-scalar/#findComment-1286989 Share on other sites More sharing options...
xyph Posted November 10, 2011 Share Posted November 10, 2011 The newer MySQL handlers don't have this, but the original has mysql_result to grab a single column. You can always use list( $name ) = mysql_fetch_array( $sql ); Quote Link to comment https://forums.phpfreaks.com/topic/250853-mysql-scalar/#findComment-1287100 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.