0
function GetID($newsTitle){

  mysql_select_db($database_dbConnection, $dbConnection);
  $query_rsGetId = sprintf("SELECT id FROM news WHERE title = %s", GetSQLValueString($newsTitle, "text"));
  $rsGetId = mysql_query($query_rsGetId, $dbConnection) or die(mysql_error());
  $row_rsGetId = mysql_fetch_assoc($rsGetId);
  $totalRows_rsGetId = mysql_num_rows($rsGetId);

  return $row_rsGetId['id'];

}

The above code will retrieve the ID when a string variable is passed to it and it will return the ID of that String once it matches in the database, but currently its not returning anything.

Thanks

3
  • are you sure the query is correct? Commented Dec 13, 2016 at 11:21
  • 0. Please do not use mysql_* functions. They are deprecated and removed. 1. Check var_dump($query_rsGetId) to be the right query and put it in phpMyAdmin or something to verify that it's correct. 2. database_dbConnection and $dbConnection aren't defined inside the function. Commented Dec 13, 2016 at 11:22
  • 1
    $database_dbConnection and $dbConnection don't appear to be in the function scope, the mysql_* functions don't exist in PHP7 and using sprintf to interpolate the "title" is weird (and it'll not have the requisite apostrophes surrounding it) - best to start over. Commented Dec 13, 2016 at 11:23

1 Answer 1

3

Summarising everything that I said in comments, which more or less solves the issue. Your code has some vulnerabilities and you need to fix them to make them work:

  1. Please, please, please, don't use mysql_* functions. They are deprecated in PHP 5.5 and are removed in PHP 7. (Latest version of PHP is 7.1).

  2. Global Scope? The variables database_dbConnection and $dbConnection aren't defined inside the function. If you are using them from the global scope, please use:

    global database_dbConnection;
    global $dbConnection;
    
  3. Use Error Handling mechanism to show the errors. One best way to enable the display_errors. Set it using the following:

    // Code
    ini_set("display_errors", 1);
    // php.ini
    display_errors = On
    
  4. Correct Query. Finally, make sure if the query that's gonna be executed is correct. Use var_dump or print_r to check the output and execute it on the MySQL Server using phpMyAdmin or any other client.

    var_dump($query_rsGetId);
    
Sign up to request clarification or add additional context in comments.

1 Comment

@Hallur Updated. Thanks.

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.