Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Simple query syntax

Codes here !

Moderators: macek, egami, gesf

Simple query syntax

Postby eribla » Thu Aug 30, 2012 5:49 pm

Hi

new to the forum and new to PHP, although not to SQL or programming.


I am about to kill myself over a simple query (my first one though):

I can access and query through phpadmin:
SELECT * FROM `Visitor` WHERE First_Name LIKE '%ri%'
give a perfect result.

But any attempt to get a result through php in a website has been without success (except the connection to the database (that works).

Here is the php code from the file on the web site:

$query = "SELECT * FROM 'visitor' WHERE First_Name Like '%ri%'";

echo "<p>Query: ".$query."</p>";
$result = $db->query($query) or die("A MySQL error has occurred.<br />Your Query: " . $query. "<br /> Error: (" . $db->errno . ") " . $db->error );


and the output is:

Query: SELECT * FROM 'visitor' WHERE First_Name Like '%ri%'

A MySQL error has occurred.
Your Query: SELECT * FROM 'visitor' WHERE First_Name Like '%ri%'
Error: (1064) 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 ''visitor' WHERE First_Name Like '%ri%'' at line 1



A slight variation (without the ' ) will give this:

Query: SELECT * FROM visitor WHERE First_Name Like '%ri%'

A MySQL error has occurred.
Your Query: SELECT * FROM visitor WHERE First_Name Like '%ri%'
Error: (1146) Table 'matchmyo_main.visitor' doesn't exist




I am sure I am doing something very stupid, but everybody who has attempted to learn a new language
will know the feeling.

Thanks for your help!
eribla
New php-forum User
New php-forum User
 
Posts: 2
Joined: Thu Aug 30, 2012 5:23 pm

Re: Simple query syntax

Postby johnj » Thu Aug 30, 2012 9:24 pm

Visitor and visitor - Is there a difference between the two?. Use phpmyadmin to see whether the table name is starting with a lower or upper case.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Simple query syntax

Postby freshnet » Fri Aug 31, 2012 4:34 am

agree with John - your second query looks right. You can't use ' with table names, those are for quoted strings only. You can use ` with table names, although I generally don't. The table name is probably Visitor.
User avatar
freshnet
php-forum Active User
php-forum Active User
 
Posts: 252
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: Simple query syntax

Postby eribla » Sat Sep 01, 2012 8:56 am

Thanks Guys

since when are SQL statement case sensitive?



I could have sworn that I tried that before, but maybe I changed two things at the same time...

Thanks again
eribla
New php-forum User
New php-forum User
 
Posts: 2
Joined: Thu Aug 30, 2012 5:23 pm

Re: Simple query syntax

Postby freshnet » Sun Sep 02, 2012 2:50 am

Object names are case sensitive, in MYSQL at least. Keywords aren't case sensitive, i.e. SELECT = Select = SeLeCt. Strings can be case sensitive depending on the platform.
User avatar
freshnet
php-forum Active User
php-forum Active User
 
Posts: 252
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada


Return to mySQL & php coding

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron