Web Hosting Talk







View Full Version : mySQL Query Wont Work


DanMerc
01-09-2005, 09:28 AM
OK, I have been trying this for hours and it just wont work for me.

To start off, I am trying to grab the value '93' from this table...

http://www.pixelradio.net/external/table.gif

The server I'm using is running MySQL 4.0.22-standard, with phpMyAdmin 2.6.0-pl3.

-----------------------------------------------------------------------

OK, the script has successfully made a connection to the server and selected the database.

I then try...

$count = mysql_query("SELECT value FROM values WHERE name='swearcount'") or die(mysql_error());
print($count);

... but get the error:
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'values WHERE name='swearcount'' at line 1

I go into phpMyAdmin, manually run the query, and it works fine, and I click the "Create PHP Code" link, and it gives me...

Create PHP Code
SQL-query:
$sql = 'SELECT * FROM `values` LIMIT 0, 30';

I then try that...

$sql = 'SELECT value FROM `values` WHERE (`name` = ''swearcount'') LIMIT 0, 30';
$count = mysql_query($sql) or die(mysql_error());
print($count);

...and get:
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/pr/public_html/sandbox/php/addswear2.php on line 3

So I then try:

$sql = 'SELECT value FROM `values` WHERE `name` = \'swearcount\'';
$count = mysql_query($sql) or die(mysql_error());
print($count);

...and get:
Resource id #3

------------------------------------------------------------------

Can anyone tell me what is wrong and what I should do to fix this? I never thought getting one number from a database could be so hard.

Thanks in advance,
Dan.

zoid
01-09-2005, 10:07 AM
VALUES is a reserved word for MySQL, hence you should rename your table to something else, however the following might also work
$count = mysql_query('SELECT value FROM `values` WHERE name="swearcount"') or die(mysql_error());
print(mysql_result($count,0));

CyberAlien
01-09-2005, 10:08 AM
$sql = 'SELECT `value` FROM `values` WHERE `name` = \'swearcount\'';
$result = mysql_query($sql) or die('Error: ' . mysql_error());
$row = mysql_fetch_assoc($result);
if($row === false) die('Row not found');
mysql_free_result($result);
echo $row['value'];

DanMerc
01-09-2005, 10:19 AM
Thank you very much CyberAlien! That code worked.

I never thought of that Zoid, so I have renamed the table and "value" field.

Even after renaming all the values, I updated my query...
mysql_query('SELECT data FROM `vals` WHERE name="swearcount"'); and it still doesn't work! It still returns "Resource id #3" - I just don't think this version of mySQL likes that query.

Once again, thank you both for your help. :D

zoid
01-09-2005, 10:26 AM
Originally posted by DanMerc
Even after renaming all the values, I updated my query...
mysql_query('SELECT data FROM `vals` WHERE name="swearcount"'); and it still doesn't work! It still returns "Resource id #3" - I just don't think this version of mySQL likes that query.

Once again, thank you both for your help. :D
To be honest your MySQL is even in love with this query ;)

Seriously, you are getting this output, because you print $count which holds the result of mysql_query(). Now this function does not return any data but only a link/reference to a result set which then actually contains the data. You have to retrieve it with with mysql_result or preferable with any of the mysql_fetch_* functions.

DanMerc
01-09-2005, 10:34 AM
Right... I think I understand :eek:

So when I do a query with mysql_query(); it gives a reference to the data. PHP can't read that and echo it, unless I use mysql_result(); or something similar to retrieve the actual data itself, and then print it out. :cartman:

zoid
01-09-2005, 10:51 AM
Exactly. It simply cant do it because it doesnt know what MySQL returns. One row, multiple rows, just one column, multiple columns, ...... it all depends on your query, which columns you asked for, which data MySQL could return, etc. ...... hence mysql_query() only returns the link which can then be used to retrieve the data in the desired format.

DanMerc
01-09-2005, 10:52 AM
Wow, I understood something! Yay! :D