Jump to content

Recommended Posts

Trying to have an echo display a result from a query.

Not such a hard concept. Yet I am having trouble.

What good is the data without analysis.

Here is what I am using to try and grab result.

Have been told it looks correct, must be missing something small.

When this runs, alternate echo displays in browser.

So you know: 'testdata' is the table

'score' & 'id' are columns or fields.

 

Almost think it is missing a table structure to display in.

Looking for player name column and score column (for the sake of example).

 

 

<?

$name = $_GET['name'];

$score = $_GET['score'];

$dbh=mysql_connect ("localhost", "username", "password") or

die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db

("database");

 

if ($score > 0) {

$query = 'SELECT `id` , `score` FROM `testdata` WHERE id = 1 LIMIT 0, 2 ';

$result = mysql_query('SELECT `id` , `score` FROM `testdata` WHERE id = 1 LIMIT 0, 2 ');

echo mysql_result($result, 2);

}

else {

echo('No score was submitted');

}

?>

 

 

=================

This is what phpmyadmin says is correct php code for sql query?

=================

 

$sql = 'SELECT `id` , `score` FROM `testdata` WHERE id = 1 LIMIT 0, 2 ';

 

========================================================

Would appreciate any feedback that will help me get to my next step.

Link to comment
https://forums.phpfreaks.com/topic/55150-echo-a-result-of-query/
Share on other sites

Tr this simple test (Because I am too lazy to check if your code is correct because it is not highlighted as PHP in the code tags :-P)...

 

 

<?php

$query = mysql_query("SELECT id, score FROM testdata WHERE id = '1' LIMIT 0, 2 ");
$result = mysql_fetch_array($query);

echo"$result[0] $result[1]";

?>

<?

$name = $_GET['name'];

$score = $_GET['score'];

$dbh=mysql_connect ("localhost", "username", "password") or

die ('I cannot connect to the database because: ' . mysql_error());

mysql_select_db("database");

 

if ($score > 0) {

$query = "SELECT id , score FROM testdata WHERE id = 1 LIMIT 0, 2 ";

$result = mysql_query($query);

$row=mysql_fetch_array($result);

echo "Id=" .$row['id']."<br>";

echo "score=" .$row['score']."<br>";

 

 

}

else {

echo('No score was submitted');

}

?>

Caesar,

 

I appreciate the help, first recommendation that showed a result.

I was able to show one users data, but had to use something other than:

SELECT id , score FROM testdata WHERE id = 1 LIMIT 0, 2;

 

Almost like commands score < 300  don't work....or id < 30 ...or id = 30

Should there be 'marks like these around fields'....or "quotations"?

 

Getting calculations to work is one thing. What about multiple rows of this data?

Think suma237 might have been in this direction.

For example....SELECT all users with score < x  and place all there information (id #, name, score, date)

In row format...using your $result[0]$result[1]$result[2]$result[3]$result[4] Method.

Just want a next row for next users data    ex:  $result[a]$result[a1]$result[a2]$result[a3]$result[a4]

 

I'd just like to look at this bit of code:

 

<?php

if ($score > 0) {
$query = 'SELECT `id` , `score` FROM `testdata` WHERE id = 1 LIMIT 0, 2 ';
$result = mysql_query('SELECT `id` , `score` FROM `testdata` WHERE id = 1 LIMIT 0, 2 ');
echo mysql_result($result, 2);
}
else {
echo('No score was submitted');
}

?>

 

Instead of doing echo mysql_result($result, 2); try this:

 

<?php

if ($score > 0) {
$query = 'SELECT `id` , `score` FROM `testdata` WHERE id = 1 LIMIT 0, 2 ';
$result = mysql_query('SELECT `id` , `score` FROM `testdata` WHERE id = 1 LIMIT 0, 2 ');

//Here is the new code:
$data = mysql_fetch_array( $result );
print_r( $data );

}
else {
echo('No score was submitted');
}

?>

 

That will output the exact result of an array. If you are trying to do something more formatted to your needs, try this:

 

<?php

//I will just put the new code to save space

while( $row = mysql_fetch_array( $result ) ){

   //This would be a varibable to be echoed. On the right are the specific DB results from the array, except now we will format
   $myEchoVar = $row['myVarinDB'] . "<br />";
   $myEchoVar .= $row['myVar2inDB'];

   //After the data is there, lets echo

   echo( $myEchoVar );

}

?>

 

Hope this helps

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.