user123
user123

Reputation: 5407

Fetching data from database in php file

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";

Upvotes: 0

Views: 53

Answers (2)

symcbean
symcbean

Reputation: 48357

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)

Upvotes: 1

Brewal
Brewal

Reputation: 8189

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.

Upvotes: 1

Related Questions