Page 1 of 1
[SOLVED] MySQL error on a simple query
Posted: Tue Dec 02, 2003 9:39 am
by Draco_03
Hi i have this very straight forward code but i have an error
Code: Select all
<?php
$connect = mysql_connect($host, $user, $pswd) or die("Could not connect: " . mysql_error());
print ("Connected successfully");
mysql_select_db("tech support");
print ("<br />We'r on tech support now ");
$query = mysql_query("SELECT 'id' FROM 'clients' WHERE 1");
print ("<br />query done");
$num_rows = mysql_num_rows($query);
print ("<br />num_rows is --> $num_rows");
mysql_close($connect);
print ("<br />Connection over");
?>
but when i run my code that what s i get
Connected successfully
We'r on tech support now

query done
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in d:\program files\easyphp\www\tech_support\db.php on line 25
num_rows is -->
Connection over
....i can't get it...
thx for help
Posted: Tue Dec 02, 2003 9:47 am
by microthick
Did you mean:
$query = mysql_query("SELECT 'id' FROM 'clients' WHERE id = '1'");
print ("<br />query done");
Or something to that effect?
Posted: Tue Dec 02, 2003 9:58 am
by Draco_03
I tried it but no it doesn't work..
what s bizzar it s i can connect
i can choose my DB
i can call a query
but when i try to get info (num_row for exemple)
it give me the error...
i think it might be something i have to setup (permission or something maybe)
i m pretty new to phpmyadmin
Posted: Tue Dec 02, 2003 10:08 am
by infolock
just cleaning up the code :
Code: Select all
<?php
$connect = mysql_connect($host, $user, $pswd) or die("Could not connect: " . mysql_error());
print ("Connected successfully");
mysql_select_db('tech support') or die(MySQL_Error());
print ("<br />We'r on tech support now ");
$sql = "SELECT id FROM clients WHERE id = '1'";
$result = mysql_query($sql) or die(MySQL_Error());
print ("<br />query done");
$num_rows = mysql_num_rows($result);
print ("<br />num_rows is --> $num_rows");
mysql_close($connect);
print ("<br />Connection over");
?>
anyways, if this doesn't work then you don't have your table setup correctly, or you aren't connecting to the db. try those error codes i put in the code and see if you get anything back.
Posted: Tue Dec 02, 2003 10:15 am
by Draco_03
It worked.. well i had to change tech support for tech_support
but it did work..
can you explain why ?
and i also changed WHERE id='1' for 0 since i only have one row..
but i can see why my code didn t work..
Btw Thank you very much

Posted: Tue Dec 02, 2003 10:23 am
by infolock
It worked.. well i had to change tech support for tech_support
but it did work..
? i don't think i understand.. do you mean you changed
mysql_select_db('tech support')
to
mysql_select_db('tech_support')
?
if so, it's because you can't have spaces for a db name and you had to create it like that when you first created the db...
Posted: Tue Dec 02, 2003 11:29 am
by Draco_03
hehe i did not express myself properly
i know in the code i submitted i did have a space.. but i changed it on my computer..but it still didn t work...
so basically it s was irrelevent for my to pin point the change..

Posted: Tue Dec 02, 2003 11:37 am
by Weirdan
Table and field names should be enclosed in backticks (
`), not in single quotes (
').
MySQL manual wrote:
Note that the rules changed starting with MySQL Version 3.23.6 when we introduced quoting of identifiers (database, table, and column names) with `.
" will also work to quote identifiers if you run in ANSI mode. See section 1.7.2 Running MySQL in ANSI Mode.
Posted: Tue Dec 02, 2003 12:28 pm
by Draco_03
ah okay..
i guessit s just a matter of the "way" of writting the code..
thx to you all guys you really were helpfull