0

I have been trying for the past while to echo out the result of a a MySQL query in PHP. I can't seem to get it to work so I am a bit lost. I know for a fact that the query works as I have done it in PHPMYADMIN and it is working fine, however whenever I load the webpage, nothing is outputted. For the purposes of this question I have generalized the things in the query as I obviously don't want anyone accessing my database. This is what I have tried:

<?php
    $connect = mysql_connect('host', 'user', 'password', 'dbname');
    if (!$connect) {
        die('Could not connect: ' . mysql_error());
    }
    if (!mysql_select_db('dbname')) {
        die('Could not select database: ' . mysql_error());
    }
    $result = mysql_query('SELECT SUM(row name) FROM `table`');
    if (!$result) {
        die('Could not query:' . mysql_error());
    }
    echo mysql_result($result);

    mysql_close($connect);
?>

I look forward to your replies.

2
  • What about echo "mysql_result($result)"; Commented Dec 25, 2013 at 10:03
  • MYSQL_* is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. Commented Dec 25, 2013 at 10:04

7 Answers 7

3

mysql_result need the second parameter (The row number from the result that's being retrieved).

echo mysql_result($result, 0);
Sign up to request clarification or add additional context in comments.

Comments

2

you have to change

echo mysql_result($result); by

echo mysql_result($result,0);

or you can use

print_r(mysql_fetch_assoc($result));

Comments

1

Try this

echo mysql_result($result,0);

For more information, please give a look on http://www.php.net/mysql_result

or you can use mysql_fetch_row instead of mysql_result.

$row = mysql_fetch_row($result);
echo $row[0];

Comments

1

You may use this following format: Give your Sql command like this:

 $result = mysql_query('SELECT SUM(row name) as new_column_name FROM `table`');

then use this loop

while($fetched_values = mysql_fetch_array($result)){
    echo $fetched_values['new_column'];
}

Comments

0

In your mysql_connect you have provided 4 parameters. it accepts 3 params

host, username and password. dbname is given inside mysql_select_db

as you can see from official docs. So remove the 4th param from mysql_connect function.

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

http://php.net/function.mysql-connect

$db_selected = mysql_select_db('foo', $link);

http://www.php.net/manual/en/function.mysql-select-db.php

but these mysql_* function will be deprecated use mysqli instead.

Comments

0

pass a row number as second parameter in mysql_result

echo mysql_result($result, 1);

or

use print_r(mysql_fetch_array($result)); where $result is your result resource

Comments

0

if you have any doubt you may refer my following example program.

<?php
$connect = mysql_connect('localhost', 'user_name', 'password', 'db_name');
if (!$connect) 
{
    die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db('db_name')) 
{
    die('Could not select database: ' . mysql_error());
}
$row = mysql_query('SELECT (Tamil+English+Maths) as total FROM table_name');
if (!$row) 
{
    die('Could not query:' . mysql_error());
}
while($fetched_row = mysql_fetch_array($row)){
echo $fetched_row['total'];
}

mysql_close($connect);

?>

here Tamil,English and Maths are the column values of my table

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.