Display mysql database using php

How do i display a mysql database using php

The database is a practise test question for practise test. However, the answers are 4 multile choice
answers grouped together in one field with 2 tiny square boxs between each answer (what are these, deviders of some sort?)

I want to be able to display all the questions and answers in the database using the multiple choice format of 1 question & 4 possible answers. and a next button to go to the next question.

Can someone advise me please

Database can be downloaded from here (database is microsoft access for easy viewing, but I have database running in myphpadmin)
http://www.calcararecruitment.com/application.php
(Virus Free I promise. It is a practise exam database for international nurses.)
kevinb4940Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
More info on these pages:
http://us2.php.net/manual/en/function.mysql-query.php

Look at the menus on the left-hand side.
<?php
// we connect to example.com and port 3307
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
} 
/ make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
} 

// Formulate Query
// This is the best way to perform a SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT firstname, lastname, address, age FROM friends WHERE firstname='%s' AND lastname='%s'",
    mysql_real_escape_string($firstname),
    mysql_real_escape_string($lastname)); 
// Perform Query
$result = mysql_query($query); 
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
} 
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
    echo $row['firstname'];
    echo $row['lastname'];
    echo $row['address'];
    echo $row['age'];
} 
// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result); 

echo 'Connected successfully';
mysql_close($link);
?>

Open in new window

0
Cornelia YoderArtistCommented:

//connect to your database first ...

$sql = "SELECT * FROM Table";
$result=mysql_query($sql) or die(mysql_error());
while ($row=mysql_fetch_array($result))
{
   $question=$row["Questionfield"];
   $answers=$row["Answersfield"];
   $answerarray=explode("thesquarecharacter", $answers);
   echo $question;  
   echo "<br>";
   echo $answerarray[0]; //first answer
   echo $answerarray[1];
   echo $answerarray[2];
   echo $answerarray[3];
}

Change the Table name and field names to your own, of course.  The "thesquarecharacter" needs to be the little square you describe, probably some kind of unprintable special character.
0
kevinb4940Author Commented:
Just getting a blank screen . Using wampserver version5

Code below

<?php
// we connect to example.com and port 3307
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('movedb', $link);
if (!$db_selected) {
    die ('Can\'t use movedb : ' . mysql_error());
}

// Formulate Query
// This is the best way to perform a SQL query
// For more examples, see mysql_real_escape_string()
$sql = "SELECT * FROM v1answers";
$result=mysql_query($sql) or die(mysql_error());
while ($row=mysql_fetch_array($result))
{
   $question=$row["Stemtext"];
   $answers=$row["RationalText"];
   $answerarray=explode("thesquarecharacter", $answers);
   echo $question;  
   echo "<br>";
   echo $answerarray[0]; //first answer
   echo $answerarray[1];
   echo $answerarray[2];
   echo $answerarray[3];
}

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);

echo 'Connected successfully';
mysql_close($link);
?>
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Cornelia YoderArtistCommented:
You need to figure out what the "square character" is and put that into the explode() function.

However, the echo for $question should be working, if you are getting any result from the table and have all the names correct.  Remember, in php, lower/upper case matters.... Is it Stemtext and RationalText?
0
kevinb4940Author Commented:
Thanks

Yes it is Is it Stemtext and RationalText

You need to figure out what the "square character" is ?? will i just google it?
0
Cornelia YoderArtistCommented:
See if you can find a way to display it in hex.  Then you can look up the hex code and see what it is.
0
kevinb4940Author Commented:
It's a carriage return character

http://support.microsoft.com/kb/210372

How will i write that in php? is it <br />
0
Cornelia YoderArtistCommented:
Are you free to change it?  If so, use phpmyadmin or some similar program and using a copy/paste of the character from the data, do a change-all to some other character that you can use as a delimiter ($ or something like that).

If not, then tell me the hex value of the character and I'll see if I can find another alternative.
0
Cornelia YoderArtistCommented:
Can you copy/paste a short segment of one of the strings containing that character here?  
0
hieloCommented:
Change:
$answerarray=explode("thesquarecharacter", $answers);

to:
$answerarray=explode("\n", $answers);
0
kevinb4940Author Commented:
Sorry, I am new to php

The page line breaks are showing in the print view of phpmyadmin for the content of the v1answers table
0
Cornelia YoderArtistCommented:
Heilo, that is what we are trying to determine, if his special character is in fact the same as \n.  If not, I have a way to solve this, once he gets the hex code to me.
0
Cornelia YoderArtistCommented:
Go here, copy/paste from the data directly, and then copy/paste the hex back here.

http://centricle.com/tools/ascii-hex/
0
Cornelia YoderArtistCommented:
Another alternative is to isolate the special character with php, into a variable $char, and then use

echo hexdec($char);

I can work from the decimal value.
0
kevinb4940Author Commented:
%d%a%d%a
0
Cornelia YoderArtistCommented:
That isn't helpful, because it should be 2-digit hex values.  Did you try getting the decimal value of the special character?
0
kevinb4940Author Commented:
Yes,  sorry about this but the square cahr will not paste into the ascii to hex convertor
0
Cornelia YoderArtistCommented:
The hex D is a carriage return and hex A is a line feed, so that might be what you have.  So far I haven't found a way to specify those in php to use in the explode.  The \n suggested by hielo is a possiblity, or perhaps a double \n\n.

Try using each of those in the explode and see if it works.

0
Cornelia YoderArtistCommented:
I've been doing a lot of searching, and I can find places where others have asked similar questions, and not one of them has a useful answer.

You may need to do some experimenting in your database ... make a temp table, copy a row containing the CR to it, and then try some things like CONV() to convert it to base 10.  If you are able to find a way to select it out of the text string, you should be able to work out a way to replace it with a different delimiter.  

It looks to me like that is going to be your best option.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kevinb4940Author Commented:
Thanks for all your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.