-1

I have a Table named 'progress' and inside a column 'number' with value '2'. I want to pick the value 2 and put in a $result.

$letter ='A';
$number = "SELECT number FROM progress";
$result = mysql_fetch_object($number);
$finalcode = $letter . $result;
miro toff
  • 1
  • 1

1 Answers1

0

You did not execute your query. So mysql_fetch_object() has nothing to return. Moreover, You need to connect to your database which is accomplished using mysql_connect(). To execute queries using mysql you can use mysql_query. Wrapping the previous notes in your code as an example would be:

// Connecting to database
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully'; // Connected

$letter ='A';
$number = "SELECT number FROM progress";

$result = mysql_query($number); // Executing your query
$row = mysql_fetch_object($result); // Fetching your result into 'row'

$finalcode = $letter . $row;

In the above code, your object was fetched into $row. You can also specify the class name as the second parameter in mysql_fetch_object(), as an example:

$row = mysql_fetch_object($result,'foo');

Note: If your query is going to return more than one row you will have to loop over the $result, which has many examples in the manuals provided.

Note: Please have a look at mysqli overview, which should show you the differences between mysql and mysqli.

ndrwnaguib
  • 4,539
  • 2
  • 26
  • 47