0

when running this query in phpMyAdmin, I get the correct values, but when using the same line in a PHP script, It always gives x=0 y=0.

All other values are correct only x and y for some reason return 0.

EDITED not getting the right values

code:

$sql = "select a.image_id as id, 
 i.image_url as url, 
 i.image_x as x,
 i.image_y as y 
from album a 
join images i 
 where a.album_id = 1 
and 
 i.image_id = a.image_id";

echo getFunc($sql); 

function getFunc($sql) {
 try {
    $db = getConnection();
    $stmt = $db->query($sql);
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $db = null;
    return json_encode($result);
 } catch(PDOException $e) {
    echo $e->getMessage();
 }
};                      
function getConnection() {
  $dbhost="127.0.0.1";
  $dbuser="root";
  $dbpass="";
  $dbname="efrattest";
  $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
  $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $dbh->exec("set names utf8");
  return $dbh;
}

mySQL table:

image_id    int(11)          
image_url   varchar(250)             
image_x int(9)       
image_y int(9)
6
  • 2
    is $sql defined? We cannot tell in this example as there is a stray select query Commented Oct 13, 2013 at 16:02
  • As Ray points out, is $sql defined? Is $album_id correctly defined? Commented Oct 13, 2013 at 16:32
  • Sorry guys I edited the question. Anything else that is wrong with it? Commented Oct 13, 2013 at 17:13
  • 1
    You forgot the ; after the first line... (Where you do your $sql = [...]) Commented Oct 13, 2013 at 17:16
  • indeed Marty, that's because of copy/pasting. the code is good, nothing wrong except for the values returned Commented Oct 13, 2013 at 17:41

1 Answer 1

1

Thanks everybody, the problem lays at

$dbhost="127.0.0.1";

I changed that from 'localhost' to '127.0.0.1' as it was supposed to be better with PDO (A tip I heard online)

changed it back to 'localhost' now everything works as it should.

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

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.