-1

I have a pretty basic website connected to a database and I want to access the result of an SQL query:

$connection = mysql_connect("localhost", "username", "password") 
    or die(mysql_error()); 
mysql_select_db("Database") 
    or die(mysql_error());

$query = mysql_query("SELECT path FROM db1 WHERE id > 4");
while($row = mysql_fetch_object($query))
{
    echo $row->path;
}

On the website nothing shows, not even an error (The SQL code works for sure)

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Maximilian
  • 754
  • 1
  • 5
  • 26
  • 2
    check for errors against your query also, not just the connection – Funk Forty Niner May 05 '16 at 15:07
  • You're checking for errors after your connection, after selecting the database, but not after actually running the query. I would try that, even if you're sure the query works (you executed it successfully through phpmyadmin or workbench). There certainly are reasons that you wouldn't get an error trying to connect but would not be able to execute queries. – Don't Panic May 05 '16 at 15:07
  • Or more succinctly, just do what @Fred-ii- said. – Don't Panic May 05 '16 at 15:08
  • Yeah, @Don'tPanic mine was *"in a nutshell"* ;-) – Funk Forty Niner May 05 '16 at 15:08
  • I checked for errors against the query but there are no – Maximilian May 05 '16 at 15:10
  • what is the id column type? – Funk Forty Niner May 05 '16 at 15:11
  • the id column type is int – Maximilian May 05 '16 at 15:12
  • 1
    you should better use mysqli or PDO_mysql as your sql lib... mysql is deprecated – lpg May 05 '16 at 15:13
  • what about http://php.net/manual/en/function.error-reporting.php and try `mysql_select_db("Database", $connection)` – Funk Forty Niner May 05 '16 at 15:13
  • Yeah i know but for this project its ok @lpg – Maximilian May 05 '16 at 15:13
  • ask the guy who gave you an answer below then, I'll have to move on now. Good luck, sincerely. – Funk Forty Niner May 05 '16 at 15:18
  • ok... the code looks fine to me, so I would suggest looking to data or config issues... Is your table having id's greater than 4? Are you able to see other "echo" outputs in your webpage? – lpg May 05 '16 at 15:18
  • Yes i am able to see other echo outputs and my table has id's greater than 4 – Maximilian May 05 '16 at 15:19
  • Ouu you are right there are no id's greater than 4 , with greater than 3 everything works. Sry my mistake :D – Maximilian May 05 '16 at 15:22
  • 2
    Therefore you could have just done `WHERE id >= 4` which means greater than or equal to 4 and you would have had results. This is what the real solution here is ;-) @Maximilian – Funk Forty Niner May 05 '16 at 15:26
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 05 '16 at 15:28
  • Thanks to all for your efforts I feel sorry for making such an dumb mistake can someone please make an answer so i can accept it – Maximilian May 05 '16 at 15:31
  • You're welcome @Maximilian ;-) *cheers* – Funk Forty Niner May 05 '16 at 15:35

1 Answers1

1

Since you stated in comments that there were no id greater than 4,

you could have just done WHERE id >= 4 which means greater than or equal to 4 and you would have had results.

References:

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141