Regarding this line
database::query("DELETE FROM bo WHERE name='{$this->_protected_arr[a1]}' AND email='$_SESSION[email]'");
How can I update it so that it only deletes one row instead of all of them.
Also, the syntax looks off, is there any way not to use the {}.
Also, normally PHP calls warnings if ther are no apostrohpes in associative array but it does not pick it up in this case. So this seems wrong as well.
So this is actually 3 questions.
How can I update it so that it only deletes one row instead of all of them.
Use a LIMIT 1 clause at the end of the query.
the syntax looks off
It's correct. It's what PHP calls the "complex (curly) syntax" for variable parsing in strings:
http://php.net/manual/en/language.types.string.php#language.types.string.parsing.complex
You can of course use string concatenation there if you prefer.
You should use LIMIT 1. The question is which row will be caught in LIMIT 1 selection :)
The best case scenario is that your database::query supports placeholders.
Concerning curly brakets, just add apostrophes to the array key
database::query("DELETE FROM bo WHERE name='{$this->_protected_arr['a1']}' AND email='{$_SESSION['email']}'");
If you don't wanna use curly brakets, then just concatenate strings like "DELETE ...".$this->property." AND ...".
database::query("DELETE FROM bo WHERE name='{$this->_protected_arr[a1]}' AND email='$_SESSION[email]' LIMIT 1");
As everyone else suggested, adding a LIMIT 1 to the end of your query will make it so that only one row is deleted. However, given you're using static classes here, I'm guessing you know enough to understand why everything in a database should have its own unique identifier and I'm curious why you aren't using that as part of your criteria for determining what you delete? Or there should at least be some other comparison you can do, like running a select statement that pulls all the rows and then loops over the results, setting them to a multidimensional array where you can then compare the column values for the multiple rows and use that comparison to decide which row to keep. If you're getting multiple entries maybe look into preventing users from double-clicking by using jquery to hide the button or gray it out as soon as it's been clicked. Just some thoughts...
Related
i have a question about searching in MySQL.I couldn't find answer for long time.
I use symfony 3.1 and i have the next situation:
I have column site_languages (longtext, (DC2Type:simple_array) ) in sql with values (for example) "0,1,7", "11,15,27" etc
So my question is:
how i can select row by site_languages which include in array a search value?
I tried use LIKE, checked REGEXP but for example if i need search by value "1" it will return rows with 11/51/111 etc too.I was thinking about keep values like "[1],[15]" But i think there is exist easier and right solution for it?
If you'd like to solve this issue by using a regex you can use this regex (^|,)1(,|$) to match a 1 that is preceded by a comma and followed by a comma OR the 1 may be preceded by the beginning of the line or end of the line.
You should consider normalizing your DB table but if you insist to keep it this way you can save it as json then retrieve all rows and iterate (bad idea) but is ok if it is a short table
I'm not sure that I have the terminology correct but basically I have a website where members can subscribe to topics that they like and their details go into a 'favorites' table. Then when there is an update made to that topic I want each member to be sent a notification.
What I have so far is:
$update_topic_sql = "UPDATE topics SET ...My Code Here...";
$update_topic_res = mysqli_query($con, $update_topic_sql)or die(mysqli_error());
$get_favs_sql = "SELECT member FROM favourites WHERE topic = '$topic'";
$get_favs_res = mysqli_query($con, $get_favs_sql);
//Here I Need to update the Members selected above and enter them into a notes table
$insert_note_sql = "INSERT INTO notes ....";
Does anyone know how this can be achieved?
Ok, so we've got our result set of users. Now, I'm going to assume from the nature of the question that you may be a bit of a newcomer to either PHP, SQL(MySQL in this case), web development, or all of the above.
Your question part 1:
I have no idea how to create an array
This is easier than what you may think, and if you've already tried this then I apologize, I don't want to insult your intelligence. :)
Getting an array from a mysqli query is just a matter of a function call and a loop. When you ran your select query and saved the return value to a variable, you stored a mysqli result set. The mysqli library supports both procedural and object oriented styles, so since you're using the procedural method, so will I.
You've got your result set
$get_favs_res = mysqli_query($con, $get_favs_sql);
Now we need an array! At this point we need to think about exactly what our array should be of, and what we need to do with the contents of the request. You've stated that you want to make an array out of the results of the SELECT query
For the purposes of example, I'm going to assume that the "member" field you've returned is an ID of some sort, and therefore a numeric type, probably of type integer. I also don't know what your tables look like, so I'll be making some assumptions there too.
Method 1
//perform the operations needed on a per row basis
while($row = mysqli_fetch_assoc($get_favs_res)){
echo $row['member'];
}
Method 2
//instead of having to do all operations inside the loop, just make one big array out of the result set
$memberArr = array();
while($row = mysqli_fetch_assoc($get_favs_res)){
$memberArr[] = $row;
}
So what did we do there? Let's start from the beginning to give you an idea of how the array is actually being generated. First, the conditional in the while loop. We're setting a variable as the loop condition? Yup! And why is that? Because when PHP (and a lot of other languages) sets that variable, the conditional will check against the value of the variable for true or false.
Ok, so how does it get set to false? Remember, any non boolean false, non null, non 0 (assuming no type checking) resolves to true when it's assigned to something (again, no type checking).
The function returns one row at a time in the format of an associative array (hence the _assoc suffix). The keys to that associative array are simply the names of the columns from the row. So, in your case, there will be one value in the row array with the name "member". Each time mysqli_fetch_assoc() is called with your result set, a pointer is pointed to the next result in the set (it's an ordered set) and the process repeats itself. You essentially get a new array each time the loop iterates, and the pointer goes to the next result too. Eventually, the pointer will hit the end of the result set, in which case the function will return a NULL. Once the conditional picks up on that NULL, it'll exit.
In the second example, we're doing the exact same thing as the first. Grabbing an associative array for each row, but we're doing something a little differently. We're constructing a two dimensional array, or nested array, of rows. In this way, we can create a numerically indexed array of associative arrays. What have we done? Stored all the rows in one big array! So doing things like
$memberArr[0]['member'];//will return the id of the first member returned
$memberArr[1]['member'];//will return the id of the second member returned
$lastIndex = sizeof($memberArr-1);
$memberArr[$lastIndex]['member'];//will return the id of the last member returned.
Nifty!!!
That's all it takes to make your array. If you choose either method and do a print_r($row) (method 1) or print_r($memberArr) (method 2) you'll see what I'm talking about.
You question part 2:
Here I Need to update the Members selected above and enter them into a notes table
This is where things can get kind of murky and crazy. If you followed method 1 above, you'd pretty much have to call
mysqli_query("INSERT INTO notes VALUES($row['member']);
for each iteration of the loop. That'll work, but if you've got 10000 members, that's 10000 inserts into your table, kinda crap if you ask me!
If you follow method two above, you have an advantage. You have a useful data structure (that two dim array) that you can then further process to get better performance and make fewer queries. However, even from that point you've got some challenges, even with our new processing friendly array.
The first thing you can do, and this is fine for a small set of users, is use a multi-insert. This just involves some simple string building (which in and of itself can pose some issues, so don't rely on this all the time) We're gonna build a SQL query string to insert everything using our results. A multi insert query in MySQL is just like a normal INSERT except for one different: INSERT INTO notes VALUES (1),(2),(x)
Basically, for each row you are inserted, you separate the value set, that set delineated by (), with a comma.
$query = 'INSERT INTO notes VALUES ';
//now we need to iterate over our array. You have your choice of loops, but since it's numerically indexed, just go with a simple for loop
$numMembers = sizeof($memberArr);
for($i = 0; $i < $numMembers; $i++){
if($i > 0){
$query .= ",({$membersArr[$i]['member']})";//all but the first row need a comma
}
else {
$query .= "({$membersArr[$i]['member']})";//first row does not need a comma
}
}
mysqli_query($query);//add all the rows.
Doesn't matter how many rows you need to add, this will add them. However, this is still going to be a costly way to do things, and if you think your sets are going to be huge, don't use it. You're going to end up with a huge string, TONS of string concats, and an enormous query to run.
However, given the above, you can do what you're trying to do.
NOTE: These are grossly simplified ways of doing things, but I do it this way because I want you to get the fundamentals down before trying something that's going to be way more advanced. Someone is probably going to comment on this answer without reading this note telling me I don't know what I'm doing for going about this so dumbly. Remember, these are just the basics, and in no way reflect industrial strength techniques.
If you're curious about other ways of generating arrays from a mysqli result set:
The one I used above
An even easier way to make your big array but I wanted to show you the basic way of doing things before giving you the shortcuts. This is also one of those functions you shouldn't use much anyway.
Single row as associative(as bove), numeric, or both.
Some folks recommend using loadfiles for SQL as they are faster than inserts (meaning you would dump out your data to a file, and use a load query instead of running inserts)
Another method you can use with MySQL is as mentioned above by using INSERT ... SELECT
But that's a bit more of an advanced topic, since it's not the kind of query you'd see someone making a lot. Feel free to read the docs and give it a try!
I hope this at least begins to solve your problem. If I didn't cover something, something didn't make sense, or I didn't your question fully, please drop me a line and I'll do whatever I can to fix it for you.
Quite simply, is such a query/statement inefficient or bad?
<?
$strSql="SELECT * FROM clients, projects
WHERE clients.clientID = $intClientId
AND projects.clientID=$intClientId LIMIT 1";
$objResult=mysql_query($strSql);
if(mysql_num_rows($objResult)==0) {
echo("No data"); }
while ($arrRow=mysql_fetch_array($objResult))
{
?>
<h1>Sub Project(s) for: <span><?=$arrRow[clientName]?></span></h1>
<?
} ?>
In general, you should avoid using SELECT * and select only the fields you need unless absolutely necessary. Whether or not this is efficient depends on how your tables are indexed. I assume in this case that that clientID is the primary key of the clients table. If you have an index on clientID in the projects table, this query should be quite fast.
There are several things that are off here:
As Michael Mior mentions, you should avoid SELECT *. This can be an efficiency issue. It can also break applications in some cases if your application makes assumptions about the columns that are in the table and then the table changes in the database.
You have a LIMIT 1 in your query, but then you loop over the results. This doesn't make sense because LIMIT 1 means you'll only get one row of results, regardless of how many rows matched your query.
You are not escaping your inputs. This may be OK in this case if, in earlier code, you have already verified that those variables definitely contain integer values. I generally just use prepared statements and avoid this problem altogether.
And i also want to add one little advice: Try not to use php shorttags(Use <?php echo instead of <?=) from now. Since PHP6 it will not be supported and it might create code errors and other difficulties for you in the future.
When writing, or should I say developing new features, one should always debug and run a performance profile on query to ensure that you'r using proper index(s). Always use EXPLAIN (or EXPLAIN EXTENDED if your into specifics) on a query to determine its performance.
EXPLAIN SELECT * FROM clients, projects WHERE clients.clientID = 1 AND projects.clientID = 1
I noted also that your doing a while() loop, which is unnecessary if your are only fetching a single row.
I prefer writing projects.clientID=clients.clientID so to be more easy to see how the tables are joined.
The other thing I will avoid (mostly for clarity) is the while loop. Since you expect one record, no need to loop over the result set.
And finally, it is not a good idea to use the short tags.
I have a mysql statement which grabs just one record based on an id. I want to know if using mysql_fetch_row or something along the lines of that is better then using something such as mysql_fetch_array, for just grabbing one row in a database.
It may seem like common sense to go with the mysql_fetch_row but I just want to make sure.
An important thing to note is that
using mysql_fetch_array() is not
significantly slower than using
mysql_fetch_row(), while it provides a
significant added value.
Source
why don't you just use LIMIT 1 inside the mysql statement and no matter what you will have one row.
The two functions are almost identical, the name is just making you think it's different:
$r=mysql_query('SELECT name,email FROM tbl LIMIT 1');
var_dump(mysql_fetch_assoc($r));
// array('name'=>[dbName], 'email'=>[dbEmail]);
var_dump(mysql_fetch_row($r));
// array([dbName], [dbEmail]);
// It's a normal incremental integer index array
var_dump(mysql_fetch_array($r,MYSQL_ASSOC));
// same as mysql_fetch_assoc();
var_dump(mysql_fetch_array($r,MYSQL_NUM));
// same as mysql_fetch_row();
var_dump(mysql_fetch_array($r,MYSQL_BOTH));
// array(0=>[dbName], 1=>[dbEmail], 'name'=>[dbName], 'email'=>[dbEmail]);
The manual also suggests performance wise the two functions fared well. Local tests hint in the same direction. I'd suggest you not worry about the .0001 time/memory saved and find true bottlenecks in your applications.
This is how the query looks like: (simplified)
SELECT * FROM posts WHERE user='37' ORDER BY date DESC
I currently only ave one row in that table, but still for some reason it returns two rows that are exactly the same. At first i thought i messed up with the loop, but i tried printing the returned array out with print_r() and it actually returns two rows.
I tried searching, but i didn't find any similar issues. I do however remember that a friend of mine had the same issue at school, so i'm sure we aint the only ones. I probably just didn't use the right search terms, heh.
If you have only one record (verify this), it has to be application logic that is duplicating the returned values.
Are you sure you only have one row in the table? If so, it seems like the problem must be happening outside of SQL.
What are you doing outside of this query? That seems like the likely source of the issue. You mention a loop: could you be adding the query result to your array twice? Or is the array persisting between calls without being reinitialized (in other words, the result of a previous query is remaining in the array when you don't expect it to)?
limit 1 is your friend :)
Try adding it to the end of your query.