Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
MYSQL query isnt working
Old 04-26-2007, 09:22 AM MYSQL query isnt working
SpottyDog's Avatar
Skilled Talker

Posts: 82
Trades: 0
PHP Code:
$query  "SELECT * FROM `images` WHERE `imageid`= $imageid";
$result mysql_query($query) or die(mysql_error());
while ( 
$row mysql_fetch_array($result) ) : 
  
//Check to see if this is 5th image and if so, close the table row and reset the count to 1 

if ($count==5){ 
 echo(
'<TD><a href="javascript:popUp('."popupimage.php".')"><img src="$row->imageid/$row->imagename"</a><br>'); 
 echo(
'</TD></TR><TR>'); 
    
$count=1
  
//If this is not the 5th image, add the image in the table and add 1 to the count 
  
}else{ 
 echo(
'<TD><a href="javascript:popUp('."popupimage.php".')"><img src="$row->imageid/$row->imagename"</a><br>'); 
 echo(
'</TD>'); 
    
$count++; 
  } 
endwhile; 
Can someobdy please tell me what is worng with this.

My error is
Code:
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 'f1b4de0d' at line 1
f1b4de0d is the value of $imageid


The query works fine withour the where condition and just turns up all the images in the table.

Last edited by SpottyDog; 04-26-2007 at 09:24 AM..
SpottyDog is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-26-2007, 10:18 AM Re: MYSQL query isnt working
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
$imageid is a string so needs single quotes around it
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-26-2007, 11:42 AM Re: MYSQL query isnt working
SpottyDog's Avatar
Skilled Talker

Posts: 82
Trades: 0
cheers chrishirst, you save my *** once again. just isnt my week
SpottyDog is offline
Reply With Quote
View Public Profile
 
Old 04-26-2007, 12:43 PM Re: MYSQL query isnt working
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
sometimes it just takes a different set of eyes
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-29-2007, 08:01 AM Re: MYSQL query isnt working
Extreme Talker

Posts: 214
Trades: 0
Always use quotes around your $php_variables you put in your MySQL query.
kaisellgren is offline
Reply With Quote
View Public Profile
 
Old 04-29-2007, 10:53 AM Re: MYSQL query isnt working
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Originally Posted by kaisellgren View Post
Always use quotes around your $php_variables you put in your MySQL query.
Not always

Only when it is a string value.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-29-2007, 02:37 PM Re: MYSQL query isnt working
Extreme Talker

Posts: 214
Trades: 0
Quote:
Originally Posted by chrishirst View Post
Not always

Only when it is a string value.
For beginners (as the topic author is I assume), it is a good practice to always use quotes regardsless of value type. This works fine for integers, etc. too.
kaisellgren is offline
Reply With Quote
View Public Profile
 
Old 04-30-2007, 06:40 AM Re: MYSQL query isnt working
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
I disagree,
Beginners especially should be learning the correct way to set the syntax of queries, NOT using some all encompassing method of avoiding errors.

Slightly off topic but nonetheless relevant is this, from Coding Horror
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-30-2007, 07:39 AM Re: MYSQL query isnt working
Extreme Talker

Posts: 214
Trades: 0
Quote:
Originally Posted by chrishirst View Post
I disagree,
Beginners especially should be learning the correct way to set the syntax of queries, NOT using some all encompassing method of avoiding errors.

Slightly off topic but nonetheless relevant is this, from Coding Horror
So if I was a beginner, I would do it this way?

PHP Code:
<?php

$value 
$_POST['value'];
if (
get_type($value) == 'string')
 
$value "'$value'";
mysql_query("INSERT INTO table (a) VALUES ($value);");

?>
If the data type can be anything, why would I check for if it is a string and then insert quotes around it? Would be much easier to just every time add quotes.

WHERE a = '5';. That finds all rows which field a equals to 5 no matter is the field numeric or string.

EDIT: What errors are you talking about?

Last edited by kaisellgren; 04-30-2007 at 07:42 AM..
kaisellgren is offline
Reply With Quote
View Public Profile
 
Old 04-30-2007, 03:49 PM Re: MYSQL query isnt working
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
But the data type cannot and should not be anything.

It should match whatever the column type is that it is being inserted into. Just adding quotes around anything "just in case" so you avoid the server throwing a syntax error is very poor programming practice.

Quote:
So if I was a beginner, I would do it this way
No you certainly wouldn't!

You would know what values and what types are to be inserted in to the SQL query and cast or convert the user supplied data into those types.
Failure to check types and values and/or allowing almost anything to be fed into the query opens up your code to SQL injection abuse and all manners of potential data errors.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-30-2007, 05:40 PM Re: MYSQL query isnt working
Extreme Talker

Posts: 214
Trades: 0
Quote:
Originally Posted by chrishirst View Post
But the data type cannot and should not be anything.

It should match whatever the column type is that it is being inserted into. Just adding quotes around anything "just in case" so you avoid the server throwing a syntax error is very poor programming practice.

No you certainly wouldn't!

