0

Wordpress 4.2.2

I've got the connection to my database working just fine. Now I just want to print or echo a table from my database using this php code and display it on my webpage.

I can't get it to display anything and I think it's because of the echo statement. I never inserted anything in the echo thinking that it doesn't require anything. I think the echo string is printing out $row which fetches $result which has my query in it.

I've tried making a separate echo and print statement with no results.

Would anyone have any suggestions?

$c = mysql_connect("localhost", "user", "password");
mysql_select_db("database");
$result = mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM    DUAL");
$row = mysql_fetch_assoc($result);
echo htmlentities($row['_message']);

EDIT: above is the code I copied from php.net. Below is my version.

$c = mysql_connect("localhost", "my_user", "f2f3243f2");
mysql_select_db("my_DB"); 
$result = mysql_query("SELECT * FROM fakeDB"); 
$row = mysql_fetch_assoc($result); 
echo htmlentities($row['_message']);

EDIT: Thank you @andrewsi. var_dump($row) has displayed the information I wanted.

array(5) { ["CharName"]=> string(6) "Majaba" ["Level"]=> string(1) "3" ["class"]=> string(4) "Bard" ["race"]=> string(8) "High Elf" ["skills"]=> string(8) "Blocking" }

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    Check your return values from your query; you can also `var_dump($row)` to see what's in there. – andrewsi Jan 16 '16 at 18:43
  • I don't see the purpose of your SQL statement? It is just repeating the same text string for every row in the DUAL table. – kojow7 Jan 16 '16 at 19:41
  • I should have mentioned that the code was from copied from php.net. There really isn't much of a difference, I just put my information in the $c variable, mysql_select_db function and my query in the $result variable. $c = mysql_connect("localhost", "my_user", "f2f3243f2"); mysql_select_db("my_DB"); $result = mysql_query("SELECT * FROM fakeDB"); $row = mysql_fetch_assoc($result); echo htmlentities($row['_message']); – Cody Tetreault Jan 17 '16 at 04:29
  • andrewsi, this var_dump function gave the results I wanted! Thank you! array(5) { ["CharName"]=> string(6) "Majaba" ["Level"]=> string(1) "3" ["class"]=> string(4) "Bard" ["race"]=> string(8) "High Elf" ["skills"]=> string(8) "Blocking" } – Cody Tetreault Jan 17 '16 at 04:53
  • Please don't use the mysql_extension! http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – marijnz0r Apr 14 '17 at 14:36
  • Yeah I don't use that method anymore anyway. Because I'm using Wordpress, I've changed all my code to use the $wpdb (Wordpress database) class. – Cody Tetreault Apr 16 '17 at 16:37

2 Answers2

0

@andrewsi wrote in a comment:

Check your return values from your query; you can also var_dump($row) to see what's in there.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • ([Answered in a comment and converted to a community wiki](https://meta.stackoverflow.com/questions/251597/question-with-no-answers-but-issue-solved-in-the-comments-or-extended-in-chat)) – Brian Tompsett - 汤莱恩 Apr 14 '17 at 14:32
-1
$c = mysql_connect("localhost", "user", "password");
mysql_select_db("database");
$result = mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM    DUAL");
while($row = mysql_fetch_assoc($result)) {
echo $row['_message'];
}
Rohit J
  • 11
  • 2