我不确定在数据库上运行查询时是应该使用 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/