synking Posted August 14, 2012 Share Posted August 14, 2012 Hey guys hope you can help. I'm learning to use pdo and just started but for some reason what i am trying does not work. here is the code try{ $dbh = new PDO("mysql:host=$serv;dbname=$data", $name, $pass); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $sql = 'Select * from Customer_Card'; $sth = $dbh->prepare($sql); $sth->execute(); $tables = ''; foreach ($sth->fetch(PDO::FETCH_ASSOC) as $row) { echo "<pre>"; print($row); echo "</pre>"; $tables .= " <tr>"; $tables .= " <td>{$row['Name']} </td>\n"; $tables .= " <td>{$row['Headline']} </td>\n"; $tables .= " <td>{$row['Problem']} </td>\n"; $tables .= " <td>{$row['Promise']} </td>\n"; $tables .= " <td>{$row['Proof']} </td>\n"; $tables .= " <td>{$row['Threats']} </td>\n"; $tables .= " <td>{$row['Passion']} </td>\n"; $tables .= " <td>{$row['Cost']} </td>\n"; $tables .= " <td>{$row['Value']} </td>\n"; $tables .= " <td>{$row['Purpose']} </td>\n"; } } catch(PDOException $e) { echo ("I'm sorry, Dave. I'm aftraid I can't do that."); error_log($e-getMessage()); } Im not sure what im doing wrong but i get no errors and no data from mysql. Quote Link to comment https://forums.phpfreaks.com/topic/267065-pdo-issues/ Share on other sites More sharing options...
scootstah Posted August 14, 2012 Share Posted August 14, 2012 I don't see any syntax errors. Are you sure the database credentials are correct? Do you have error reporting turned on? At the top of your script: error_reporting(-1); ini_set('display_errors', 1); Quote Link to comment https://forums.phpfreaks.com/topic/267065-pdo-issues/#findComment-1369330 Share on other sites More sharing options...
synking Posted August 14, 2012 Author Share Posted August 14, 2012 I did not until you mentioned it... Now i am getting an error that says Warning: Invalid argument supplied for foreach() in /var/www/html/innovation/inno_show.php on line 72 And doing some searching means my query is wrong... but i don't see how it is. Quote Link to comment https://forums.phpfreaks.com/topic/267065-pdo-issues/#findComment-1369338 Share on other sites More sharing options...
scootstah Posted August 14, 2012 Share Posted August 14, 2012 Put echo '<pre>' . print_r($dbh->errorInfo(),true) . '</pre>'; after $sth->execute(). What do you get? Quote Link to comment https://forums.phpfreaks.com/topic/267065-pdo-issues/#findComment-1369341 Share on other sites More sharing options...
synking Posted August 14, 2012 Author Share Posted August 14, 2012 Alright now im getting this Array ( [0] => 00000 [1] => [2] => ) but i don't know what that means and a quick google didn't turn anything up. Quote Link to comment https://forums.phpfreaks.com/topic/267065-pdo-issues/#findComment-1369344 Share on other sites More sharing options...
scootstah Posted August 14, 2012 Share Posted August 14, 2012 Try this instead: echo '<pre>' . print_r($sth->errorInfo(),true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/267065-pdo-issues/#findComment-1369346 Share on other sites More sharing options...
synking Posted August 14, 2012 Author Share Posted August 14, 2012 echo ('<pre>'. print_r($sth->errorInfo(), true) .'</pre>'); gives me the exact same array at the very top of the page. Quote Link to comment https://forums.phpfreaks.com/topic/267065-pdo-issues/#findComment-1369348 Share on other sites More sharing options...
scootstah Posted August 14, 2012 Share Posted August 14, 2012 In that case, there's no issue with the query. It just looks like no rows are being returned. To test that theory: Place echo count($sth->fetchAll()) . ' rows'; after $sth->execute(); What do you get? Quote Link to comment https://forums.phpfreaks.com/topic/267065-pdo-issues/#findComment-1369353 Share on other sites More sharing options...
synking Posted August 14, 2012 Author Share Posted August 14, 2012 Hmmm.... Im getting 0 rows... but that does not make sense... if i use the same exact query with mysqli i get what i want... Hmmm Quote Link to comment https://forums.phpfreaks.com/topic/267065-pdo-issues/#findComment-1369355 Share on other sites More sharing options...
scootstah Posted August 14, 2012 Share Posted August 14, 2012 Again, are the database credentials (mainly $serv and $data) correct and what you expect? Quote Link to comment https://forums.phpfreaks.com/topic/267065-pdo-issues/#findComment-1369360 Share on other sites More sharing options...
synking Posted August 14, 2012 Author Share Posted August 14, 2012 Yes it is i just loged into mysql and checked everything with the credintials and everything. Unless PDO does not like localhost as a server name. Thats the only thing i can think of.. Does PDO prefer a type of server over another. I even changed the connection to try{ $dbh = new PDO("mysql:host=localhost;dbname=innovation", 'inno', 'inno'); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); But still no rows returned I am confused by this. Quote Link to comment https://forums.phpfreaks.com/topic/267065-pdo-issues/#findComment-1369364 Share on other sites More sharing options...
synking Posted August 14, 2012 Author Share Posted August 14, 2012 Actually just found out what was wrong... When i logged in as the user that is allowed to access just that database nothing happens... when i looged in as root it was reporting the database is curropt... does pdo not see that... either way it works now that i repeaired the database. Sorry for all the issues. Quote Link to comment https://forums.phpfreaks.com/topic/267065-pdo-issues/#findComment-1369366 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.