0

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;
}
21
  • You're closing your connection before returning your $row - place it on top of mysqli_close($con); so that mysqli_close($con); is your last line. Commented Sep 5, 2013 at 1:21
  • 1
    please print_r(getData()); and show me result.Avoid require inside your function Commented Sep 5, 2013 at 1:24
  • 1
    @Fred-ii- no difference,if he puts close statement after return ,this statement wouldn't execute and connection will not be closed Commented Sep 5, 2013 at 1:26
  • 3
    shouldnt the mysql_query() function be mysql_query($sql, $con);? and to close shouldnt it be mysql_close($con);? edit:: i think that is why it isnt returning.. it isnt completing the function due to the syntax errors Commented Sep 5, 2013 at 1:32
  • 2
    You are mixing mysql_* and mysqli_* Commented Sep 5, 2013 at 1:37

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 e.g. 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.

3 Comments

I also mentioned something to the affect earlier, about the connection being closed, but that theory was quickly shot down.
@Fred-ii- You thought that nothing is returned because he closed connection before return, that's not true since the result is in $row and that's all it matters. I was trying to say that he only needs to connect to db once per request. mysql_close is not necessary since it's closed automatically (unless he specifically wants/have a reason).
Ok I understand, thanks for clearing that up. I misunderstood "Connection is closed at the end of request, you are just making unnecessary connect/disconnect actions." in your answer, that's why I said that.
0
    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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.