0

I am trying to fetch data from table. Table contains the data and query is true. Even why following query says $u and $t are not define. While condition becoming false.

I manually checked in database, it was showing results.

$url = "http://paulgraham.com/";
$user_id = "123";
$con = mysqli_connect('127.0.0.1', 'root', '', 'mysql');
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    return;
}
$result = mysqli_query($con,"SELECT * FROM post_data WHERE userid =".$url." and url=".$user_id."");
while ($row = @mysqli_fetch_array($result))
{
    echo "hi";
    $t = $row['title'];
    $u = $row['url'];

}

echo "title is : $t";
echo "url is : $u";
1
  • 3
    "SELECT * FROM post_data WHERE userid =".$url." and url=".$user_id."" You still really don't see where the problem is ? Commented Oct 31, 2013 at 16:34

2 Answers 2

1

Giving your SQL query :

"SELECT * FROM post_data WHERE userid =".$url." and url=".$user_id.""

You can see you are mixing url and userid... Change to :

"SELECT * FROM post_data WHERE userid =".$user_id." and url=".$url.""

Also define your $t and $u variables before your loop in case you have no record. Next time, try to var_dump your generated query to test it.

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

Comments

1

If you were able to see the errors the DBMS is reporting back to PHP then you'd probably be able to work out what's wrong with the code.

Before the 'while' loop try...

print mysql_error();

(the obvious reason it's failing is that strings mut be quoted in SQL, and you've got the parameters the wrong way around)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.