katlis Posted August 16, 2008 Share Posted August 16, 2008 I have a link like track.php?id=30 ... I want to track clicks to a row where id=30.. but what's the best function for first checking to see if the row already exists, and if not, create that entry? Link to comment https://forums.phpfreaks.com/topic/119996-add-a-row-to-a-table-if-not-found/ Share on other sites More sharing options...
dannyb785 Posted August 16, 2008 Share Posted August 16, 2008 youd do a query for the row id like <?php $id = addslashes($_GET['id']); $result = mysql_query("SELECT * FROM Table WHERE id='$id' "); if(mysql_num_rows($result) == 0) // do INSERT query to create a row else // do whatever function to $id ?> Link to comment https://forums.phpfreaks.com/topic/119996-add-a-row-to-a-table-if-not-found/#findComment-618111 Share on other sites More sharing options...
ignace Posted August 16, 2008 Share Posted August 16, 2008 function map_clicks_to_row($row_id) { global $dbConnection; $query = "SELECT * FROM clickable_rows_table WHERE id = %s"; $result = mysql_query(sprintf($query, $row_id), $dbConnection); if (0 !== mysql_num_rows($result)) { // row exist, make it extra clickable.. } } @dannyb785 and all other phpfreaks forum regulars mysql_query() has a second optional parameter called $link_identifier, leaving this parameter empty will use the latest used open connection to the database, if you like me, use more then one database will this result in a serious rewrite of your code, in my opinion the second parameter shouldn't even be optional Link to comment https://forums.phpfreaks.com/topic/119996-add-a-row-to-a-table-if-not-found/#findComment-618113 Share on other sites More sharing options...
Mchl Posted August 16, 2008 Share Posted August 16, 2008 If id in your table is not autoincrement, you could also try: INSERT INTO table SET id = $id, clicks = 0 ON DUPLICATE KEY UPDATE clicks = clicks + 1; Link to comment https://forums.phpfreaks.com/topic/119996-add-a-row-to-a-table-if-not-found/#findComment-618118 Share on other sites More sharing options...
dannyb785 Posted August 17, 2008 Share Posted August 17, 2008 function map_clicks_to_row($row_id) { global $dbConnection; $query = "SELECT * FROM clickable_rows_table WHERE id = %s"; $result = mysql_query(sprintf($query, $row_id), $dbConnection); if (0 !== mysql_num_rows($result)) { // row exist, make it extra clickable.. } } @dannyb785 and all other phpfreaks forum regulars mysql_query() has a second optional parameter called $link_identifier, leaving this parameter empty will use the latest used open connection to the database, if you like me, use more then one database will this result in a serious rewrite of your code, in my opinion the second parameter shouldn't even be optional why on earth would you need to have 2 separate database connections in the same script? I've never needed it nor can I ever see needing it. Link to comment https://forums.phpfreaks.com/topic/119996-add-a-row-to-a-table-if-not-found/#findComment-618330 Share on other sites More sharing options...
chronister Posted August 17, 2008 Share Posted August 17, 2008 possibly accessing 2 different databases. I have needed it before and simply called my db_connect function each time I needed it. There are many reasons to have 2 db connections open, but this is the first one I could think of. Link to comment https://forums.phpfreaks.com/topic/119996-add-a-row-to-a-table-if-not-found/#findComment-618337 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.