The following PHP code:
$dbc = mysql_connect ('localhost','root','abcde');
if (!$dbc) {
die('Not Connected:' . mysql_error ());
}
$db_selected = mysql_select_db ('db_test', $dbc);
if (!$db_selected) {
die ("Can't Connect :" .mysql_error());
}
$query="SELECT * FROM 140423 WHERE GAME='Clash of Clans'";
//add "result"
$result=mysql_query($query);
if (!$query) {
die ("Can't Connect :" .mysql_error());
}
echo $result;
Doesn't return anything.
Should I be using print instead of echo?
Also, if I change
echo $result;
To
echo 'whatever';
it returns "whatever" on my post.
Help?
From the manual:
Identifiers may begin with a digit but unless quoted may not consist solely of digits.
- http://dev.mysql.com/doc/refman/5.1/en/identifiers.html
Which is exactly what you're doing, and should not be doing.
Solution:
Choose a different name for your table, one consisting of letters preferably.
Use backticks around the table name.
Plus, if you wish to view data, you need to use a loop.
I.e.:
$query="SELECT * FROM `140423` WHERE GAME='Clash of Clans'";
while ($row = mysql_fetch_assoc($query))
{
$var = $row["column"]; // the column of your choice
}
echo $var;
Rewrite:
$dbc = mysql_connect ('localhost','root','abcde');
if (!$dbc) {
die('Not Connected:' . mysql_error ());
}
$db_selected = mysql_select_db ('db_test', $dbc);
if (!$db_selected) {
die ("Can't Connect :" .mysql_error());
}
$query="SELECT * FROM `140423` WHERE GAME='Clash of Clans'";
//add "result"
$result=mysql_query($query);
// as pointed out already
if (!$result) {
die ("Can't Connect :" .mysql_error());
}
echo $result; // this is up to you
// if you want to see data from your table, loop through it.
while ($row = mysql_fetch_assoc($query))
{
$var = $row["column"]; // the column of your choice
}
echo $var;
Footnotes:
mysql_*
functions deprecation notice:
http://www.php.net/manual/en/intro.mysql.php
This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.
These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/.
Documentation for MySQL can be found at » http://dev.mysql.com/doc/.
mysql_query returns a resource or false. You need to manipulate the resource with one of the various fetch functions.
Instead of looking if $query
is false, you should be looking if $result
is false.
if (!$result) {
die ("Can't Connect :" .mysql_error());
}
After that
$result=mysql_query($query);
if (!$query) {
die ("Can't Connect :" .mysql_error());
}
Use
while($row = mysql_fetch_array($result)) {
echo $row['YourTableFieldName'];
}
OR use
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// If you want to display all results from the query at once:
print_r($row);
// If you want to display the results one by one
echo $row['YourTableFieldName1'];
echo $row['YourTableFieldName2'];
}