Trying to load questions and answers from MySQL shows nothing - html

In my quiz system I am trying to actually now make the page of the questions, but it shows nothing when I try to show the question rows and the answer rows to the page.
<?php
$q_qselect = mysql_query("SELECT * FROM `questions`");
$q_qnumrows = mysql_num_rows($q_select);
for($i=0;$i<$q_qnumrows;$i++){
$q_qselect = mysql_query("SELECT * FROM `questions` WHERE `id`='$i'");
$q_aselect = mysql_query("SELECT * FROM `answers` WHERE `question_id`='$i'");
$q = mysql_fetch_assoc($q_qselect);
$a = mysql_fetch_assoc($q_aselect);
echo $q['question'] . "<br />";
echo $a['answer'] . "<br />";
}
?>
And also, another question - how can I actually check that he selected the correct answer? (radio button near each answer) when the field in the answers table is correct?

<?php
$question = mysql_query("SELECT questions.*, answers.* FROM questions inner join answers on questions.qid=answers.id");
while($row = mysql_fetch_array($question)) {
echo $row['question_column_name_in_DB'].'<br />' .$row['answer_column_name_in_DB'].'<br />';
}
?>
change column names to their appropriate names. Please look into PDO's once you have gotten this to work.

You Can Try by using As Like Following....
<?php
$query_result = mysql_query("SELECT questions.*, answers.* FROM questions LEFT JOIN answers on questions.id=answers.`question_id`");
while($row = mysql_fetch_array($query_result)) {
echo $row ['question'] . "<br />";
echo $row ['answer'] . "<br />";
}
?>

Related

Getting comments from mysql

I am just wondering where my mistake was , and if it is a simple fix or if it will be a bit harder to fix. You guys don't have to write the answers for me, just point me in the right direction and I think I should be fine, because I've been looking at this for half an hour now , and I can't seem to figure out where my mistake was..
Inserting comments is working wonderfully, I'm just having issues with getting them, and posting them on my page(?)
// INSERTING COMMENTS INTO THE DATABASE
<?php
function setComments($conn) {
if (isset($_POST['commentSubmit'])) {
$author = $_POST['cauthor'];
$date = $_POST['date'];
$message = $_POST['message'];
$sql = "INSERT INTO comments (c_author, c_date, c_message)
VALUES ('$author', '$date', '$message')";
$result = mysqli_query($conn, $sql);
}
} ?>
// GETTING COMMENTS FROM THE DATABSE
<?php
function getComments($conn) {
$sql = "SELECT * FROM comments";
$result = mysqli_query($conn, $sql);
$row = $result->fetch_assoc();
echo "$row['c_message']";
}
?>

How to echo specific mysql data in specific places using php?

If the 'SELECT' statement is used to select data from a database then how do we echo specific rows to specific places on a page using php?
To explain this better - I am trying to SELECT * ALL FROM a table but to echo multiple rows to particular places on the html page using php.
So, imagine that my entire mark up and css has 20 thumbnails on a page and each thumbnail has data and an image that is unique to each thumbnail....do I have to replicate the below 20 times?
I am thinking that the best way to do this (which is probably completely wrong) is to use this statement
SELECT * FROM name_of_table WHERE ID = 4 >>> i.e. where I'd like that specific data echoed....
So, if I have 20 thumbnails do I do this 20 times?
<?php
// Connects to your Database
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$data = mysql_query("SELECT * FROM name_of_table WHERE ID = 4;")
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['name'] . "</td> ";
Print "<th>Product:</th> <td>".$info['product_name'] . " </td></tr>";
}
Print "</table>";
?>
And, rinse and repeat but I change the below statement each time for each thumbnail (each thumbnail has unique data that comes from each row on the MySQL)
SELECT * FROM name_of_table WHERE ID = 4;
What is the best way of doing this?
Thanks!
Simple example.. First get the data with wanted ID:s. Create function for data request.
<?php
// Connects to your Database
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$data = mysql_query("SELECT * FROM name_of_table WHERE ID IN (2,3,4,5,6);")
or die(mysql_error());
// This holds all data rows
$data_array = array();
while($info = mysql_fetch_array( $data ))
$data_array[] = $data;
// Function for rendering data to html
function getItemHtml($id) {
$html = "";
foreach($data_array as $row) {
if ($row['ID'] == $id) {
$html = "<td>" . $row['title'] . "</td>";
// etc.. create item html here
break;
}
}
return $html;
}
// To create one item just call this with item id.
echo getItemHtml(4);
?>

Left Join Drupal 7

