Nov 09 2009

Execute Scalar and Execute Row for Php

Category: MySql,PhpGiulio Pons @ 3:36 pm

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 "";
}
Share

Related posts:

  1. 10 PHP usefull functions for MySQL stuff
  2. MYSQL add counter in a query
  3. PHP function to fix collation on database fields of MySQL

Tags:

One Response to “Execute Scalar and Execute Row for Php”

  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

Leave a Reply