I'm working on a project to further learn php and how it can be used to interface with a mysql database. The project is a forum, with the page in question displaying all the topics in a category. I'd like to know if I am handling my calls efficiently, and if not, how can I structure my queries so they are more efficient? I know its a small point with a website that isn't used outside of testing, but I'd like to get a handle on this early.
<?php
$cid = $_GET['cid'];
$tid = $_GET['tid'];
// starting breadcrumb stuff
$catname = mysql_query("SELECT cat_name FROM categories WHERE id = '".$cid."'");
$rcatname = mysql_fetch_array( $catname );
$topicname = mysql_query("SELECT topic_title FROM topics WHERE id = '".$tid."'");
$rtopicname = mysql_fetch_array( $topicname );
echo "<p style='padding-left:15px;'><a href='/'> Home </a> » <a href='index.php'> Categories </a> » <a href='categories.php?cid=".$cid."'> ".$rcatname['cat_name']."</a> » <a href='#'> ".$rtopicname['topic_title']. "</a></p>";
//end breadcrumb
$sql = "SELECT * FROM topics WHERE cat_id='".$cid."' AND id='".$tid."' LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res) == 1) {
echo "<input type='submit' value='Reply' onClick=\"window.location = 'reply.php?cid=".$cid."&tid=".$tid."'\" />";
echo "<table>";
if ($_SESSION['user_id']) { echo "<thead><tr><th>Author</th><th>Topic » ".$rtopicname['topic_title']."</th></thead><hr />";
} else {
echo "<tr><td colspan='2'><p>Please log in to add your reply.</p><hr /></td></tr>";
}
echo "<tbody>";
while ($row = mysql_fetch_assoc($res)) {
$sql2 = "SELECT * FROM posts WHERE cat_id='".$cid."' AND topic_id='".$tid."'";
$res2 = mysql_query($sql2) or die(mysql_error());
while ($row2 = mysql_fetch_assoc($res2)) {
echo "<tr><td width='200' valign='top'>by ".$row2['post_creator']." <hr /> Posted on:<br />".$row2['post_date']."<hr /></td><td valign='top'>".$row2['post_content']."</td></tr>";
}
$old_views = $row['topic_views'];
$new_views = $old_views + 1;
$sql3 = "UPDATE topics SET topic_views='".$new_views."' WHERE cat_id='".$cid."' AND id='".$tid."' LIMIT 1";
$res3 = mysql_query($sql3) or die(mysql_error());
echo "</tbody></table>";
}
} else {
echo "<p>This topic does not exist.</p>";
}
?>
Thanks guys!
Looks like a classic (n+1) query mistake that could die a latent death. You get a key using one round trip, then you loop over the results to get n values based on it. If the first result set is large you'll have a lot of network round trips.
You could bring it all back in one go with a JOIN and save yourself a lot of network latency.
The statements themselves are fairly simple so there's not much you can do to optimize them further that I know of. However, if you create some business objects and cache the data into them on a single call and then access data from the business objects then it could be faster.
In other words, 1 SQL call for 1,000 rows is going to be much faster than 1,000 calls for a single row.
Here are some of extra things I would do when I write a code like above:
Never use * in SELECT statement when you know the columns you are going to use.
Always use or die(mysql_error()) when executing the query.
Unset the result sets once the result sets has served its purpose.
Use mysql_real_escape_string() to escape the injections when using some substitutions in your queries.
Related
I'm trying to display data from database and it is important to me that this output is placed on different sides of website. I used php to connect to database, and ajax jquery to refresh data because every 20second values change.
I tried to
echo <div styles='position: absolute; top: 0px' class='text'>{$row['id']}</div>
in a foreach loop but when I do this all 6 of my id's are stacked on top each other.
Making <div> outside loop was unsuccessful too. I guess my problem is in reading data from database because I read all at once but I don't know any other way to do this except wrtiting 6 connection files to gather only the one value that I want to display and then styling it, but I feel like there is smarter way of doing this.
This is my code. Just want to say this is my first contact with php.
<?php
$hostname = "someinfo";
$username = "someinfo";
$password = "someinfo";
$db = "someinfo";
$dbconnect = mysqli_connect($hostname,$username, $password,$db) or die("cant");
if ($dbconnect->connect_error) {
die("Database connection failed: " . $dbconnect->connect_error);
}
$sensor_names = array();
$query2 = mysqli_query($dbconnect,"show tables");
while($row2 = mysqli_fetch_array($query2)){
if($row2[0] == 'sensors' or $row2[0] == 'measurments'){
break;
}
else{
array_push($sensor_names,$row2[0]);
}
}
$query = mysqli_query($dbconnect, "select s.id, s.sensor_name, max(dev.id), dev.temprature, dev.date from sensors s, `{$sensor_names[0]}` dev where s.id=dev.sensor_id gro
up by s.id, s.sensor_name order by s.id asc");
while($row = mysqli_fetch_array($query)){ //i konw this is ugly but this is working placeholder
foreach($sensor_names as $sn){
$query = mysqli_query($dbconnect, "select s.id, s.sensor_name, dev.temprature, dev.date from sensors s, `{$sn}` dev where s.id=dev.sensor_id order by dev.id desc limit 1");
$row = mysqli_fetch_array($query);
echo "
{$row['id']}
{$row['sensor_name']}
{$row['temprature']}
{$row['date']}
<br>";
}
}
?>
This is off-the-cuff from a guy who hasn't touched PHP in a long while, so watch for major bugs. But the basic idea is like this: build the code in a variable, and when done, echo out the entire variable. Makes it easier to add the structure/formatting you want. Note that you can also stick in a style tag along with that code and blurp out the style along with the "table" (Personally, I wouldn't use a table for styling, this is just for demo).
Note: I didn't style the output so that it puts the data on either side of the page - I left that for you to do. It's basic HTML - divs, styles, maybe css grid or flexbox. The point is to create your CSS/HTML/PHP mashup in a string variable and output the entire thing when done.
$out = '<style>.cell_id{font-weight:bold;}</style>';
$out .= '<table><tr><th>Label 1</th><th>Label 2</th><th>Etc</th></tr>'; //<=== ADDED!
while($row = mysqli_fetch_array($query)){
foreach($sensor_names as $sn){
$query = mysqli_query($dbconnect, etc. etc. etc.);
$row = mysqli_fetch_array($query);
$out .= "
<tr>
<td class='cell_id'>{$row['id']}</td>
<td>{$row['sensor_name']}</td>
<td>{$row['temprature']}</td>
<td>{$row['date']}</td>
</tr>";
}
}
echo $out;
Ok I think I got it. Cssyphus's answer got me thinking and I wrote something like that array_push($data, $row) and $data is two dimentional array that hold all data I need and now I can style it easily.
Noob alert here. I'm just (trying to) teaching myself PHP because I need it on my internship job. Now I am creating a survey and I am trying to use while loops to show the questions and their respective answers onto my XAMPP. I am using this code:
<?php
mysqli_select_db($conn, "surveyordina");
//code hier schrijven
$sql = "SELECT questions_body FROM survey_questions where subthema_id = 1";
$sql2 = "SELECT answer_body FROM survey_answers where answer_id = 1 or answer_id = 2 or answer_id = 3";
$result = mysqli_query($conn, $sql);
$result2 = mysqli_query($conn, $sql2);
if(mysqli_num_rows($result2) > 0){
while($row = mysqli_fetch_assoc($result)) {
echo "<br>" ."Vraag: <br>" . $row["questions_body"]. "<br>";
while($row_answer = mysqli_fetch_assoc($result2)) {
echo $row_answer["answer_body"]. "<br>";
}
}
}
else{
echo "No results";
}
Now the returning part of this code looks like this:
Vraag:
Is the organization aware of where all the personal data is stored? Be it on-site or in the cloud, hosted by the company or by a third party?
Yes
No
I don't know
Vraag:
Is the organization able to locate and find personal data of a particular data subject?
Vraag:
Does the organization have technology in place to return all personal data on a given data subject, given a single search from personnel?
Vraag:
testerino kekkerino
I am trying to get the Yes, No, and I don't know parts under each and every question but only seem to get it under one of the questions.
Is there a method to return the whole answer part under each question? If so, am I on the right path or am I doing something completely wrong?
Thanks in advance.
Once you hit the end of a result-set, mysqli_fetch_assoc() doesn't just keep cycling through. So you're hitting the end of the $result2 set within the first outer loop, and then that's it, there are no more results to fetch.
You should fetch all of the $result2 results outside of the $result loop, assign the results to a variable, and then loop through that array within the $result loop.
Something like:
if(mysqli_num_rows($result2) > 0){
$result2Set = mysqli_fetch_all($result2, MYSQLI_ASSOC);
while($row = mysqli_fetch_assoc($result)) {
echo "<br>" ."Vraag: <br>" . $row["questions_body"]. "<br>";
foreach($result2Set as $row_answer) {
echo $row_answer["answer_body"]. "<br>";
}
}
}
I want to do a a query to a mysql database that returns multiple rows and columns. I then want to assign the results to a variable and echo them out later in the page. However, the method I am using is long, tedious, and for this project impractical. here is what I am doing.
$result = mysql_query("SELECT * FROM people WHERE open_or_closed !='Closed' ORDER BY
number",$c) or die("two");
$number=mysql_num_rows($result);
if($mynumber>0){
$data= mysql_fetch_array($result,MYSQL_ASSOC);
$full_name1=mysql_result($result,0, 'full_name');
$phone_number1=mysql_result($result,0, 'phone_number');
$one=1;
}
if($mynumber>1){
$full_name2=mysql_result($result,1, 'full_name');
$phone_number2=mysql_result($result,1, 'phone_number');
$two=2;
}
Later when I want to echo it, I will not know if there is a record there or not, so I will have to
<?php if($one==1){echo '<div id="blackline"></div>';}?>
<div id="titletext"><?php echo $full_name1; ?></div><br />
<div id="datetext"><?php echo $phone_number1; ?></div>
Try this
$result = mysql_query("SELECT * FROM people WHERE open_or_closed !='Closed' ORDER BY
number",$c) or die("two");
$number=mysql_num_rows($result);
if($number>0)
{
$i=0;
while($row_result = mysql_fetch_array($result))
{
$full_name[$i][] = $row_result['full_name'];
$phone_number[$i][] = $row_result['phone_number'];
$i++;
}
}
I have a page displaying random bible quotes from which you can also search. The quotes on this page are displayed in dynamic link format, e.g. bible-query.php?id=200
On the search results page, I have put a link each at the bottom and top of the page to help the users get back to the random display page. These links are dynamic links. The only problem is, I can only get the top link to display the dynamic link, the bottom one just loads a page with no quotes.
What I want is to have the bottom and top 'Back' links display the same dynamic link location.
Here is the code I have for the search results:
<html>
<font face="arial">
<title>BQuotes: Random Bible Verses</title>
<?php
// db requirements
$db_host="localhost";
$db_username="username";
$db_password="password";
$db_name="name";
$db_tb_name="table";
$db_tb_atr_name="line";
$db_tb_atr_name2="book";
$db_tb_atr_name3="cap";
$db_tb_atr_name4="verse";
//do search task
mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");
$query=mysql_real_escape_string($_GET['query']);
$query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE
$db_tb_atr_name like '%".$query."%' OR $db_tb_atr_name2 like '%".$query."%'
OR $db_tb_atr_name3 like '%".$query."%' OR $db_tb_atr_name4 like '%".$query."%'");
echo "Search Results<ol>";
// new bible query section begins
define ('HOSTNAME', 'localhost');
define ('USERNAME', 'username');
define ('PASSWORD', 'password');
define ('DATABASE_NAME', 'name');
$db = mysql_connect(HOSTNAME, USERNAME, PASSWORD) or die ('I cannot connect to
MySQL.');
mysql_select_db(DATABASE_NAME);
$query = "SELECT id,book,cap,verse,line FROM table ORDER BY RAND() LIMIT 1 ";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo "<center><a href='bible-query.php?id=$row[id]'>Back</a></center> ";
}
//mysql_free_result($result);
//mysql_close();
//bible query new section ends
while($data_fetch=mysql_fetch_array($query_for_result))
{
echo "<li>";
echo substr($data_fetch[$db_tb_atr_name2], 0,160)," ";
echo substr($data_fetch[$db_tb_atr_name3], 0,160)," ";
echo substr($data_fetch[$db_tb_atr_name4], 0,160)," ";
echo substr($data_fetch[$db_tb_atr_name], 0,160);
echo "</li><hr/>";
}
echo "<center><a href='bible-query.php?id=$row[id]'>Back</a></center> ";
echo "</ol>";
//mysql_close();
?>
</font>
</html>
Please help!
Your while loop is fetching the row using mysql_fetch_array. On the first time it runs it retrieves a result and so the value of $row is set to an array and the conditional is "true" so the bit in the while loop runs. However, on the second run-through there is no second row and so $row is set to false and the while loop doesn't run. This means that after the while loop $row is an empty variable. When it gets to the second calling of that array it is empty. By removing the while loop the $row variable is only fetched once and the first row is returned. This is all you need.
Just change this bit:
while ($row = mysql_fetch_array($result)) {
echo "<center><a href='bible-query.php?id=$row[id]'>Back</a></center> ";
}
to:
$row = mysql_fetch_array($result);
echo "<center><a href='bible-query.php?id=$row[id]'>Back</a></center>";
and both parts should work.
Change your code to this. No need for a while loop since you are limiting 1 row in your query. Also I changed the php mysql function that you should use.
$query = "SELECT id,book,cap,verse,line FROM table ORDER BY RAND() LIMIT 1 ";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo "<center><a href='bible-query.php?id=$row[id]'>Back</a></center> ";
Noticed I used mysql_fetch_assoc
I have searched around and have found many solutions to my question, but I am still having one problem. I am trying to create a table that will have four images per row. This has been done. I have told the script to group by rewardid and it does that so now duplicate images do not appear (the steps to do these I have found on the site). What I am having problems with is that I m trying to put a multiplier under each image to count how many was in each group. (i.e. Lets say I have 5 medals. On my page, it only shows one medal, but I really have 5, so I would like for it to say x5 below the image.) This is what I have so far:
print "<div class='style1'><br> <br><h2>User Medals</div></h3><br />
<table width='80%' class='table' border='0' cellspacing='1'><tr bgcolor='gray'><th></th><th></th><th></th><th></th></tr>";
$result=mysql_query("SELECT * FROM awardsearned WHERE userid=$userid group by rewardid");
$count = 0;
while($res=mysql_fetch_array($result))
{
$award = $res['rewardpic'];
$award2 = $res['rewardid'];
$result2=mysql_query("SELECT awardid, count(rewardid) FROM awardsearned WHERE rewardid=$award2 AND userid=$userid");
$count2 = count($result2);
if($count==4) //three images per row
{
print "</tr>";
$count = 0;
}
if($count==0)
print "<tr>";
print "<td>";
print "<center><img src='$award' width='100' height='100'/><br />x$count2</center> ";
$count++;
print "</td>";
}
if($count>0)
print "</tr>";
print "
</table>";
Sorry if this is messed up, never posted here before. Here it is on pastebin if needed http://pastebin.com/iAyuAAzV
Update your query like this:
SELECT *,COUNT(rewardid) no_of_rewards
FROM awardsearned WHERE userid=$userid group by rewardid
$res['no_of_rewards'] will hold your number.
This will also eliminate the need for the second query.
These things are called "aggregate functions". More in the documentation.
To answer your question:
The value of $result2 would only give TRUE(1) or FALSE(0). If your query executed correctly $count2 will return 1, therefore you always get 1 in your code.
Try and change
$result2=mysql_query("SELECT awardid, count(rewardid) FROM awardsearned WHERE rewardid=$award2 AND userid=$userid");
$count2 = count($result2);
TO:
$result = mysql_query("SELECT awardid, count(rewardid) FROM awardsearned WHERE rewardid=$award2 AND userid=$userid");
$arr = mysql_fetch_array($result);
$count2 = count($arr);
This should give you the actual numbes of records from the resultset.
There are better ways of doing this (look at Bart Friederichs answer!) , like doing only SELECT and loop through resultset once. This would be for performance and for flexibility.
Also take in my consideration that mysql_query is deprecated now, and should not be used. (The preferal methods are to use PDO or mysqli instead)