I tried using this function to return a MySQL result in PHP . I ran the query in PhpMyAdmin and it ran successfully. But when I try to run it in PHP , nothing is returned.
My table is structured like this:
<table>
<tr>
<td>Setting</td>
<td>Value</td>
</tr>
<tr>
<td>Setting</td>
<td>stylesheet.css</td>
</tr>
</table>
Function :
function getData()
{
require 'config.php';
$con = mysql_connect($hostname, $username, $password, $database);
$sql = "SELECT Value FROM Settings WHERE Setting = 'Stylesheet'";
$result = mysql_query($con,$sql);
$row = mysql_result($result, 0);
mysqli_close($con);
return $row;
}
Update : Still not working
function getData()
{
require 'config.php';
$con = mysql_connect($hostname, $username, $password, $database);
$sql = "SELECT Value FROM Settings WHERE Setting = 'Stylesheet'";
$result = mysql_query($sql, $con);
$row = mysql_result($result, 0);
mysql_close($con);
return $row;
}
Shouldn't the mysql_query()
function be mysql_query($sql, $con);
?
And to close shouldn't it be mysql_close($con);
?
Edit: i think that is reason why it isn't returning.. It isnt completing the function due to the syntax errors.
You are mixing mysql_*
and mysqli_*
. You don't need to specify connection if there is only one for mysql_*
.
Function [ <internal:mysql> function mysql_query ] {
- Parameters [2] {
Parameter #0 [ <required> $query ]
Parameter #1 [ <optional> $link_identifier ]
}
}
If you can't use eg PDO , consider refactoring your function:
require 'config.php';
$con = mysql_connect($hostname, $username, $password, $database);
function getData()
{
$sql = "SELECT Value FROM Settings WHERE Setting = 'Stylesheet'";
$result = mysql_query($sql);
$row = mysql_result($result, 0);
return $row;
}
Connection is closed at the end of request, you are just making unnecessary connect/disconnect actions.
class MySQL {
var $ServerURL='localhost';
var $ServerPort='3306';
var $DataBaseName='test';
var $DataBaseUser='root';
var $DataBasePass='';
public function __construct() {
$this->mysqli = new mysqli($this->ServerURL,$this->DataBaseUser,$this->DataBasePass,$this->DataBaseName,$this->ServerPort);
if (mysqli_connect_errno()) {
printf('failed to connect');
}
else {
}
}
}
$MySQL = new MySQL;
require 'config.php';
class Data extends MySQL {
function getData()
{
$this->sql = "SELECT Value FROM Settings WHERE Setting = 'Stylesheet'";
$result = mysqli_query($this->mysqli, $this->sql);
$this->row = mysql_result($this->result, 0);
return $this->row;
}
$Data = new Data;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.