I have an question, how I can select from two table, Tables unrelated to each other: adscode_big and channel_name is in tabel1 and ad1 is on tabel2 this is my code: <?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: how can I do this SELECT ? Thanks in advance!
If they are unrelated why don't you just do a select query before this one and store the result in a variable and call it on line 10?
like wwfc_barmy_army said, you should just do 2 queries if unrelated. But if you can add ID's or something to the DB you can do this. http://www.w3schools.com/sql/sql_join_left.asp You would use left join. But you will need to add an ID number to both or something else that would match so the query works. The below code should work for you SELECT table1.adscode_big, table1.channel_name, table2.ad1 FROM table1 LEFT JOIN table2 ON table1.ID=table2.ID Code (SQL):
Well it's pretty much what you've done so far, but again... So if I was doing it I would do something like... <?php //Whatever id you want from table 2 $id = 1; if ($stmt = $dbh->prepare("SELECT ad1 FROM table2 WHERE id = ?")) { /* bind parameters */ $stmt->bind_param("i", $id); /* execute query */ $stmt->execute(); /* bind result variables */ $stmt->bind_result($ad1); /* fetch value */ $stmt->fetch(); /* close statement */ $stmt->close(); } //$ad1 now contains the value from table 2 //echo $ad1; $ads = ''; $channel_name = $_GET['v']; if ($stmt = $dbh->prepare("SELECT adscode_big FROM `table` WHERE `channel_name` = ?")) { /* bind parameters */ $stmt->bind_param("s", $channel_name); /* execute query */ $stmt->execute(); /* bind result variables */ $stmt->bind_result($adscode_big); /* fetch value */ while ($stmt->fetch()) { if (empty($adscode_big)) { $ads = $ad1; } else { $ads = $adscode_big; } } /* close statement */ $stmt->close(); } echo $ads; ?> PHP: Hope this helps.
Thank you for help... what I mistake... files not work ! This is my realy code... <?php include ('configurations.php'); //Whatever id you want from table 2 $id = 1; if ($stmt = $dbh->prepare("SELECT ad1 FROM ads WHERE id = $id")) { /* bind parameters */ $stmt->bind_param("i", $id); /* execute query */ $stmt->execute(); /* bind result variables */ $stmt->bind_result($ad1); /* fetch value */ $stmt->fetch(); /* close statement */ $stmt->close(); } //$ad1 now contains the value from table 2 echo $ad1; $ads = ''; $channel_name = $_GET['v']; if ($stmt = $dbh->prepare("SELECT adscode_big FROM `userlogin_tbl` WHERE `channel_name` = $channel_name")) { /* bind parameters */ $stmt->bind_param("s", $channel_name); /* execute query */ $stmt->execute(); /* bind result variables */ $stmt->bind_result($adscode_big); /* fetch value */ while ($stmt->fetch()) { if (empty($adscode_big)) { $ads = $ad1; } else { $ads = $adscode_big; } } /* close statement */ $stmt->close(); } echo $ads; ?> PHP: Thanks in advance!
Use the SQL statements with question marks. You then bind the values. <?php include ('configurations.php'); //Whatever id you want from table 2 $id = 1; if ($stmt = $dbh->prepare("SELECT ad1 FROM ads WHERE id = ?")) { /* bind parameters */ $stmt->bind_param("i", $id); /* execute query */ $stmt->execute(); /* bind result variables */ $stmt->bind_result($ad1); /* fetch value */ $stmt->fetch(); /* close statement */ $stmt->close(); } //$ad1 now contains the value from table 2 echo $ad1; $ads = ''; $channel_name = $_GET['v']; if ($stmt = $dbh->prepare("SELECT adscode_big FROM `userlogin_tbl` WHERE `channel_name` = ?")) { /* bind parameters */ $stmt->bind_param("s", $channel_name); /* execute query */ $stmt->execute(); /* bind result variables */ $stmt->bind_result($adscode_big); /* fetch value */ while ($stmt->fetch()) { if (empty($adscode_big)) { $ads = $ad1; } else { $ads = $adscode_big; } } /* close statement */ $stmt->close(); } echo $ads; ?> PHP: See the documentation here: http://php.net/manual/en/mysqli.prepare.php
Thanks so much! how can I check if the code is working ? with isset like this ? I try, but not see isset on page... { if(isset($_GET['v'])) }
Not sure what you mean. I guess checked it it's there: if(isset($_GET['whatever'])){ echo "set"; } else { echo "not set"; } PHP: Then tweak with the URL index.php?whatever=1 - then try removing it.
Thanks again for your help ! I use mysql and server apache on centos 6 VPS... can be this as a problem for this code? can be an mistake on server settings ? not work ...
You know you'll need an object $dbh (an instantiation of the PDO / mysqli_ class) for it to work, right?
Why don't you use a script like ezSQL ? It is much more easier for sql queries. You could do two selects to an array and then merge them together. Check php documentations, they have quite number of ways to merge array without changing their keys as well.