Jump to content

Recommended Posts

Is it possible to select multiple rows from a db.

I have vars $1, $2....$5.

 

Is it possible to SELECT something FROM dbname WHERE `id`='$1'..... but do it for all five?

 

Please help,

Thanks,

Sam.

 

 

that is impossible for anyone to have php variables $1, $2,$3 because a variable can not start with a number in php

Yes you would do it just like that

 

mysql_query("SELECT * FROM table WHERE id='1', id='2', id='3', id='4', id='5'");

 

That will produce MySQL syntax error. Use the 'IN' keyword. Example

 

. . . WHERE id IN (1, 2, 3, 4, 5)

 

or this method:

 

. . . WHERE id = 1 OR id = 2 OR id = 3 OR id = 4 OR id = 5

 

See manual for syntax:

 

http://dev.mysql.com/doc/refman/5.0/en/select.html

 

 

cooldude832 is generally correct. However, there are ways to have a variable name with a number (but not recommended). The $1 syntax will not work, but this ${1} will.

 

It is possible if they have a column ID in the DB that is auto-increment and they want to select from those certain rows.

 

No. Not at least in MySQL syntax. Commas will not work like that in the "WHERE" clause.

 

http://dev.mysql.com/doc/refman/5.0/en/select.html

It is possible if they have a column ID in the DB that is auto-increment and they want to select from those certain rows.

 

No. Not at least in MySQL syntax. Commas will not work like that in the "WHERE" clause.

 

http://dev.mysql.com/doc/refman/5.0/en/select.html

 

 

you both seem a bit off I will try and sort this.

 

 

Mysql has databases in which tables live. 

The database is just like a open space iit can't hold any raw data, that data has to live inside of the tables.

In the tables a single field is assigned the "primary key" that being the primary key gives a uniqueness value to that row of data to differentiate it from another row.

 

This primary key can be an integer that is auto-incremented, this is very common.

A database can not have a "primary key" because it can not have rows.

 

So in short yes you can have a field in a table called some sort of "id" that is auto-incremented, but mysql can not magically tell you that value, you have to explain to it which one of those  auto-incremented "ids" you wish to retrieve (or which ones)

you both seem a bit off I will try and sort this.

 

cooldude832, some or rather most members on this forum tend to say DB (or database) and they really mean tables. But it's good you clarified it for the poster.

 

I'm simply trying to correct the syntax. Read my previous posts.

 

Thx.

 

 

Yes you can use commas between values, I do it all the time with no problem.

 

No, you can't in MySQL. You may thing it works but I bet it's not.

 

You're probably not checking if the query worked before executing subsequent commands (such as fetching).

 

 

You said "will produce MySQL syntax error" I don't get any syntax error and I have even seen it recommend many times and it works for me like a dream with no problems.

 

Here is one example where I use them and I have no problems

 

mysql_query("UPDATE users SET displayname = '$displayname', displaytype = '$displaytype', description = '$description', address = '$address', address2 = '$address2', city = '$city', state = '$state', postal = '$postal', country = '$country', website = '$website' WHERE email='$email'");

I don't see you producing an output of said mysql syntax error

 

you can suppress errors from displaying proper debugging query structure is

<?php
$q = "Select this from `that` where this=that";
$r = mysql_query($q) or die(mysql_error()."<br /><br />".$q);
?>

 

That will show a mysql error if the query is capable of producing the above stated error.

You said "will produce MySQL syntax error" I don't get any syntax error and I have even seen it recommend many times and it works for me like a dream with no problems.

 

Here is one example where I use them and I have no problems

 

mysql_query("UPDATE users SET displayname = '$displayname', displaytype = '$displaytype', description = '$description', address = '$address', address2 = '$address2', city = '$city', state = '$state', postal = '$postal', country = '$country', website = '$website' WHERE email='$email'");

 

That is an UPDATE and those commas are not in the "WHERE" clause. That is the correct syntax for the UPDATE.

 

But we're not talking about UPDATE in this topic.  As stated earlier, the SELECT does not allow commas in the "WHERE" clause like the way you show in your first post here. Glad that's sorted out now.

 

:)

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.