php - mysql_query() 和 mysql_result() 有什么区别?

标签 php mysql json

我不确定在数据库上运行查询时是应该使用 mysql_result() 还是 mysql_query()。这对下面的情况有影响吗?

$usertable = 'tableName';
$colName    = 'columnA'; 
$xlookup = 'columnB';

// Connect to Server
$con = mysql_connect($hostname, $username, $password);

// select db
mysql_select_db($dbname);

// run query
$result = mysql_query("SELECT $colName FROM $usertable where $xlookup = 5");

// pass results to webpage
$a = 51;
$x = array($a, $a, mysql_result($result));
echo json_encode($x);

目前,我是否使用它并没有什么区别,因为两者都不起作用,但我认为错误会阻止代码运行。


我试图使用下面的代码来识别任何错误,但不确定它是否正确。

// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die("<html><script language='JavaScript'>alert('Unable to run query'), $message</script></html>");
}

最佳答案

mysql_query 执行查询并返回结果集。

mysql_result 返回结果集中的行供您使用。

查看一些示例 here .

mysql_result 具有能够返回特定字段的区别,但正如其他张贴者指出的那样,它比其他获取函数慢。

关于php - mysql_query() 和 mysql_result() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6164396/

相关文章:

php - Laravel ownsToMany 插入查询中的表顺序

c# - 放置后如何从 philips hue 桥获取 http json 响应

php - 为什么一个变量在类中可以是静态的而不能是全局的

php - 使用 WooCommerce API 检索产品属性数据和值

mysql - 使用 mysql 进行 MAX 和 CONVERT

json - 如何在golang中使用变量设置json标签?

javascript - 解析没有逗号分隔符的json数据

php - 获取错误 "Below is a rendering of the page up to the first error."

php - 更新数据时出现禁止错误

Mysql结果减半