Hello, I need a little help... I try to extract from database mysql... but I have an problem... not work Can anyone help me? <?php $channel_name = $_GET['v']; $ads=""; $result = mysql_query("SELECT * FROM `table` WHERE `channel_name`='{$channel_name}';"); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result)) { $adscode_big = $row["adscode_big"]; $ads1 = $row["ad1"]; if($adscode_big == '') { $ads = $ads1 ; } else { $ads = $adscode_big; } } ?> I try to extract with condition... what is wrong ? Thanks for all sugestion!
Seriously... First: DO NOT USE mysql_query. It's deprecated, and should not be used. Second: DO NOT PUT unescaped variables into a query. EVER. Third... oh, man. Okay, let's for a moment say you had used PDO (a modern DB-class within PHP) to access your database: <?php $ads = ''; $query = $dbh->prepare("SELECT * FROM `table` WHERE `channel_name` = :channel_name"); if ($query->execute(array(':channel_name'=>$_GET['v']))) { while ($row = $query->fetch()) { if (empty($row['adscode_big'])) { $ads = $row['ad1']; } else { $ads = $row['adscode_big']; } } } else { //do error-handling here } ?> PHP: This assumes you have a database-object already established named $dbh, and uses the built-in prepared queries methodology to sanitize the content. There's no reason to create multiple variables when you can just use the result variables. The query above, and the handling, assumes there's only one row to fetch each time - if there are multiple rows, you'll always get the last one in the result set. Normally, a PDO-query is run in a try/catch-block - I personally don't do that, since I think it messes up the code too much - this is based on my own implementation, but apart from the missing error-catching, it works just fine.
Thanks so much for your help!! I have an question, how I can select from two table: adscode_small and channel_name is in tabel1 and ad1 is is tabel2 how can I do this SELECT ?