I'm a fairly newbie php coder, got this php code in a tutorial to create a randomly generated quote, but I don't fully understand the code
IN particular, I don't understand
a) line 4 -- where does the "rows" come from. Is that a name made up on the spot. Could it just as easily have been called "ferrari"
b) line 9 -- where does "storedtext" come from? is it just made up on the spot?
c) based on the code, do you have an idea what the database is supposed to look like. Would it just be a database called "text" with a table called "quotables" in it?
<?
//
// count() gets the number of rows from database text--
//it assigns a number to rowcount
1 $rowcount = mysql_query("select count() as rows from text");
// don't understand what exactly is happening here, where did "rows" come from
2 while ($row = mysql_fetch_assoc($rowcount))
3 {
4 $max = $row["rows"];
5 }
// Selects an item's index at random
6 $rand = rand(1,$max)-1;
//there is a database table called "quotables?" taking one random row
7 $result = mysql_query("select from quotables limit $rand, 1");
8 $row = mysql_fetch_array($result);
//where does "storedText" come from?????
9 $randomOutput = $row['storedText'];
10 echo '<p>' . $randomOutput . '</p>';
11 ?>
where does the "rows" come from?
It is a name assigned to a value in the SQL query select count() as rows from text.
where does "storedtext" come from?
It seems to be the name of a field in the quotables table.
based on the code, do you have an idea what the database is supposed to look like. Would it just be a database called "text" with a table called "quotables" in it?
No. We cannot say anything about the database name. But this database contains the tables text and quotables where the latter has at least the field storedText.
a) Yes.
b) It must be a column in the database. In line 7, it's pulling all columns in the database, but isn't specific about it's names
c) No, you could print_r($row) to see the table structure though (will print out the array, showing all columns). You should also have access to the db (to make this work, you'll need the db and tables set up), so however you mysql_connect() and mysql_select_db() will tell you the name of the host/db.
A) rows is the alias given to the count() function in the mysql query in line 1. If you changed rows in line one to ferrari then changed rows in line 4 to ferrari then it would still work.
B) stored text comes from the second mysql query on line 7. This will be the name of a column from within that table.
C) Based on the code you have given I can tell you that you have a database which I do not know the name of and that database has two tables one called text and the other quotables I can tell you that quotables has one column called storedText.
a) rows came from sql query "select count() as rows from text"
b) I think there "*" in line 7 because there is no column specified, so if * is there then its selecting all the columns in that table and "storedText" is one column in it
c) text,quotables both are tables
a) rows comes from the SQL query:
$rowcount = mysql_query("select count() as rows from text");
It is the name given to the count() column.
b) storedText is a column in the quotables table, probably with the quote in it.
What the script does is, get a row count from the text table.
Get a random number in the range of 1 to $max.
Get the corresponding quote from the quotables table.
The SQL doesn't tell us anything except that quotables is a table and storedText is a column in it. The names in strings show up in the SELECT statement and come into existance when it completes.
"rows" come from mysql table. This is the name of column. For instance, if you have table with 2 columns, and one of them named like "price" you should use $var['price'] after using mysql_fetch_assoc to use it.
'storedText' the same.
ANS - 1
in while loop, we have assigned value to $row...
ANS - 2
If you have looked code carefully, on line no 8, you have again assigned value to $row.
in line no 8 mysql_fetch_array($result) will fetch all the values for 1st row in array format.
there must be one column in "quotables" table named "storedText" so that it is coming in the $row['storedText'].
you should refer php manual toi understand mysql_fetch_array and mysql_fetch_assoc functions..
refer this url : http://be.php.net/manual/en/book.pdo.php
Line 2 is relevant to a)
while ($row = mysql_fetch_assoc($rowcount))
the while-construct will repeat as long as the expression inside the parentheses evaluates to a true-ish value. In PHP; an assignment is also an expression, which evaluates to the value being assigned, i.e. $row. Before each iteration, the function is executed and tries to retrieve the next row from the database. When it fails, it will return false instead of a row, which then in turn make the loop end, because the assignment-expression will evaluate to false, which is the terminating condition for the while-statement.
b) line 9 -- where does "storedtext" come from? is it just made up on the spot?
It comes from a randomly fetched result from the database. It's the value of one column.
c) based on the code, do you have an idea what the database is supposed to look like. Would it just be a database called "text" with a table called "quotables" in it?
We only know that the database has at least two tables text and quotables, the latter of which has at least one column storedText.
What it does
It tries fetching a random quote. However, the logic is not sound and will most likely not work in all instances.
Why is it wrong?
Basically, it assumed that for each row in text, there is exactly one entry in quoteables. If that was so, you would not need two tables in the first place. Because of this, I assume that quoteables can contain any number of rows, possibly even less, in which case you would sometimes not get a single result from the query. This would have your query fail and your script, because you try to access "false["storedText"] so to speak.
A
$rowcount = mysql_query("select count() as rows from text");
// the $rowcount is an array, the lines below get the values (that actually is just one)
// and put into $max variable
while ($row = mysql_fetch_assoc($rowcount))
{
$max = $row["rows"];
}
B
//get the text from database based on randon limit offet
$result = mysql_query("select from quotables limit $rand, 1");
//put the value returned into $row variable
$row = mysql_fetch_array($result);
//get the storedText (that is a table column name), and put the value into $randomOutput
$randomOutput = $row['storedText'];
C:
Based on code I can't afirm what is the database model.
Related
I am hoping someone can help me because I am attempting to do something that is beyond my limits, I don't even know if a function exists for this within PHP or MySQL so my search on google hasn't been very productive.
I am using PHPWord with my PHP/MySql Project, the intention is that I want to create a word document based on a template.
I have used this guide which is also on stack exchange.
However this approach requires that the number of rows and the values are hard coded, i.e. in his example he has used cloneRow('first_name', 3), which then clones the table to have 3 rows, and then goes on to manually define the tags, i.e.
$doc->setValue('first_name#1', 'Jeroen');
$doc->setValue('last_name#1', 'Moors');
$doc->setValue('first_name#2', 'John');
I am trying to make this dynamic, in my instance I am trying to make a timetable, and one of the child tables is exactly that, so the query I have looks up how many entries there are and then collects a count of them, this $count is then used to dynamically create the correct number of rows. This is the count I am using:
$rs10 = CustomQuery("select count(*) as count FROM auditplanevents where AuditModuleFk='" . $result["AuditModulePk"]."'");
$data10 = db_fetch_array($rs10);
$Count = $data10["count"];
I then use this $document->cloneRow('date', $Count); to executive the clonerow function, which works great and my document now looks something like this.
So, so far so good.
What I now want is for a way to then append each row value of the query into the document, so rather than manually setting the tag value i.e. $doc->setValue('first_name#1', 'Jeroen'); I could use something like $doc->setValue('first_name#1', '$name from row 1'); I suspect this will involve a foreach query but not too sure.
I hope the above makes sense, but please feel free to ask me for anything else and become my personal hero. Thanks
Update: Just for sake of clarity, what I would like is for the output to look something like this:
In my example are 5 results and therefore 5 rows created, I want to set values in following way:
${$date1} = date column from query 1st row
${$date2} = date column from query 2nd row
${$date3} = date column from query 3rd row
${$date4} = date column from query 4th row
${$date5} = date column from query 5th row
I was able to sort this out by inserting the records from the query into a temp table, with an AI ID, then using:
//update timetable with events from temp table
$rs14 = CustomQuery("select * FROM tempauditplan where AuditModuleFk='" . $result["AuditModulePk"]."'");
while ($data14 = db_fetch_array($rs14))
{
$document->setValue('date#'.$data14["rowid"], date('d/m/y', strtotime($data14["date"])));
$document->setValue('time#'.$data14["rowid"], date('H:i', strtotime($data14["time"])));
$document->setValue('auditor#'.$data14["rowid"], $data14["auditor"]);
$document->setValue('area#'.$data14["rowid"], $data14["area"]);
$document->setValue('notes#'.$data14["rowid"], $data14["notes"]);
$document->setValue('contact#'.$data14["rowid"], $data14["contact"]);
}
The trick is to also have a function that truncates the table after use so can be used over again
Might not be the most efficient way, but it works!
Please refer to the following image. In this image number (1) is what the field position in my database userdb looks like before I run my code. Number (2) is what change I am expecting in the position field of some rows after I run the code. The value of the position field of second row is primarily 2 what I want to be changed to 3. Similarly, The values of the position field of third, fourth and fifth rows are primarily 3, 4 and 5 respectively what I want to be changed to 4, 5 and 6 respectively. So, I run the following code and in my browser I see what I expect. However, In the database I see wrong data is inserted, like number (3) on the image. Notice that my last expected value of the position field is 6 (for fifth row), but I am getting 6 in all rows!!!
Here is the code I run:
$requestedPosition = 2; $oldPosition = 6;
for($i=$requestedPosition+1; $i <= $oldPosition; $i++){
$query = mysql_query("UPDATE userdb SET position = '$i' WHERE position='{$requestedPosition}' ");
echo 'Old Position: '.$requestedPosition.' is now: '.$i.'<br>';
$requestedPosition++;
}
I think something must be wrong in the mysql query. If I comment out the query I get expected result in browser. However, to insert those data into database I need to run the query but when I do it, I get wrong data inserted in database. I am on windows, running PHP 5.3.13, MySQL 5.5.24.
Most Confusingly, in the for loop, if I change $i=$requestedPosition+1; to $i=$requestedPosition; I get 2,3,4,5 inserted (in 2nd,3rd,4th,5th row respectively) rather than 6,6,6,6. However, I want 3,4,5,6 inserted (in 2nd,3rd,4th,5th row respectively), so I had to use $i=$requestedPosition+1; in for loop, and when I do, Code gets Mad!!! So, do I :( Stuck with this (with bad headache) for last two days!
There's a slight flaw in your logic.
The first time that your loop runs, you're updating one row - you're updating the row with position 2 to be position 3. This is fine.
But the second time the loop runs, you're updating all rows where position is 3 to be position 4 - and there's two of them now, the one from the table, and the one you've just updated. The same happens on the third and fourth iteration.
You can probably do this more elegantly, with one call:
$query = mysql_query("UPDATE userdb SET position = position + 1 WHERE position >={$requestedPosition} AND position <= ${oldPosition}' ");
I've not tested that, but it should at the least give you an idea.
I have the following call to my database to retrieve the last row ID from an AUTO_INCREMENT column, which I use to find the next row ID:
$result = $mysqli->query("SELECT articleid FROM article WHERE articleid=(SELECT MAX(articleid) FROM article)");
$row = $result->fetch_assoc();
$last_article_id = $row["articleid"];
$last_article_id = $last_article_id + 1;
$result->close();
I then use $last_article_id as part of a filename system.
This is working perfectly....until I delete a row meaning the call retrieves an ID further down the order than the one I want.
A example would be:
ID
0
1
2
3
4-(deleted row)
5-(deleted row)
6-(next ID to be used for INSERT call)
I'd like the filename to be something like 6-0.jpg, however the filename ends up being 4-0.jpg as it targets ID 3 + 1 etc...etc...
Any thoughts on how I get the next MySQL row ID when any number of previous rows have been deleted??
You are making a significant error by trying to predict the next auto-increment value. You do not have a choice, if you want your system to scale... you have to either insert the row first, or rename the file later.
This is a classic oversight I see developers make -- you are coding this as if there would only ever be a single user on your site. It is extremely likely that at some point two articles will be created at almost the same time. Both queries will "predict" the same id, both will use the same filename, and one of the files will disappear, one of the table entries may point to the wrong file, and the other entry will reference a file that does not exist. And you'll be scratching your head asking "how did this happen?!"
Predicting auto-increment values is bad practice. Don't do it. Plan for concurrency.
Also, the information_schema tables are not really tables... they are server internals exposed to the SQL interface. Calls to the "tables" table, and show table status are expensive calls that you do not want to make in production... so don't be tempted to use something you find there.
You can use mysql_insert_id() after you insert the new row to retrieve the new key:
$mysqli->query($yourQueryHere);
$newId = $mysqli->insert_id();
That requires the id field to be a primary key, though (I believe).
As for the filename, you could store it in a variable, then do the query, then change the name and then write the file.
The code:
$review = mysql_query("SELECT conceptID, MIN(nextReview) FROM userconcepts WHERE userID='$userID'");
$nrows = mysql_num_rows($review);
echo "$nrows<br />\n";
The query works when the table has such entries and returns the correct column values. However, when the table is empty, as I can confirm right now in HeidiSQL, mysql_num_rows still returns 1, but the column values are empty. (The problem still remains if the table has other values for different userIDs).
I expect this query to return the empty set sometimes during normal operations, and I want to take action based on the existence of a result, but I also want to use the result if it exists. Any idea why this code is not working as I expect it to work (I expect it to return 0 if the table is empty)?
First of all, the query has a very simple problem: you're showing the conceptID field, but not grouping by it. If you want to show a field on a SELECT that uses aggregate functions, you should show it; not doing so is an error, and will make many engines not execute your query.
That aside, whenever you have an aggregate function, and you don't group by anything (i.e., don't add a GROUP BY clause), the result is one row. Regardless of the amount of rows in the table.
The reason why is because when a SQL engine executes a query with only aggregation functions, then it returns one row. So:
select count(*)
from table
where 1 = 2
is going to return 1 row with the value 0. This is the way that all SQL engines work.
Your query is a little different:
select conceptID, MIN(nextReview)
FROM userconcepts
WHERE userID='$userID'"
In most SQL dialects, you would get an error of the from "conceptID not in group by clause" or something like that. That is, the query would have a syntax error.
MySQL supports this. It will return the minimum value of nextReview (from the rows that meet the where condition) along with an arbitrary value of conceptID (from those same rows). In this case, there are no rows, so the values will be set to NULL.
Perhaps, you want one row per conceptId. That query would be:
select conceptID, MIN(nextReview)
FROM userconcepts
WHERE userID='$userID'
group by conceptId
I got some help with gettin the number of rows returned from mysql, and it works fine...
BUT, how do I get the number of rows with a certain field value?
Do I have to make a new Mysql search query?
Here is the code where I query mysql and display in a table using fetch_array... Also, Im using mysql_num_rows to get number of rows.
So how do I get number of rows with certain field value also?
$qry_result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($qry_result);
while($row = mysql_fetch_array($qry_result))
Thanks for all help
OBSERVE: Im trying to avoid using another SELECT WHERE clause...
Is there a way to do what I want withouth another search?
In your query, you can use the where clause. (select * from table where column1 = 'value')
Another option would be to have a counter variable that you increment in your while loop:
$counter = 0;
while($row = mysql_fetch_array($qry_result))
{
if($row[0] == "value")
$counter++;
}
After you have this counter, reset the result set using mysql_data_seek($qry_result, 0); and then continue with your original while loop.
There are several ways to reach this, either run additional queries against MySQL or use programm logic to calculate what you need while iterating over the array.
Fetching the number of rows from MySQL is a task that has several solutions as well. You could blindly call SELECT count(*) FROM table WHERE foo = bar, or use the more advanced SQL_CALC_FOUND_ROWS variable of the database.
If you could explain yourself better, I would be glad to provide a good solution!
OBSERVE: Im trying to avoid using another SELECT WHERE clause... Is there a way to do what I want withouth another search?
I'm curious why you don't want to use another SELECT WHERE clause?
From what I interpret of your question, you are asking to have the number of rows of a given query AND, a count of unique variables?
ex:
NAME AGE
Joe 15
Simon 13
Simon 16
Joe 21
Mary 15
Joe 28
Your row count would be 6 and your count (that you are requesting) would be:
Joe x 3
Simon x 2
Mary x 1
If that is what you are asking, why not use 2 queries, 1 for your set of data, and another query where you GROUP BY 'name' and return only UNIQUE 'name' results? That would get you a count of your "certain fields".
Then again correct me if I miunderstood your question.