0

sorry to bother you all but I'm really struggling with this one:

I connect to my database fine and then I try the following mysql statements:

$query1 = "select row1 from mydatabase where row2 = $Name ";
$answer1 = mysql_query($query1);

However, a few lines later when I try :

echo $answer1;

I'm given only nulls :(

Can anyone give me any suggestions please?

edit: SQL logins:

mysql_connect("correct", "username", "password");

mysql_select_db("dbname") or die(mysql_error());
10
  • Have you run this query directly on the Db to see if there is any data in there for this query? Commented Apr 16, 2014 at 15:56
  • @AbhishekAsthana yes, and I get my row :) Commented Apr 16, 2014 at 15:57
  • Could you please provide the actual SQL you are running from PHP encase there is a Syntax error. Also a larger code example would help us answer your question better. Commented Apr 16, 2014 at 15:58
  • 2
    We are also assuming that $answer1 remains in scope when it's called again, but we cannot say for sure with the limited code posted. Commented Apr 16, 2014 at 15:59
  • can you try a var_dump right after $answer1 = mysql_query($query1); and confirm that variable is in scope at echo $answer1; like @Skewled mentioned? Commented Apr 16, 2014 at 16:01

3 Answers 3

4

everything you did is right you have just to fetch the data like this:

$query1 = "select row1 from mydatabase where row2 = $Name ";
$answer1 = mysql_query($query1);

while($data= mysql_fetch_array($answer1)){
    echo $data['row1'];
}

And this is a complet answer, i adjust it as you need ;)

<?php
    //Connect to your database
    $con=mysqli_connect("db_hostname","db_user","db_password","db_name");

    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    //Value of the row to select
    $row2 = 'some value';

    //Make select query
    $result = mysqli_query($con, "SELECT row1 FROM MyTable WHERE row2='$row2'");

    //Fetch datas
    while($row = mysqli_fetch_array($result))
    {
        echo $row['row1'];
        echo "<br>";
    }

    //Close database
    mysqli_close($con);
?>

Good Luck :)

Sign up to request clarification or add additional context in comments.

3 Comments

it says "expects parameter 1 to be resource, boolean given" as error message. for the while statement.
i just give you another simple code it's working just fine, just you have to adapt it with your needs
i preferd to use mysqli extension than just mysql , because mysql it's really so old and has so many problems
1

Try using MySQLi_* instead MySQL_* functions and pass the connection variable to the function calls.

If this doesn't work then you might want to try some further debugging by enabling all error reporting and dumping the global scope.

<?php
error_reporting(E_ALL); // Show all errors & warnings

$conn = mysqli_connect("server", "username", "password");
mysqli_select_db($conn, "dbname") or die(mysql_error());

$sql1 = "SELECT `row1` FROM `mydatabase` WHERE `row2` = '".$Name."';";
$query1 = mysqli_query($conn, $sql1);
$answer1 = mysqli_fetch_assoc($query1);

var_dump($GLOBALS); // Dumps all variables in the global scope
?>

Comments

-1

add this after $answer1= mysql_query($query1);

while ($row = mysql_fetch_assoc($answer1)) {
   // echo data
   echo $row['row1'];
}

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.