[SOLVED] MySQL error on a simple query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

[SOLVED] MySQL error on a simple query

Post 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
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Did you mean:

$query = mysql_query("SELECT 'id' FROM 'clients' WHERE id = '1'");
print ("<br />query done");

Or something to that effect?
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post 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
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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.
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post 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 :)
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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...
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post 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.. :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post 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
Post Reply