php - assigning variables to sql query result -
amateur here, practicing sql.
i trying assign php variables sql query results, , thought saw work in fashion, doesn't seem working me:
$query1 = mysql_query("select * markers id = '$markerid'"); $result = mysql_query($query1); $markerdata = mysql_fetch_array($result, mysql_assoc); $name = $markerdata['name']; $description = $markerdata['description']; $directions = $markerdata['directions']; $lat = $markerdata['lat']; $lng = $markerdata['lng']; $type = $markerdata['type']; $addedby = $markerdata['addedby']; is there rule blatantly disregarding?
additionally, know sql deprecated/unsafe, trying larn basics here.
sincere help.
you should feed query statement mysql_query function, not feed mysql query:
$sql = "select * markers id = '$markerid'"; $result = mysql_query($sql); obligatory note:
please, don't utilize mysql_* functions in new code. no longer maintained and officially deprecated. see red box? larn prepared statements instead, , utilize pdo or mysqli - this article help decide which. if take pdo, here tutorial.
it doesn't matter if you're still in starting stage. since know api deprecated , queries usafe, why not start habits.
this way of pdo prepared statements:
$db = new pdo('mysql:host=localhost;dbname=db_name', 'username', 'password'); $sql = 'select * markers id = :markerid'; $select = $db->prepare($sql); $select->bindparam(':markerid', $markerid, pdo::param_int); $select->execute(); $markerdata = $select->fetch(pdo::fetch_assoc); $name = $markerdata['name']; $description = $markerdata['description']; $directions = $markerdata['directions']; $lat = $markerdata['lat']; $lng = $markerdata['lng']; $type = $markerdata['type']; $addedby = $markerdata['addedby']; php mysql sql
No comments:
Post a Comment