You would know what values and what types are to be inserted in to the SQL query and cast or convert the user supplied data into those types.
Failure to check types and values and/or allowing almost anything to be fed into the query opens up your code to SQL injection abuse and all manners of potential data errors.
Let me explain this.

I have a field in MySQL:

username VARCHAR(255)

So the username field has some text that contains max 255 characters. The size of the field is always text length bytes + 1 byte.

If I have a form that has field username, and visitor enters 43534534 in the username field it would become:

mysql_query("SELECT a FROM b WHERE username = '43534534';");

That works fine, MySQL will convert your integer to string that will work with our varchar field. Try to debug, zero errors.

PS. I love to argue with you. I always argue

EDIT: If you still don't believe in me. Tell me why phpBB enters data into MySQL without checking data type? Because it does not have to check it, data will be converted to string in all cases. You understand that putting single quotes around $variable will become always a string if we successfully protect against injections. In some cases injections can be harder to protect like in GBK, 0xbf27 is not a valid multi-byte character, but 0xbf5c is. Interpreted as single-byte characters, 0xbf27 is 0xbf (¿) followed by 0x27 ('), and 0xbf5c is 0xbf (¿) followed by 0x5c (\). If I want to attempt an SQL injection attack against a MySQL database, having single quotes escaped with a backslash is a bummer. If you're using addslashes(), however, I'm in luck. All I need to do is inject something like 0xbf27, and addslashes() modifies this to become 0xbf5c27, a valid multi-byte character followed by a single quote. In other words, I can successfully inject a single quote despite your escaping. I a kind of know what you think, but I think you are thinking totally different thing what I am thinking. I actually don't even care what are you trying to claim, I will always follow other scripts made by professional coders like IPS - that way I am 100% sure there is nothing going wrong. And actually nothing has ever gone wrong. My word 'ever' consists of many years.

If I have a MySQL field

userid INT(11) UNSIGNED NOT NULL AUTO_INCREMENT

I know the (11) does nothing but clears the sentence. Anyway, then we need to check that our inserted data is integer.

$userid = (int) $_GET['userid'];
---> ready to insert into DB.

That is what we need to do. But we can always use numbers as strings at database and then use numbers as numbers in PHP, ASP or whatever we are coding with.

CHRISHIRST: Let me test you, tell me is this professional MySQL coding or not:

time INT(10) UNSIGNED NOT NULL DEFAULT '0',

Let's assume we are creating a table with some columns and the above is one of those columns.

Last edited by kaisellgren; 04-30-2007 at 06:00 PM..
kaisellgren is offline
Reply With Quote
View Public Profile
 
Old 05-05-2007, 06:05 AM Re: MYSQL query isnt working
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
username VARCHAR(255)

So the username field has some text that contains max 255 characters. The size of the field is always text length bytes + 1 byte.

If I have a form that has field username, and visitor enters 43534534 in the username field it would become:

mysql_query("SELECT a FROM b WHERE username = '43534534';");

That works fine, MySQL will convert your integer to string that will work with our varchar field. Try to debug, zero errors.
I don't see your point
username = '43534534' is a string and a VARCHAR column type is a string column.
therefore no errors

Quote:
CHRISHIRST: Let me test you, tell me is this professional MySQL coding or not:

time INT(10) UNSIGNED NOT NULL DEFAULT '0',
It depends on how you define "professional"

Yes it would work and create the column complete with default value, whether it is "good practice" to allow the DB Server to make the choice is debatable.
If and when you are designing code and queries to be cross platform, then you should be explicit with regards to casting and variable/column type.
If you are designing for one platform only and you know that platform and its vagaries well, then you can be less diligent on code interpretation.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-05-2007, 07:30 AM Re: MYSQL query isnt working
Extreme Talker

Posts: 214
Trades: 0
Quote:
Originally Posted by chrishirst View Post
I don't see your point
username = '43534534' is a string and a VARCHAR column type is a string column.
therefore no errors
heh lol?

Quote:
Originally Posted by kaisellgren View Post
Try to debug, zero errors.
kaisellgren is offline
Reply With Quote
View Public Profile
 
Old 05-06-2007, 06:22 AM Re: MYSQL query isnt working
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
why would you want to debug something that is correct???

You have a number surrounded by quotes which makes it a string value, which is being inserted into a VARCHAR column.

Where's the problem???

In the original post (the one where adding quotes solved the problem) the imageid variable type was ambiguous.
That is;
Because it had characters in it that were not in the range 1 .. 9 it could not be handled by the SQL engine as being numeric, but it did not have quotes around it to explicitly declare it as a string.
So the SQL engine throws an exception.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-06-2007, 06:26 AM Re: MYSQL query isnt working
Extreme Talker

Posts: 214
Trades: 0
You just got me wrong, nothing else. Just forget it.
kaisellgren is offline
Reply With Quote
View Public Profile
 
Old 05-06-2007, 06:31 AM Re: MYSQL query isnt working
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
maybe you should explain where I got you wrong, just for future reference.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to MYSQL query isnt working
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.53968 seconds with 12 queries