Execute Scalar and Execute Row for Php

A couple of simple functions thar returns the first value and the first row of a result set. Very usefull.…

November 9, 2009

A couple of simple functions thar returns the first value and the first row of a result set.

Very usefull.

Need the connection to be opened.


$user = execute_row("select * from user where id='23'");
$value = execute_scalar("select name from users where id='23'");

// $user['name'] is the same of $value;

function execute_scalar($sql,$def="") {
	$rs = mysql_query($sql) or die("bad query");
	if (mysql_num_rows($rs)) {
		$r = mysql_fetch_row($rs);
		mysql_free_result($rs);
		return $r[0];
		mysql_free_result($rs);
	}
	return $def;
}

function execute_row($sql) {
	$rs = mysql_query($sql) or die("bad query");
	if (mysql_num_rows($rs)) {
		$r = mysql_fetch_array($rs);
		mysql_free_result($rs);
		return $r;
	}
	mysql_free_result($rs);
	return "";
}

Author

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

Comments on “Execute Scalar and Execute Row for Php”

One thought

  1. markonan says:

    Hm ,..nice blog ,.. if you have some time just take a look on my video ,.. tnx in advance ,..
    http://www.youtube.com/watch?v=pVjq80t5E7Y

Comments are closed

Recommended

10 PHP usefull functions for MySQL stuff

Here is my personal collection of 10 php function that I always include in my database function library. I think…

January 25, 2010

MySQL fulltext search always empty

If you’ve just switched to FULLTEXT indexes and you’ve just started playing with MATCH... AGAINST syntax, but you always get…

November 23, 2013

How to remove custom fields from WordPress

Ok. You’ve added custom fields in wordpress, but how can you remove them? How to delete custom fields wordpress Two…

September 29, 2013

13 mysql tips that you don’t know

Here is a list of thirteen tips that can be usefull for your queries. If you know more tips send…

October 19, 2012

PHP function to fix collation on database fields of MySQL

This PHP function search for tables in the active db, match table’s name with the regular expression passed as first…

February 11, 2010

10 PHP usefull functions for MySQLi stuff (mysql improved)

I’ve modified the previous post to use mysqli instead of mysql. Was it necessary? I think it was good step…

January 29, 2010