I have this problem regarding Drupal 7 Mysql queries especially on LEFT JOIN.
I found this solution but I can't seem it apply it on my problem since I'm not aware how the syntax goes.
https://drupal.stackexchange.com/questions/4317/how-do-i-write-a-left-join-query
This is the solution that I found on the link above.
$terms = db_select('taxonomy_index', 'ti')
->fields('ti', array('tid', 'name'))
->leftJoin('taxonomy_term_data', 'ttd', 'ti.tid = ttd.tid')
->condition('vid', 2)
->condition('nid', $nid)
->execute();
foreach ($terms as $term) {
// $term contains the object for the taxonomy term.
}
Yet I'm having a problem on how do I apply it to my query.
Here is my LEFT JOIN query on mysql.
$query = "SELECT sweep_table.end_offer, sweep_table.title, embed.fbp_id, embed.sweep_stat
FROM sweep_table, embed
WHERE sweep_table.uid=embed.uid AND sweep_table.promo_id=embed.sweep_id";
I already did the first few lines but the rest, I don't know how.
$terms = db_select('sweep_table', 'embed')
->fields('sweep_table', array('end_offer', 'title'))
->fields('embed', array('fbp_id', 'sweep_stat'))
->leftJoin('taxonomy_term_data', 'ttd', 'ti.tid = ttd.tid') //Don't know how to apply to my query.
->condition('vid', 2)
->condition('nid', $nid)
->execute();
foreach ($terms as $term) {
}
Also, was wondering how do I retrieve the data after I successfully LEFT JOIN it?
Would be glad if you help me guys.
Wouldn't have thought that this would work though. Thanks to rekire for the hint.
$query = "SELECT sweep_table.end_offer, sweep_table.title, embed.fbp_id, embed.sweep_stat FROM sweep_table, embed WHERE sweep_table.uid=embed.uid AND sweep_table.promo_id=embed.sweep_id";
$result = db_query($query);
foreach ($result as $row) {
echo $row->end_offer . " " . $row->title . " " . $row->fbp_id . " " . $row->sweep_stat . "<br>";
}

How to remove width and height attributes from multiple posts in Wordpress (MySQL)

So I have thousands of Wordpress posts with the width and height attributes which I want to remove. For example:
img class="aligncenter size-full wp-image-21011999" title="sometitle"
alt="somealt" src="http://mysite.com/blabla/somefile.jpg" width="xxx" height="xxx"
As i mentioned i want to remove width="xxx" height="xxx" and i want to remove them directly from MySQL, dont want to use any PHP functions or similar.
Is there a query i can run through PhpMyadmin?
Are there any regex i can use for the xxx which is different for each post.
Thank you!
You can write a small PHP script (outside of WordPress of course) that will accomplish this quite easily using Regex.
<?php
$host = "hostname";
$user = "username";
$pass = "password";
$db = "database";
mysql_connect($host, $user, $pass);
mysql_select_db($db);
$pattern = "/width|height=\"[0-9]*\"/";
$query = "SELECT ID, post_content FROM wp_posts";
while ($row = mysql_fetch_assoc($query)) {
if (preg_match($pattern, $row['post_content']))
{
$row['post_content'] = preg_replace($pattern, "", $row['post_content']);
mysql_query("UPDATE wp_posts SET post_content=" . $row['post_content'] . "WHERE ID=" . $row['ID'];
}
}
?>
That's the way I'd do it anyway. I'm assuming when you said "no PHP functions" that you mean that you want the data permanently updated in the database, rather than just updated on the fly every time the page is loaded. This should solve the issue. Writing a raw SQL query to deal with this problem will likely be much more complicated.
You don't need to do this within WordPress. You could even run this on a different host, provided said host has database access.
Note: I haven't tested any of this. If you use it, I'd make sure to run it on a small subsection of your database before applying it sitewide.
I have used this script and it has some errors. I have corrected them and used it. It works. Make sure the table name (wpfs_posts) correspondents to your table name with posts. Always have a backup off course.
<?php
$host = "localhost";
$user = "nameuser";
$pass = "password";
$db = "namedb";
mysql_connect($host, $user, $pass);
mysql_select_db($db);
$pattern = "/width|height=\"[0-9]*\"/";
$query = "SELECT ID, post_content FROM wpfs_posts";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
if (preg_match($pattern, $row['post_content']))
{
$row['post_content'] = preg_replace($pattern, "", $row['post_content']);
$row['post_content']=mysql_real_escape_string($row['post_content']);
mysql_query("UPDATE wpfs_posts SET post_content='" . $row['post_content'] . "' WHERE ID=" . $row['ID']);
}
}
?>

Help with PHPExcel Library and mySQL data from a table

I have this script
$query = "SELECT id,last_name,first_name FROM users WHERE tmima_id='6'";
$result = #mysql_query($query);
while($row = mysql_fetch_array($result))
{
$i = 3;
$emp_id = $row['id'];
$cell = 'A'.$i;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($cell, $row['last_name']. $row['first_name']);
$i++;
}
But in the .xls file it prints only one user. Why id doesnt print all of the users ? W
Thanks in advance.
I make the change you said with $sheet
$query = "SELECT id,last_name,first_name FROM users WHERE tmima_id='6'";
$result = #mysql_query($query);
while($row = mysql_fetch_array($result))
{
$i = 3;
$emp_id = $row['id'];
$cell = 'A'.$i;
$sheet->setCellValue($cell, $row['last_name']. $row['first_name']);
$i++;
}
But it still prints out only one record. And yes when i run the query in phpmyadmin it returns more than one record.
How can i print out data from mySql table.. What is going wrong ?
I am pretty sure it is because you are using a unique identifier (WHERE tmima_id='6'). It is only finding the results for that one unique identifier and displaying that. Hope this helps.
$i is being reset to row 3 every loop. Set $i=3; before the while loop, not inside it.