---- Getting contents from a (MYSQL) database and displaying on php page
rocknbil - 4:09 pm on Mar 27, 2012 (gmt 0)
That's correct, if signatures were allowed here mine would say "none of the above is working code and is typed on the fly for example only unless it explicitly says 'tested example.'" The missing first bracket after isset($_GET['id'])
$query = "select * from table where id=$song_id";
This is also correct, with a couple twists you need to know.
The first is that in PHP, scalar $variables will interpolate (output the value they represent) when in double quotes. This is why the above works. if you did this,
$query = 'select * from table where id=$song_id';
it would not work, because the mysql query would error on one level, and do nothing on another: the first is that there is no literal song_id '$song_id' (variables don't interpolate when single quoted) and the second has to do with quoting on the mysql layer.
In the mySQL layer, there is a quoting syntax separate from PHP. The "other reason" the above query works is that numeric values do not need to be quoted in mySQL - in fact, it's a good thing that you don't quote when you search for numeric values. Consider if your variable $song_id is empty or null;
$song_id=null; $query = "select * from table where id=$song_id";
This gives you, literally,
select * from table where id=
Which will give you a mySQL error (not a PHP error) and alert you to the problem. Now consider if you had quoted that statement. There are many ways to quote within PHP statements, these three are synonymous:
$query = "select * from table where id=\"$song_id\""; $query = 'select * from table where id="' . $song_id . '"'; $query = "select * from table where id='$song_id'";
So now your literal statement would be
select * from table where id='';
Which would do what?
It wouldn't error. It wouldn't give you any notice something was wrong. It just wouldn't work, and you could spend hours wondering why.
Textual values will always need quoting, but when dealing with numeric values it's to your advantage not to quote them.
Returning to your code, what is "runrows?"
$title = $runrows['title']; (etc)
will it display the correct things, or does it need more code?
Yes it will need more code - I've no idea if those are even your database field names, This assumes the names of the fields in your database are "title", "artist", "year", and "duration" (length of song, at a guess. :-) ):